Skip to content

Commit

Permalink
further adventures in phpstanistan
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine committed Oct 15, 2023
1 parent 5dc2696 commit da510ae
Show file tree
Hide file tree
Showing 48 changed files with 225 additions and 784 deletions.
4 changes: 2 additions & 2 deletions app/Better/Bad.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function mode(): string {
}

public function setBadType(string $bad): static {
$this->torrentFlag = match($bad) {
$this->torrentFlag = match($bad) { /** @phpstan-ignore-line */
'files' => \Gazelle\TorrentFlag::badFile,
'folders' => \Gazelle\TorrentFlag::badFolder,
'lineage' => \Gazelle\TorrentFlag::noLineage,
Expand All @@ -24,7 +24,7 @@ public function torrentFlag(): \Gazelle\TorrentFlag {
}

public function heading(): string {
return match($this->torrentFlag) {
return match($this->torrentFlag) { /** @phpstan-ignore-line */
\Gazelle\TorrentFlag::badFile => 'Releases with with bad filenames',
\Gazelle\TorrentFlag::badFolder => 'Releases with with bad folders',
\Gazelle\TorrentFlag::noLineage => 'Releases with missing lineage details',
Expand Down
27 changes: 15 additions & 12 deletions app/Contest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function info(): array {
$info['bonus_pool'] = new BonusPool($info['bonus_pool_id']);
// calculate the ratios of how the bonus pool is carved up
// sum(bonusUser + bonusContest + bonusPerEntry) == 1.0
$bonusClaimSum = $info['bonus_user'] + $info['bonus_contest'] + $info['bonus_per_entry'];
$bonusClaimSum = (int)floor($info['bonus_user'] + $info['bonus_contest'] + $info['bonus_per_entry']);
$info['bonus_user_ratio'] = $info['bonus_user'] / $bonusClaimSum;
$info['bonus_contest_ratio'] = $info['bonus_contest'] / $bonusClaimSum;
$info['bonus_per_entry_ratio'] = 1 - ($info['bonus_user_ratio'] + $info['bonus_contest_ratio']);
Expand Down Expand Up @@ -150,16 +150,16 @@ public function hasBonusPool(): bool {
return !is_null($this->info()['bonus_pool']);
}

public function bonusPoolTotal(): float {
return $this->info()['bonus_pool']?->total() ?? 0.0;
public function bonusPoolTotal(): int {
return (int)($this->info()['bonus_pool']?->total() ?? 0);
}

public function bonusStatus(): string {
return $this->info()['bonus_status'];
}

public function bonusPerContest(): float {
return $this->info()['bonus_contest'];
public function bonusPerContest(): int {
return (int)$this->info()['bonus_contest'];
}

public function bonusPerContestRatio(): float {
Expand All @@ -168,11 +168,11 @@ public function bonusPerContestRatio(): float {

public function bonusPerContestValue(): int {
$totalUsers = $this->totalUsers();
return $totalUsers ? floor($this->bonusPoolTotal() * $this->bonusPerContestRatio() / $totalUsers) : 0;
return $totalUsers ? (int)floor($this->bonusPoolTotal() * $this->bonusPerContestRatio() / $totalUsers) : 0;
}

public function bonusPerEntry(): float {
return $this->info()['bonus_per_entry'];
public function bonusPerEntry(): int {
return (int)$this->info()['bonus_per_entry'];
}

public function bonusPerEntryRatio(): float {
Expand All @@ -181,11 +181,11 @@ public function bonusPerEntryRatio(): float {

public function bonusPerEntryValue(): int {
$totalEntries = $this->totalEntries();
return $totalEntries ? floor($this->bonusPoolTotal() * $this->bonusPerEntryRatio() / $totalEntries) : 0;
return $totalEntries ? (int)floor($this->bonusPoolTotal() * $this->bonusPerEntryRatio() / $totalEntries) : 0;
}

public function bonusPerUser(): float {
return $this->info()['bonus_user'];
public function bonusPerUser(): int {
return (int)$this->info()['bonus_user'];
}

public function bonusPerUserRatio(): float {
Expand All @@ -194,7 +194,7 @@ public function bonusPerUserRatio(): float {

public function bonusPerUserValue(): int {
$totalEnabledUsers = (new Stats\Users())->enabledUserTotal();
return $totalEnabledUsers ? floor($this->bonusPoolTotal() * $this->bonusPerUserRatio() / $totalEnabledUsers) : 0;
return $totalEnabledUsers ? (int)floor($this->bonusPoolTotal() * $this->bonusPerUserRatio() / $totalEnabledUsers) : 0;
}

public function isOpen(): bool {
Expand Down Expand Up @@ -302,6 +302,9 @@ public function doPayout(Manager\User $userMan): int {
$perEntryBonus = $this->bonusPerEntryValue();

$report = fopen(TMPDIR . "/payout-contest-" . $this->id . ".txt", 'a');
if ($report === false) {
return 0;
}
fprintf($report, "# user=%d contest=%d entry=%d\n", $enabledUserBonus, $contestBonus, $perEntryBonus);

$participants = $this->type()->userPayout($enabledUserBonus, $contestBonus, $perEntryBonus);
Expand Down
3 changes: 1 addition & 2 deletions app/Contest/RequestFill.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/* how many requests filled */

class RequestFill extends AbstractContest {

public function leaderboard(int $limit, int $offset): array {
// TODO
return [];
Expand Down Expand Up @@ -85,7 +84,7 @@ public function userPayout(int $enabledUserBonus, int $contestBonus, int $perEnt
return self::$db->to_array('ID', MYSQLI_ASSOC, false);
}

public function requestPairs() {
public function requestPairs(): array {
$key = "contest_pairs_" . $this->id;
$pairs = self::$cache->get_value($key);
if ($pairs === false) {
Expand Down
12 changes: 6 additions & 6 deletions app/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract public function payload(): array;
/**
* Configure JSON printing (any of the json_encode JSON_* constants)
*/
public function setMode(int $mode) {
public function setMode(int $mode): static {
$this->mode = $mode;
return $this;
}
Expand All @@ -24,16 +24,16 @@ public function setMode(int $mode) {
* value when there is significant change in the payload.
* If not called, the version defaults to 1.
*/
public function setVersion(int $version) {
public function setVersion(int $version): static {
$this->version = $version;
return $this;
}

/**
* General failure routine for when bad things happen.
*/
public function failure(string $message) {
print json_encode(
public function failure(string $message): void {
echo json_encode(
array_merge([
'status' => 'failure',
'response' => [],
Expand All @@ -46,13 +46,13 @@ public function failure(string $message) {
);
}

public function emit() {
public function emit(): void {
$payload = $this->payload();
if (!$payload) {
return;
}
try {
print json_encode(
echo json_encode(
array_merge([
'status' => 'success',
'response' => $payload,
Expand Down
8 changes: 5 additions & 3 deletions app/Manager/AutoEnable.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function findByUser(\Gazelle\User $user): ?\Gazelle\User\AutoEnable {
LIMIT 1
", $user->id()
);
return is_null($id) ? null : new \Gazelle\User\AutoEnable($id, $user);
return is_null($id) ? null : new \Gazelle\User\AutoEnable((int)$id, $user);
}

public function findByToken(string $token): ?\Gazelle\User\AutoEnable {
Expand All @@ -82,7 +82,9 @@ public function findByToken(string $token): ?\Gazelle\User\AutoEnable {
public function openTotal(): int {
$total = self::$cache->get_value(self::CACHE_TOTAL_OPEN);
if ($total === false) {
$total = self::$db->scalar("SELECT count(*) FROM users_enable_requests WHERE Outcome IS NULL");
$total = (int)self::$db->scalar("
SELECT count(*) FROM users_enable_requests WHERE Outcome IS NULL
");
self::$cache->cache_value(self::CACHE_TOTAL_OPEN, $total);
}
return $total;
Expand Down Expand Up @@ -149,7 +151,7 @@ public function filterAdmin(string $username): static {
public function total(): int {
$joinList = implode(' ', $this->join);
$where = $this->cond ? ('WHERE ' . implode(' AND ', $this->cond)) : '';
return self::$db->scalar("
return (int)self::$db->scalar("
SELECT count(*)
FROM users_enable_requests AS uer
INNER JOIN users_info ui ON (ui.UserID = uer.UserID)
Expand Down
11 changes: 6 additions & 5 deletions app/Manager/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function findById(int $blogId): ?\Gazelle\Blog {
", $blogId
);
if (!is_null($id)) {
self::$cache->cache_value($key, $id, 7200);
self::$cache->cache_value($key, (int)$id, 7200);
}
}
return $id ? new \Gazelle\Blog($id) : null;
return $id ? new \Gazelle\Blog((int)$id) : null;
}

/**
Expand All @@ -50,11 +50,12 @@ public function headlines(): array {
$idList = self::$cache->get_value(self::CACHE_KEY);
if ($idList === false) {
self::$db->prepared_query("
SELECT b.ID FROM blog b
SELECT b.ID
FROM blog b
ORDER BY b.Time DESC
LIMIT 20
");
$idList = self::$db->collect(0, false) ?? [];
$idList = self::$db->collect(0, false);
self::$cache->cache_value(self::CACHE_KEY, $idList, 7200);
}
return array_map(fn ($id) => new \Gazelle\Blog($id), $idList);
Expand Down Expand Up @@ -88,6 +89,6 @@ public function latestId(): int {
*/
public function latestEpoch(): int {
$latest = $this->latest();
return $latest ? strtotime($latest->created()) : 0;
return $latest ? (int)strtotime($latest->created()) : 0;
}
}
2 changes: 1 addition & 1 deletion app/Manager/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function remove(int $id): int {
}

public function total(): int {
return self::$db->scalar("
return (int)self::$db->scalar("
SELECT count(*) FROM changelog
");
}
Expand Down
16 changes: 7 additions & 9 deletions app/Manager/ClientWhitelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@ public function create(string $peer, string $vstring) {

/**
* Get the peer ID of client
*
* @param int $clientId The ID of the client
* @return string The peer identifier
*/
public function peerId(int $clientId) {
return self::$db->scalar("
public function peerId(int $clientId): string {
return (string)self::$db->scalar("
SELECT peer_id
FROM xbt_client_whitelist
WHERE id = ?
", $clientId
);
}

public function list() {
if (($list = self::$cache->get_value(self::CACHE_KEY)) === false) {
public function list(): array {
$list = self::$cache->get_value(self::CACHE_KEY);
if ($list === false) {
self::$db->prepared_query("
SELECT id as client_id, vstring, peer_id
FROM xbt_client_whitelist
ORDER BY peer_id ASC
");
$list = self::$db->to_array('client_id', MYSQLI_ASSOC);
$list = self::$db->to_array('client_id', MYSQLI_ASSOC, false);
self::$cache->cache_value(self::CACHE_KEY, $list, 0);
}
return $list;
Expand All @@ -59,7 +57,7 @@ public function list() {
* @param string $vstring The new client vstring
* @return string The previous peer identifier
*/
public function modify(int $clientId, string $peer, string $vstring) {
public function modify(int $clientId, string $peer, string $vstring): string {
$prevPeer = $this->peerId($clientId);
self::$db->prepared_query("
UPDATE xbt_client_whitelist SET
Expand Down
2 changes: 1 addition & 1 deletion app/Manager/DuplicateIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DuplicateIP extends \Gazelle\Base {
public function total(int $threshold): int {
return self::$db->scalar("
return (int)self::$db->scalar("
SELECT count(*)
FROM users_main AS um
WHERE um.Enabled = '1'
Expand Down
10 changes: 5 additions & 5 deletions app/Manager/EmailBlacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Gazelle\Manager;

class EmailBlacklist extends \Gazelle\Base {
protected $filterComment;
protected $filterEmail;
protected string $filterComment;
protected string $filterEmail;

public function create(array $info): int {
self::$db->prepared_query("
Expand Down Expand Up @@ -37,12 +37,12 @@ public function remove(int $id): bool {
return self::$db->affected_rows() === 1;
}

public function filterComment(string $filterComment) {
public function filterComment(string $filterComment): static {
$this->filterComment = $filterComment;
return $this;
}

public function filterEmail(string $filterEmail) {
public function filterEmail(string $filterEmail): static {
$this->filterEmail = $filterEmail;
return $this;
}
Expand All @@ -66,7 +66,7 @@ public function queryBase(): array {

public function total(): int {
[$from, $args] = $this->queryBase();
return self::$db->scalar("SELECT count(*) $from", ...$args);
return (int)self::$db->scalar("SELECT count(*) $from", ...$args);
}

public function page(int $limit, int $offset): array {
Expand Down
13 changes: 6 additions & 7 deletions app/Manager/ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Gazelle\Manager;

class ErrorLog extends \Gazelle\BaseManager {

protected string $filter;

public function create(
string $uri, int $userId, float $duration, int $memory, int $nrQuery, int $nrCache,
string $digest, string $trace, string $request, string $errorList, string $loggedVar
): int {
self::$db->begin_transaction();
$id = self::$db->scalar("
$id = (int)self::$db->scalar("
SELECT error_log_id FROM error_log WHERE digest = ?
", $digest
);
Expand Down Expand Up @@ -40,15 +39,15 @@ public function create(
* Get an eror log based on its ID
*/
public function findById(int $errorId): ?\Gazelle\ErrorLog {
$id = self::$db->scalar("
$id = (int)self::$db->scalar("
SELECT error_log_id FROM error_log WHERE error_log_id = ?
", $errorId
);
return $id ? new \Gazelle\ErrorLog($id) : null;
}

public function findByPrev(int $errorId): ?\Gazelle\ErrorLog {
$id = self::$db->scalar("
$id = (int)self::$db->scalar("
SELECT error_log_id
FROM error_log
WHERE updated > (SELECT updated FROM error_log WHERE error_log_id = ?)
Expand All @@ -60,7 +59,7 @@ public function findByPrev(int $errorId): ?\Gazelle\ErrorLog {
}

public function findByNext(int $errorId): ?\Gazelle\ErrorLog {
$id = self::$db->scalar("
$id = (int)self::$db->scalar("
SELECT error_log_id
FROM error_log
WHERE updated < (SELECT updated FROM error_log WHERE error_log_id = ?)
Expand All @@ -71,7 +70,7 @@ public function findByNext(int $errorId): ?\Gazelle\ErrorLog {
return $id ? new \Gazelle\ErrorLog($id) : null;
}

public function setFilter(string $filter) {
public function setFilter(string $filter): static {
$this->filter = $filter;
return $this;
}
Expand All @@ -84,7 +83,7 @@ public function total(): int {
$where = "WHERE uri LIKE concat('%', ?, '%')";
$args[] = $this->filter;
}
return self::$db->scalar("
return (int)self::$db->scalar("
SELECT count(*) FROM error_log $where
", ...$args
);
Expand Down
Loading

0 comments on commit da510ae

Please sign in to comment.