From 66b9fc376484ba6e7f5575e24734347866a36f1a Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 21:52:38 -0800 Subject: [PATCH 01/10] Add rector as a job in the standard test suite This is done for both local testing and GitHub Actions. Use `--dry-run` so that no changes are made during testing. --- .github/workflows/run-tests.yml | 24 ++++++++++++++++++++++++ composer.json | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 408d32b80..6d4ae0607 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -120,3 +120,27 @@ jobs: - name: Run code sniffer run: composer phpcs + + rector: + name: Rector + needs: build + runs-on: ubuntu-22.04 + steps: + - name: Checkout the source code + uses: actions/checkout@v4 + + - name: Set up Docker buildx + uses: docker/setup-buildx-action@v3 + + - name: Build image from cache + uses: docker/build-push-action@v6 + with: + context: . + build-args: | + NO_DEV=0 + tags: local/smr:latest + cache-from: type=gha + load: true + + - name: Run rector analysis + run: composer rector diff --git a/composer.json b/composer.json index f4ca3ce62..7ba311f4d 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,8 @@ "test": [ "@phpstan", "@phpunit", - "@phpcs" + "@phpcs", + "@rector" ], "phpunit": [ "docker compose --env-file test/env run --rm phpunit" @@ -62,6 +63,9 @@ "phpstan": [ "docker compose --env-file test/env run --rm phpstan" ], + "rector": [ + "docker compose --env-file test/env run --rm rector --dry-run" + ], "stop": [ "docker compose stop", "docker compose rm --force" From c3f12492288a276f8bae4b1123f293d8a721077d Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:09:56 -0800 Subject: [PATCH 02/10] rector.php: switch to RectorConfigBuilder Use the new fluent interface for setting up the rector configuration. No functionality changes. --- rector.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/rector.php b/rector.php index b498d69ba..66c263aa3 100644 --- a/rector.php +++ b/rector.php @@ -8,20 +8,20 @@ use Rector\Php81\Rector\Array_\FirstClassCallableRector; use Rector\PHPUnit\Set\PHPUnitSetList; -return static function (RectorConfig $rectorConfig): void { - $rectorConfig->paths([ +return RectorConfig::configure() + ->withPaths([ __DIR__ . '/test', __DIR__ . '/src', - ]); - $rectorConfig->importNames(true, false); - - $rectorConfig->rule(DirNameFileConstantToDirConstantRector::class); - $rectorConfig->rule(JsonThrowOnErrorRector::class); - $rectorConfig->rule(NullCoalescingOperatorRector::class); - $rectorConfig->rule(ClassOnObjectRector::class); - $rectorConfig->rule(FirstClassCallableRector::class); - - $rectorConfig->sets([ + ]) + ->withImportNames(true, false) + ->withRules([ + DirNameFileConstantToDirConstantRector::class, + JsonThrowOnErrorRector::class, + NullCoalescingOperatorRector::class, + ClassOnObjectRector::class, + FirstClassCallableRector::class, + ]) + ->withSets([ PHPUnitSetList::PHPUNIT_100, - ]); -}; + ]) +; From 9aa072af1dd483bbafa6d4db4421fb518df14ffb Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 21:39:03 -0800 Subject: [PATCH 03/10] imprint.php: fix for JsonThrowOnErrorRector rule This is a regression from a previously added rule. See 78baae684c. --- src/htdocs/imprint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/htdocs/imprint.php b/src/htdocs/imprint.php index a3ab87d00..6a8c37f1f 100644 --- a/src/htdocs/imprint.php +++ b/src/htdocs/imprint.php @@ -6,7 +6,7 @@ if ($json === false) { throw new Exception('Failed to fetch JSON imprint'); } -$data = json_decode($json); +$data = json_decode($json, flags: JSON_THROW_ON_ERROR); $contact = $data->imprintContact; $board = array_map( fn(stdClass $member): string => $member->role->en . ': ' . $member->name, From e84a2cae73ea92ad1bf8685e7dee89c84f8881e5 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:17:40 -0800 Subject: [PATCH 04/10] rector: add IfIssetToCoalescingRector This rule makes expressions like this safer because we only write out the variable name once. --- rector.php | 6 ++++-- src/lib/Smr/Planet.php | 10 ++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/rector.php b/rector.php index 66c263aa3..614897ae7 100644 --- a/rector.php +++ b/rector.php @@ -2,6 +2,7 @@ use Rector\Config\RectorConfig; use Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector; +use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; @@ -15,11 +16,12 @@ ]) ->withImportNames(true, false) ->withRules([ + ClassOnObjectRector::class, DirNameFileConstantToDirConstantRector::class, + FirstClassCallableRector::class, + IfIssetToCoalescingRector::class, JsonThrowOnErrorRector::class, NullCoalescingOperatorRector::class, - ClassOnObjectRector::class, - FirstClassCallableRector::class, ]) ->withSets([ PHPUnitSetList::PHPUNIT_100, diff --git a/src/lib/Smr/Planet.php b/src/lib/Smr/Planet.php index 65a0f46ec..b985c74d7 100644 --- a/src/lib/Smr/Planet.php +++ b/src/lib/Smr/Planet.php @@ -597,10 +597,7 @@ public function getStockpile(?int $goodID = null): int|array { if ($goodID === null) { return $this->stockpile; } - if (isset($this->stockpile[$goodID])) { - return $this->stockpile[$goodID]; - } - return 0; + return $this->stockpile[$goodID] ?? 0; } public function hasStockpile(?int $goodID = null): bool { @@ -659,10 +656,7 @@ public function getBuildings(): array { public function getBuilding(int $buildingTypeID): int { $buildings = $this->getBuildings(); - if (isset($buildings[$buildingTypeID])) { - return $buildings[$buildingTypeID]; - } - return 0; + return $buildings[$buildingTypeID] ?? 0; } public function hasBuilding(int $buildingTypeID): bool { From a5c4d3790a59dfb705c96ae1821e51ae7bf9d732 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:20:10 -0800 Subject: [PATCH 05/10] rector: add ClassPropertyAssignToConstructorPromotionRector This rule simplifies the class interface by combining the ctor argument with the property declaration. --- rector.php | 2 ++ src/lib/Smr/AbstractShip.php | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/rector.php b/rector.php index 614897ae7..714f808e1 100644 --- a/rector.php +++ b/rector.php @@ -5,6 +5,7 @@ use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; +use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; use Rector\Php81\Rector\Array_\FirstClassCallableRector; use Rector\PHPUnit\Set\PHPUnitSetList; @@ -17,6 +18,7 @@ ->withImportNames(true, false) ->withRules([ ClassOnObjectRector::class, + ClassPropertyAssignToConstructorPromotionRector::class, DirNameFileConstantToDirConstantRector::class, FirstClassCallableRector::class, IfIssetToCoalescingRector::class, diff --git a/src/lib/Smr/AbstractShip.php b/src/lib/Smr/AbstractShip.php index 06b7a9acb..476f82718 100644 --- a/src/lib/Smr/AbstractShip.php +++ b/src/lib/Smr/AbstractShip.php @@ -30,8 +30,6 @@ class AbstractShip { RACE_NIJARIN => SHIP_TYPE_REDEEMER, ]; - protected AbstractPlayer $player; - protected int $gameID; protected ShipType $shipType; @@ -51,9 +49,8 @@ class AbstractShip { protected bool $hasChangedCloak = false; protected bool $hasChangedIllusion = false; - public function __construct(AbstractPlayer $player) { - $this->player = $player; - $this->gameID = $player->getGameID(); + public function __construct(protected AbstractPlayer $player) { + $this->gameID = $this->player->getGameID(); $this->regenerateShipType(); } From 81ae13babdc1619378d5b650dbd36eccf6a80128 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:25:28 -0800 Subject: [PATCH 06/10] rector: add ReadOnlyPropertyRector If class properties are never set outside the ctor, they can be marked as readonly. --- rector.php | 2 ++ src/lib/Smr/Blackjack/Table.php | 2 +- src/lib/Smr/Routes/RouteIterator.php | 4 ++-- src/lib/Smr/Session.php | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/rector.php b/rector.php index 714f808e1..34f3d737e 100644 --- a/rector.php +++ b/rector.php @@ -8,6 +8,7 @@ use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; use Rector\Php81\Rector\Array_\FirstClassCallableRector; +use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; use Rector\PHPUnit\Set\PHPUnitSetList; return RectorConfig::configure() @@ -24,6 +25,7 @@ IfIssetToCoalescingRector::class, JsonThrowOnErrorRector::class, NullCoalescingOperatorRector::class, + ReadOnlyPropertyRector::class, ]) ->withSets([ PHPUnitSetList::PHPUNIT_100, diff --git a/src/lib/Smr/Blackjack/Table.php b/src/lib/Smr/Blackjack/Table.php index 4eabc8d67..85d3684a4 100644 --- a/src/lib/Smr/Blackjack/Table.php +++ b/src/lib/Smr/Blackjack/Table.php @@ -7,7 +7,7 @@ */ class Table { - private Deck $deck; + private readonly Deck $deck; public Hand $playerHand; public Hand $dealerHand; diff --git a/src/lib/Smr/Routes/RouteIterator.php b/src/lib/Smr/Routes/RouteIterator.php index a59bb2f9d..b6ae72dd9 100644 --- a/src/lib/Smr/Routes/RouteIterator.php +++ b/src/lib/Smr/Routes/RouteIterator.php @@ -12,12 +12,12 @@ class RouteIterator { /** @var InfiniteIterator> */ - private InfiniteIterator $routeIterator; + private readonly InfiniteIterator $routeIterator; private TransactionType $transaction = TransactionType::Buy; public function __construct( - private MultiplePortRoute $route, + private readonly MultiplePortRoute $route, ) { $oneWayRoutes = $route->getOneWayRoutes(); $this->routeIterator = new InfiniteIterator(new ArrayIterator($oneWayRoutes)); diff --git a/src/lib/Smr/Session.php b/src/lib/Smr/Session.php index 79b105c72..0cff32b7c 100644 --- a/src/lib/Smr/Session.php +++ b/src/lib/Smr/Session.php @@ -42,7 +42,7 @@ class Session { private array $requestData = []; private bool $generate; public readonly bool $ajax; - private string $SN; + private readonly string $SN; private string $lastSN; private int $accountID; private float $lastAccessed; From 75f8d9fcc3c012d48d53cc085578b4c346229d3c Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:27:51 -0800 Subject: [PATCH 07/10] rector: add AddOverrideAttributeToOverriddenMethodsRector No changes, but is something we want to make sure is done in the future. Related to #1775. --- rector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rector.php b/rector.php index 34f3d737e..7bbe30734 100644 --- a/rector.php +++ b/rector.php @@ -9,6 +9,7 @@ use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; use Rector\Php81\Rector\Array_\FirstClassCallableRector; use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; +use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; use Rector\PHPUnit\Set\PHPUnitSetList; return RectorConfig::configure() @@ -18,6 +19,7 @@ ]) ->withImportNames(true, false) ->withRules([ + AddOverrideAttributeToOverriddenMethodsRector::class, ClassOnObjectRector::class, ClassPropertyAssignToConstructorPromotionRector::class, DirNameFileConstantToDirConstantRector::class, From f3397dd684f51f67773affd3de2e92ad2db8a5d1 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:49:38 -0800 Subject: [PATCH 08/10] rector: add AddTypeToConstRector Explicitly declare types for all class constants. Related to #1775. --- rector.php | 2 + src/lib/Smr/AbstractPlayer.php | 12 +++--- src/lib/Smr/AbstractShip.php | 10 ++--- src/lib/Smr/Account.php | 8 ++-- src/lib/Smr/AdminPermissions.php | 4 +- src/lib/Smr/Alliance.php | 12 +++--- src/lib/Smr/BarDrink.php | 6 +-- src/lib/Smr/Blackjack/Card.php | 10 ++--- src/lib/Smr/Bounty.php | 2 +- src/lib/Smr/Chess/Board.php | 4 +- src/lib/Smr/Chess/ChessGame.php | 4 +- src/lib/Smr/Chess/ChessPiece.php | 12 +++--- src/lib/Smr/Combat/Weapon/AbstractWeapon.php | 2 +- src/lib/Smr/Combat/Weapon/CombatDrones.php | 2 +- src/lib/Smr/Combat/Weapon/Mines.php | 6 +-- src/lib/Smr/Combat/Weapon/Weapon.php | 6 +-- src/lib/Smr/EnhancedWeaponEvent.php | 4 +- src/lib/Smr/Force.php | 24 +++++------ src/lib/Smr/Galaxy.php | 10 ++--- src/lib/Smr/Game.php | 14 +++---- src/lib/Smr/Location.php | 2 +- src/lib/Smr/Lotto.php | 4 +- src/lib/Smr/Planet.php | 12 +++--- src/lib/Smr/PlanetStructureType.php | 4 +- src/lib/Smr/PlanetTypes/AridPlanet.php | 2 +- src/lib/Smr/PlanetTypes/DefenseWorld.php | 2 +- src/lib/Smr/PlanetTypes/DwarfPlanet.php | 2 +- src/lib/Smr/PlanetTypes/PlanetType.php | 4 +- src/lib/Smr/PlanetTypes/ProtoPlanet.php | 2 +- src/lib/Smr/PlanetTypes/TerranPlanet.php | 2 +- src/lib/Smr/Port.php | 42 ++++++++++---------- src/lib/Smr/Race.php | 2 +- src/lib/Smr/RaceDetails.php | 4 +- src/lib/Smr/Routes/RouteGenerator.php | 4 +- src/lib/Smr/Sector.php | 4 +- src/lib/Smr/SectorLock.php | 4 +- src/lib/Smr/Session.php | 4 +- src/lib/Smr/Ship.php | 2 +- src/lib/Smr/TransactionType.php | 2 +- src/lib/Smr/Treaty.php | 2 +- src/lib/Smr/UserRanking.php | 8 ++-- src/lib/Smr/VoteLink.php | 2 +- src/lib/Smr/WeightedRandom.php | 2 +- 43 files changed, 137 insertions(+), 135 deletions(-) diff --git a/rector.php b/rector.php index 7bbe30734..205328ac9 100644 --- a/rector.php +++ b/rector.php @@ -9,6 +9,7 @@ use Rector\Php80\Rector\FuncCall\ClassOnObjectRector; use Rector\Php81\Rector\Array_\FirstClassCallableRector; use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; +use Rector\Php83\Rector\ClassConst\AddTypeToConstRector; use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector; use Rector\PHPUnit\Set\PHPUnitSetList; @@ -19,6 +20,7 @@ ]) ->withImportNames(true, false) ->withRules([ + AddTypeToConstRector::class, AddOverrideAttributeToOverriddenMethodsRector::class, ClassOnObjectRector::class, ClassPropertyAssignToConstructorPromotionRector::class, diff --git a/src/lib/Smr/AbstractPlayer.php b/src/lib/Smr/AbstractPlayer.php index 92fdcc251..aaf8e7ab0 100644 --- a/src/lib/Smr/AbstractPlayer.php +++ b/src/lib/Smr/AbstractPlayer.php @@ -24,13 +24,13 @@ abstract class AbstractPlayer { use RaceID; - protected const TIME_FOR_FEDERAL_BOUNTY_ON_PR = 10800; - protected const TIME_FOR_ALLIANCE_SWITCH = 0; + protected const int TIME_FOR_FEDERAL_BOUNTY_ON_PR = 10800; + protected const int TIME_FOR_ALLIANCE_SWITCH = 0; - protected const SHIP_INSURANCE_FRACTION = 0.25; // ship value regained on death + protected const float SHIP_INSURANCE_FRACTION = 0.25; // ship value regained on death - protected const HOF_CHANGED = 1; - protected const HOF_NEW = 2; + protected const int HOF_CHANGED = 1; + protected const int HOF_NEW = 2; /** @var array>> */ protected static array $CACHE_SECTOR_PLAYERS = []; @@ -41,7 +41,7 @@ abstract class AbstractPlayer { /** @var array> */ protected static array $CACHE_PLAYERS = []; - public const SQL = 'account_id = :account_id AND game_id = :game_id'; + public const string SQL = 'account_id = :account_id AND game_id = :game_id'; /** @var array{account_id: int, game_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/AbstractShip.php b/src/lib/Smr/AbstractShip.php index 476f82718..cb96b0aae 100644 --- a/src/lib/Smr/AbstractShip.php +++ b/src/lib/Smr/AbstractShip.php @@ -13,12 +13,12 @@ class AbstractShip { // Player exp gained for each point of damage done - protected const EXP_PER_DAMAGE_PLAYER = 0.375; - protected const EXP_PER_DAMAGE_PLANET = 1.0; // note that planet damage is reduced - protected const EXP_PER_DAMAGE_PORT = 0.15; - protected const EXP_PER_DAMAGE_FORCE = 0.075; + protected const float EXP_PER_DAMAGE_PLAYER = 0.375; + protected const float EXP_PER_DAMAGE_PLANET = 1.0; // note that planet damage is reduced + protected const float EXP_PER_DAMAGE_PORT = 0.15; + protected const float EXP_PER_DAMAGE_FORCE = 0.075; - protected const STARTER_SHIPS = [ + protected const array STARTER_SHIPS = [ RACE_NEUTRAL => SHIP_TYPE_GALACTIC_SEMI, RACE_ALSKANT => SHIP_TYPE_SMALL_TIMER, RACE_CREONTI => SHIP_TYPE_MEDIUM_CARGO_HULK, diff --git a/src/lib/Smr/Account.php b/src/lib/Smr/Account.php index dc600e582..bc94b2812 100644 --- a/src/lib/Smr/Account.php +++ b/src/lib/Smr/Account.php @@ -11,8 +11,8 @@ class Account { - protected const USER_RANKINGS_EACH_STAT_POW = .9; - protected const USER_RANKINGS_SCORE = [ + protected const float USER_RANKINGS_EACH_STAT_POW = .9; + protected const array USER_RANKINGS_SCORE = [ // [Stat, a, b] // Used as: pow(Stat * a, USER_RANKINGS_EACH_STAT_POW) * b [['Trade', 'Experience', 'Total'], .1, 0.5], @@ -22,7 +22,7 @@ class Account { /** @var array */ protected static array $CACHE_ACCOUNTS = []; - protected const DEFAULT_HOTKEYS = [ + protected const array DEFAULT_HOTKEYS = [ 'MoveUp' => ['w', 'up'], 'ScanUp' => ['shift+w', 'shift+up'], 'MoveLeft' => ['a', 'left'], @@ -42,7 +42,7 @@ class Account { 'AttackTrader' => ['f'], ]; - public const SQL = 'account_id = :account_id'; + public const string SQL = 'account_id = :account_id'; /** @var array{account_id: int} */ protected readonly array $SQLID; diff --git a/src/lib/Smr/AdminPermissions.php b/src/lib/Smr/AdminPermissions.php index 18d3df52f..451c3089d 100644 --- a/src/lib/Smr/AdminPermissions.php +++ b/src/lib/Smr/AdminPermissions.php @@ -35,7 +35,7 @@ class AdminPermissions { // The array keys must not be changed because they are referred to // in the `account_has_permission` database table. // Info is [Permission Name, Page to Link, Category]. - private const PERMISSION_TABLE = [ + private const array PERMISSION_TABLE = [ 1 => ['Manage Admin Permissions', AdminPermissionManage::class, 3], 2 => ['Database Cleanup', DatabaseCleanup::class, 3], 3 => ['Server Open/Close', ServerStatus::class, 3], @@ -69,7 +69,7 @@ class AdminPermissions { 37 => ['Manage NPCs', NpcManage::class, 5], ]; - private const PERMISSION_CATEGORIES = [ + private const array PERMISSION_CATEGORIES = [ 1 => 'Monitor Players', 2 => 'Community Services', 3 => 'Administrative', diff --git a/src/lib/Smr/Alliance.php b/src/lib/Smr/Alliance.php index 47e823b96..5949aded8 100644 --- a/src/lib/Smr/Alliance.php +++ b/src/lib/Smr/Alliance.php @@ -11,7 +11,7 @@ class Alliance { /** @var array> */ protected static array $CACHE_ALLIANCES = []; - public const SQL = 'alliance_id = :alliance_id AND game_id = :game_id'; + public const string SQL = 'alliance_id = :alliance_id AND game_id = :game_id'; /** @var array{alliance_id: int, game_id: int} */ public readonly array $SQLID; @@ -36,13 +36,13 @@ class Alliance { protected array $seedlist; // Recruit type constants - public const RECRUIT_OPEN = 'open'; - public const RECRUIT_CLOSED = 'closed'; - public const RECRUIT_PASSWORD = 'password'; + public const string RECRUIT_OPEN = 'open'; + public const string RECRUIT_CLOSED = 'closed'; + public const string RECRUIT_PASSWORD = 'password'; // Database constraints - public const MAXLENGTH_NAME = 36; // varchar(36) - public const MAXLENGTH_DESCRIPTION = 255; // varchar(255) + public const int MAXLENGTH_NAME = 36; // varchar(36) + public const int MAXLENGTH_DESCRIPTION = 255; // varchar(255) public static function clearCache(): void { self::$CACHE_ALLIANCES = []; diff --git a/src/lib/Smr/BarDrink.php b/src/lib/Smr/BarDrink.php index 53c4b761f..3fa3c7212 100644 --- a/src/lib/Smr/BarDrink.php +++ b/src/lib/Smr/BarDrink.php @@ -8,9 +8,9 @@ class BarDrink { // Special drink used in missions - public const SALVENE_SWAMP_SODA = 'Salvene Swamp Soda'; + public const string SALVENE_SWAMP_SODA = 'Salvene Swamp Soda'; - private const DRINK_NAMES = [ + private const array DRINK_NAMES = [ 'Spooky Midnight Special', 'Azoolian Sunrise Special', 'Big Momma Mojito', @@ -33,7 +33,7 @@ class BarDrink { 'Nijarin Ion Martini', ]; - private const SPECIAL_DRINK_MESSAGES = [ + private const array SPECIAL_DRINK_MESSAGES = [ 'Spooky Midnight Special' => 'Suddenly the secrets of the universe become manifestly clear and you are at peace.', 'Azoolian Sunrise Special' => 'At the bottom of the glass, you see a reflection of the best trader in the universe, and it is you.', ]; diff --git a/src/lib/Smr/Blackjack/Card.php b/src/lib/Smr/Blackjack/Card.php index 75eb0ae87..c2668786b 100644 --- a/src/lib/Smr/Blackjack/Card.php +++ b/src/lib/Smr/Blackjack/Card.php @@ -8,12 +8,12 @@ class Card { // Special card ranks - private const RANK_ACE = 1; - private const RANK_JACK = 11; - private const RANK_QUEEN = 12; - private const RANK_KING = 13; + private const int RANK_ACE = 1; + private const int RANK_JACK = 11; + private const int RANK_QUEEN = 12; + private const int RANK_KING = 13; - private const SUITS = ['hearts', 'clubs', 'diamonds', 'spades']; + private const array SUITS = ['hearts', 'clubs', 'diamonds', 'spades']; private readonly int $rank; // non-unique rank of the card (1-indexed) diff --git a/src/lib/Smr/Bounty.php b/src/lib/Smr/Bounty.php index 1c9536def..d3b532aa7 100644 --- a/src/lib/Smr/Bounty.php +++ b/src/lib/Smr/Bounty.php @@ -9,7 +9,7 @@ class Bounty { /** * Maximum amount of bounty.credits in the database */ - private const MAX_CREDITS = SQL_MAX_UNSIGNED_INT; + private const int MAX_CREDITS = SQL_MAX_UNSIGNED_INT; /** * Returns a list of all active (not claimable) bounties for given location $type. diff --git a/src/lib/Smr/Chess/Board.php b/src/lib/Smr/Chess/Board.php index 20ac0681f..12946fac6 100644 --- a/src/lib/Smr/Chess/Board.php +++ b/src/lib/Smr/Chess/Board.php @@ -6,8 +6,8 @@ class Board { - public const NX = 8; // number of x-coordinates - public const NY = 8; // number of y-coordinates + public const int NX = 8; // number of x-coordinates + public const int NY = 8; // number of y-coordinates /** @var array, array> */ private array $canCastle; diff --git a/src/lib/Smr/Chess/ChessGame.php b/src/lib/Smr/Chess/ChessGame.php index 5a048e4a7..2d8f30179 100644 --- a/src/lib/Smr/Chess/ChessGame.php +++ b/src/lib/Smr/Chess/ChessGame.php @@ -14,8 +14,8 @@ class ChessGame { - public const END_RESIGN = 0; - public const END_CANCEL = 1; + public const int END_RESIGN = 0; + public const int END_CANCEL = 1; /** @var array */ protected static array $CACHE_CHESS_GAMES = []; diff --git a/src/lib/Smr/Chess/ChessPiece.php b/src/lib/Smr/Chess/ChessPiece.php index 30865c5ba..38e4ac31d 100644 --- a/src/lib/Smr/Chess/ChessPiece.php +++ b/src/lib/Smr/Chess/ChessPiece.php @@ -6,12 +6,12 @@ class ChessPiece { - public const KING = 1; - public const QUEEN = 2; - public const ROOK = 3; - public const BISHOP = 4; - public const KNIGHT = 5; - public const PAWN = 6; + public const int KING = 1; + public const int QUEEN = 2; + public const int ROOK = 3; + public const int BISHOP = 4; + public const int KNIGHT = 5; + public const int PAWN = 6; public function __construct( public readonly Colour $colour, diff --git a/src/lib/Smr/Combat/Weapon/AbstractWeapon.php b/src/lib/Smr/Combat/Weapon/AbstractWeapon.php index 92cae95c2..9a2e63b9d 100644 --- a/src/lib/Smr/Combat/Weapon/AbstractWeapon.php +++ b/src/lib/Smr/Combat/Weapon/AbstractWeapon.php @@ -12,7 +12,7 @@ abstract class AbstractWeapon { /** * Reduce the damage done to planets by this factor */ - protected const PLANET_DAMAGE_MOD = 0.2; + protected const float PLANET_DAMAGE_MOD = 0.2; protected bool $damageRollover; diff --git a/src/lib/Smr/Combat/Weapon/CombatDrones.php b/src/lib/Smr/Combat/Weapon/CombatDrones.php index 31ac7f4de..ef2c3f9c9 100644 --- a/src/lib/Smr/Combat/Weapon/CombatDrones.php +++ b/src/lib/Smr/Combat/Weapon/CombatDrones.php @@ -12,7 +12,7 @@ class CombatDrones extends AbstractWeapon { use ForcesTrait; - protected const MAX_CDS_RAND = 54; + protected const int MAX_CDS_RAND = 54; public function __construct(int $numberOfCDs, bool $portPlanetDrones = false) { $this->amount = $numberOfCDs; diff --git a/src/lib/Smr/Combat/Weapon/Mines.php b/src/lib/Smr/Combat/Weapon/Mines.php index cd84727e2..e1475baac 100644 --- a/src/lib/Smr/Combat/Weapon/Mines.php +++ b/src/lib/Smr/Combat/Weapon/Mines.php @@ -13,9 +13,9 @@ class Mines extends AbstractWeapon { use ForcesTrait; - protected const TOTAL_ENEMY_MINES_MODIFIER = 25; - protected const FED_SHIP_DAMAGE_MODIFIER = .5; - protected const DCS_DAMAGE_MODIFIER = .75; + protected const float TOTAL_ENEMY_MINES_MODIFIER = 25; + protected const float FED_SHIP_DAMAGE_MODIFIER = .5; + protected const float DCS_DAMAGE_MODIFIER = .75; public function __construct(int $numberOfMines) { $this->amount = $numberOfMines; diff --git a/src/lib/Smr/Combat/Weapon/Weapon.php b/src/lib/Smr/Combat/Weapon/Weapon.php index e6f3dbc0a..e43eee4a7 100644 --- a/src/lib/Smr/Combat/Weapon/Weapon.php +++ b/src/lib/Smr/Combat/Weapon/Weapon.php @@ -22,10 +22,10 @@ class Weapon extends AbstractWeapon { use RaceID; - protected const BONUS_DAMAGE = 15; // additive bonus - protected const BONUS_ACCURACY = 4; // additive bonus + protected const int BONUS_DAMAGE = 15; // additive bonus + protected const int BONUS_ACCURACY = 4; // additive bonus - protected const HIGHEST_POWER_LEVEL = 5; // must track the highest power level in db + protected const int HIGHEST_POWER_LEVEL = 5; // must track the highest power level in db protected readonly WeaponType $weaponType; protected bool $bonusAccuracy = false; // default diff --git a/src/lib/Smr/EnhancedWeaponEvent.php b/src/lib/Smr/EnhancedWeaponEvent.php index 0f3b9eda7..30e706701 100644 --- a/src/lib/Smr/EnhancedWeaponEvent.php +++ b/src/lib/Smr/EnhancedWeaponEvent.php @@ -9,8 +9,8 @@ */ class EnhancedWeaponEvent { - protected const GRACE_PERIOD = 3600; // 1 hour - protected const DURATION = 21600; // 6 hours + protected const int GRACE_PERIOD = 3600; // 1 hour + protected const int DURATION = 21600; // 6 hours protected readonly Weapon $weapon; diff --git a/src/lib/Smr/Force.php b/src/lib/Smr/Force.php index ba4ff1a66..783d121d1 100644 --- a/src/lib/Smr/Force.php +++ b/src/lib/Smr/Force.php @@ -21,18 +21,18 @@ class Force { /** @var array> */ protected static array $TIDIED_UP = []; - public const LOWEST_MAX_EXPIRE_SCOUTS_ONLY = 432000; // 5 days - protected const TIME_PER_SCOUT_ONLY = 86400; // 1 = 1 day - protected const TIME_PERCENT_PER_SCOUT = 0.02; // 1/50th - protected const TIME_PERCENT_PER_COMBAT = 0.02; // 1/50th - protected const TIME_PERCENT_PER_MINE = 0.02; // 1/50th - public const REFRESH_ALL_TIME_PER_STACK = 1; // 1 second - - public const MAX_MINES = 50; - public const MAX_CDS = 50; - public const MAX_SDS = 5; - - public const SQL = 'game_id = :game_id AND sector_id = :sector_id AND owner_id = :owner_id'; + public const int LOWEST_MAX_EXPIRE_SCOUTS_ONLY = 432000; // 5 days + protected const int TIME_PER_SCOUT_ONLY = 86400; // 1 = 1 day + protected const float TIME_PERCENT_PER_SCOUT = 0.02; // 1/50th + protected const float TIME_PERCENT_PER_COMBAT = 0.02; // 1/50th + protected const float TIME_PERCENT_PER_MINE = 0.02; // 1/50th + public const int REFRESH_ALL_TIME_PER_STACK = 1; // 1 second + + public const int MAX_MINES = 50; + public const int MAX_CDS = 50; + public const int MAX_SDS = 5; + + public const string SQL = 'game_id = :game_id AND sector_id = :sector_id AND owner_id = :owner_id'; /** @var array{game_id: int, sector_id: int, owner_id: int} */ protected readonly array $SQLID; diff --git a/src/lib/Smr/Galaxy.php b/src/lib/Smr/Galaxy.php index 4e9a4f09d..044551899 100644 --- a/src/lib/Smr/Galaxy.php +++ b/src/lib/Smr/Galaxy.php @@ -11,12 +11,12 @@ class Galaxy { /** @var array> */ protected static array $CACHE_GAME_GALAXIES = []; - public const TYPE_RACIAL = 'Racial'; - public const TYPE_NEUTRAL = 'Neutral'; - public const TYPE_PLANET = 'Planet'; - public const TYPES = [self::TYPE_RACIAL, self::TYPE_NEUTRAL, self::TYPE_PLANET]; + public const string TYPE_RACIAL = 'Racial'; + public const string TYPE_NEUTRAL = 'Neutral'; + public const string TYPE_PLANET = 'Planet'; + public const array TYPES = [self::TYPE_RACIAL, self::TYPE_NEUTRAL, self::TYPE_PLANET]; - public const SQL = 'game_id = :game_id AND galaxy_id = :galaxy_id'; + public const string SQL = 'game_id = :game_id AND galaxy_id = :galaxy_id'; /** @var array{game_id: int, galaxy_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/Game.php b/src/lib/Smr/Game.php index c0a5ee166..48782eade 100644 --- a/src/lib/Smr/Game.php +++ b/src/lib/Smr/Game.php @@ -35,13 +35,13 @@ class Game { protected bool $hasChanged = false; protected bool $isNew = false; - public const GAME_TYPE_DEFAULT = 0; - public const GAME_TYPE_HUNTER_WARS = 3; - public const GAME_TYPE_SEMI_WARS = 4; - public const GAME_TYPE_DRAFT = 5; - public const GAME_TYPE_FFA = 6; - public const GAME_TYPE_NEWBIE = 7; - public const GAME_TYPES = [ + public const int GAME_TYPE_DEFAULT = 0; + public const int GAME_TYPE_HUNTER_WARS = 3; + public const int GAME_TYPE_SEMI_WARS = 4; + public const int GAME_TYPE_DRAFT = 5; + public const int GAME_TYPE_FFA = 6; + public const int GAME_TYPE_NEWBIE = 7; + public const array GAME_TYPES = [ self::GAME_TYPE_DEFAULT => 'Default', self::GAME_TYPE_HUNTER_WARS => 'Hunter Wars', self::GAME_TYPE_SEMI_WARS => 'Semi Wars', diff --git a/src/lib/Smr/Location.php b/src/lib/Smr/Location.php index c0d8819c7..0bbf46a28 100644 --- a/src/lib/Smr/Location.php +++ b/src/lib/Smr/Location.php @@ -22,7 +22,7 @@ class Location { /** @var array>> */ protected static array $CACHE_SECTOR_LOCATIONS = []; - public const SQL = 'location_type_id = :location_type_id'; + public const string SQL = 'location_type_id = :location_type_id'; /** @var array{location_type_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/Lotto.php b/src/lib/Smr/Lotto.php index 950152a01..48ba69148 100644 --- a/src/lib/Smr/Lotto.php +++ b/src/lib/Smr/Lotto.php @@ -7,8 +7,8 @@ */ class Lotto { - public const TICKET_COST = 1000000; // cost of 1 ticket - public const WIN_FRAC = 0.9; // fraction of ticket sales returned to winner + public const int TICKET_COST = 1000000; // cost of 1 ticket + public const float WIN_FRAC = 0.9; // fraction of ticket sales returned to winner public static function checkForLottoWinner(int $gameID): void { diff --git a/src/lib/Smr/Planet.php b/src/lib/Smr/Planet.php index b985c74d7..242a093af 100644 --- a/src/lib/Smr/Planet.php +++ b/src/lib/Smr/Planet.php @@ -19,13 +19,13 @@ class Planet { /** @var array> */ protected static array $CACHE_PLANETS = []; - public const DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE = 100; - protected const CHANCE_TO_DOWNGRADE = 15; // percent - protected const TIME_TO_CREDIT_BUST = 10800; // 3 hours - protected const TIME_ATTACK_NEWS_COOLDOWN = 3600; // 1 hour - public const MAX_STOCKPILE = 600; + public const int DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE = 100; + protected const int CHANCE_TO_DOWNGRADE = 15; // percent + protected const int TIME_TO_CREDIT_BUST = 10800; // 3 hours + protected const int TIME_ATTACK_NEWS_COOLDOWN = 3600; // 1 hour + public const int MAX_STOCKPILE = 600; - public const SQL = 'game_id = :game_id AND sector_id = :sector_id'; + public const string SQL = 'game_id = :game_id AND sector_id = :sector_id'; /** @var array{game_id: int, sector_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/PlanetStructureType.php b/src/lib/Smr/PlanetStructureType.php index 243c73fa2..b01d1ddea 100644 --- a/src/lib/Smr/PlanetStructureType.php +++ b/src/lib/Smr/PlanetStructureType.php @@ -18,7 +18,7 @@ public function __construct( /** * Trade goods required to build each type of structure. */ - protected const GOODS = [ + protected const array GOODS = [ PLANET_GENERATOR => [ GOODS_WOOD => 20, GOODS_ORE => 15, @@ -57,7 +57,7 @@ public function __construct( /** * Information to display about each structure type. */ - private const INFO = [ + private const array INFO = [ PLANET_GENERATOR => [ 'name' => 'Generator', 'image' => 'generator.png', diff --git a/src/lib/Smr/PlanetTypes/AridPlanet.php b/src/lib/Smr/PlanetTypes/AridPlanet.php index 43320ef83..50e663cb4 100644 --- a/src/lib/Smr/PlanetTypes/AridPlanet.php +++ b/src/lib/Smr/PlanetTypes/AridPlanet.php @@ -6,7 +6,7 @@ class AridPlanet extends PlanetType { - public const STRUCTURES = [ + public const array STRUCTURES = [ PLANET_GENERATOR => [ 'max_amount' => 25, 'base_time' => 10800, diff --git a/src/lib/Smr/PlanetTypes/DefenseWorld.php b/src/lib/Smr/PlanetTypes/DefenseWorld.php index 55bcee8dc..2bbb98609 100644 --- a/src/lib/Smr/PlanetTypes/DefenseWorld.php +++ b/src/lib/Smr/PlanetTypes/DefenseWorld.php @@ -6,7 +6,7 @@ class DefenseWorld extends PlanetType { - public const STRUCTURES = [ + public const array STRUCTURES = [ PLANET_GENERATOR => [ 'max_amount' => 800, 'base_time' => 2700, diff --git a/src/lib/Smr/PlanetTypes/DwarfPlanet.php b/src/lib/Smr/PlanetTypes/DwarfPlanet.php index cc9b4efa2..8f012854a 100644 --- a/src/lib/Smr/PlanetTypes/DwarfPlanet.php +++ b/src/lib/Smr/PlanetTypes/DwarfPlanet.php @@ -6,7 +6,7 @@ class DwarfPlanet extends PlanetType { - public const STRUCTURES = [ + public const array STRUCTURES = [ PLANET_GENERATOR => [ 'max_amount' => 10, 'base_time' => 10800, diff --git a/src/lib/Smr/PlanetTypes/PlanetType.php b/src/lib/Smr/PlanetTypes/PlanetType.php index 2688628b5..22499078e 100644 --- a/src/lib/Smr/PlanetTypes/PlanetType.php +++ b/src/lib/Smr/PlanetTypes/PlanetType.php @@ -10,7 +10,7 @@ */ abstract class PlanetType { - protected const MAX_LANDED_UNLIMITED = 0; + protected const int MAX_LANDED_UNLIMITED = 0; /** * Returns the properties of all the structures this planet type can build. @@ -41,7 +41,7 @@ abstract public function menuOptions(): array; * Associates the planet_type_id with the planet class. * These indices must not be changed! */ - public const PLANET_TYPES = [ + public const array PLANET_TYPES = [ 1 => TerranPlanet::class, 2 => AridPlanet::class, 3 => DwarfPlanet::class, diff --git a/src/lib/Smr/PlanetTypes/ProtoPlanet.php b/src/lib/Smr/PlanetTypes/ProtoPlanet.php index 346bfa0fa..e4ecbbbcc 100644 --- a/src/lib/Smr/PlanetTypes/ProtoPlanet.php +++ b/src/lib/Smr/PlanetTypes/ProtoPlanet.php @@ -6,7 +6,7 @@ class ProtoPlanet extends PlanetType { - public const STRUCTURES = [ + public const array STRUCTURES = [ PLANET_GENERATOR => [ 'max_amount' => 5, 'base_time' => 10800, diff --git a/src/lib/Smr/PlanetTypes/TerranPlanet.php b/src/lib/Smr/PlanetTypes/TerranPlanet.php index 3e69daee2..461da919e 100644 --- a/src/lib/Smr/PlanetTypes/TerranPlanet.php +++ b/src/lib/Smr/PlanetTypes/TerranPlanet.php @@ -6,7 +6,7 @@ class TerranPlanet extends PlanetType { - public const STRUCTURES = [ + public const array STRUCTURES = [ PLANET_GENERATOR => [ 'max_amount' => 25, 'base_time' => 10800, diff --git a/src/lib/Smr/Port.php b/src/lib/Smr/Port.php index 76b3d46a5..443afd7d9 100644 --- a/src/lib/Smr/Port.php +++ b/src/lib/Smr/Port.php @@ -23,31 +23,31 @@ class Port { /** @var array>> */ protected static array $CACHE_CACHED_PORTS = []; - public const DAMAGE_NEEDED_FOR_ALIGNMENT_CHANGE = 300; // single player - protected const DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE = 325; // all attackers - protected const CHANCE_TO_DOWNGRADE = 1; - protected const TIME_FEDS_STAY = 1800; - protected const MAX_FEDS_BONUS = 4000; - protected const BASE_CDS = 725; - protected const CDS_PER_LEVEL = 100; - protected const CDS_PER_TEN_MIL_CREDITS = 25; - protected const BASE_DEFENCES = 500; - protected const DEFENCES_PER_LEVEL = 700; - protected const DEFENCES_PER_TEN_MIL_CREDITS = 250; - protected const BASE_REFRESH_PER_HOUR = [ + public const int DAMAGE_NEEDED_FOR_ALIGNMENT_CHANGE = 300; // single player + protected const int DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE = 325; // all attackers + protected const int CHANCE_TO_DOWNGRADE = 1; + protected const int TIME_FEDS_STAY = 1800; + protected const int MAX_FEDS_BONUS = 4000; + protected const int BASE_CDS = 725; + protected const int CDS_PER_LEVEL = 100; + protected const int CDS_PER_TEN_MIL_CREDITS = 25; + protected const int BASE_DEFENCES = 500; + protected const int DEFENCES_PER_LEVEL = 700; + protected const int DEFENCES_PER_TEN_MIL_CREDITS = 250; + protected const array BASE_REFRESH_PER_HOUR = [ '1' => 150, '2' => 110, '3' => 70, ]; - protected const REFRESH_PER_GOOD = .9; - protected const TIME_TO_CREDIT_RAID = 10800; // 3 hours - protected const GOODS_TRADED_MONEY_MULTIPLIER = 50; - protected const BASE_PAYOUT = 0.85; // fraction of credits for looting - public const RAZE_PAYOUT = 0.75; // fraction of base payout for razing - public const CLAIM_PAYOUT = 0.5; // fraction of base payout for claiming - public const KILLER_RELATIONS_LOSS = 45; // relations lost by killer in PR - - public const SQL = 'sector_id = :sector_id AND game_id = :game_id'; + protected const float REFRESH_PER_GOOD = .9; + protected const int TIME_TO_CREDIT_RAID = 10800; // 3 hours + protected const int GOODS_TRADED_MONEY_MULTIPLIER = 50; + protected const float BASE_PAYOUT = 0.85; // fraction of credits for looting + public const float RAZE_PAYOUT = 0.75; // fraction of base payout for razing + public const float CLAIM_PAYOUT = 0.5; // fraction of base payout for claiming + public const int KILLER_RELATIONS_LOSS = 45; // relations lost by killer in PR + + public const string SQL = 'sector_id = :sector_id AND game_id = :game_id'; /** @var array{sector_id: int, game_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/Race.php b/src/lib/Smr/Race.php index cc7584fa4..c2bb28090 100644 --- a/src/lib/Smr/Race.php +++ b/src/lib/Smr/Race.php @@ -7,7 +7,7 @@ */ class Race { - private const RACE_NAMES = [ + private const array RACE_NAMES = [ RACE_NEUTRAL => 'Neutral', RACE_ALSKANT => 'Alskant', RACE_CREONTI => 'Creonti', diff --git a/src/lib/Smr/RaceDetails.php b/src/lib/Smr/RaceDetails.php index afa3950cc..bead95baa 100644 --- a/src/lib/Smr/RaceDetails.php +++ b/src/lib/Smr/RaceDetails.php @@ -7,7 +7,7 @@ */ class RaceDetails { - private const SHORT_DESCRIPTION = [ + private const array SHORT_DESCRIPTION = [ RACE_ALSKANT => 'Diverse assortment of hardware, but no dedicated warship. Trade bonuses with all races.', RACE_CREONTI => 'Bulky ships reinforced with heavy armour plating and lots of firepower.', RACE_HUMAN => 'Jump drive technology which enables fast inter-galactic movement.', @@ -18,7 +18,7 @@ class RaceDetails { RACE_NIJARIN => 'Strong weapons and Drone Communication Scrambler technology offsets lower defenses.', ]; - private const LONG_DESCRIPTION = [ + private const array LONG_DESCRIPTION = [ RACE_ALSKANT => 'This race of tall, thin humanoids has just recently (in the last 100 years) discovered interstellar travel. However, their charisma and enterprising nature has allowed them to trade for much of the advanced technology the other races had already developed. Alskants are generally peaceful, and have designed their ships for commerce rather than war. Since they pose no military threat, they tend to have relatively good relationships with the other races. They continue to seek the knowledge of the cosmos, and to explore to the edges of space.', RACE_CREONTI => 'As the manufacturers of the most colossal warships known to history, the Creonti are perceived as formidable opponents in war. The fact that they stand over 11 feet tall does not soften their brutish reputation. While a focus on war-waging has long served the Creonti in their pursuit of resources, it has also severely stunted their culture in almost every other aspect. Compared to the other races, the Creonti lack advanced technology and only have a rudimentary economy. Painfully aware of this weakness, the Creonti are now attempting to leverage peace for assistance with trade and economic development.', RACE_HUMAN => 'After discovering Jump Drive technology, the Human race underwent a space exploration renaissance marked by many noble and altruistic deeds, including an attempt to unify all of the races through the creation of The Federation. Over time, the Human military grew in power and co-opted the Jump Drive for the purposes of conquest and colonization, weakening the Federation and dissolving any hope for true unification. Today, the Humans continue down this hegemonic path, fighting in wars over resources, and using the crumbling Federation to legitimize their political interests.', diff --git a/src/lib/Smr/Routes/RouteGenerator.php b/src/lib/Smr/Routes/RouteGenerator.php index b524e49a9..5e999139e 100644 --- a/src/lib/Smr/Routes/RouteGenerator.php +++ b/src/lib/Smr/Routes/RouteGenerator.php @@ -6,8 +6,8 @@ class RouteGenerator { - public const EXP_ROUTE = 0; - public const MONEY_ROUTE = 1; + public const int EXP_ROUTE = 0; + public const int MONEY_ROUTE = 1; /** @var array> */ private static array $expRoutes; /** @var array> */ diff --git a/src/lib/Smr/Sector.php b/src/lib/Smr/Sector.php index 7e07fd75d..9b1c7bd6b 100644 --- a/src/lib/Smr/Sector.php +++ b/src/lib/Smr/Sector.php @@ -16,7 +16,7 @@ class Sector { /** @var array>> */ protected static array $CACHE_LOCATION_SECTORS = []; - public const SQL = 'game_id = :game_id AND sector_id = :sector_id'; + public const string SQL = 'game_id = :game_id AND sector_id = :sector_id'; /** @var array{game_id: int, sector_id: int} */ public readonly array $SQLID; @@ -34,7 +34,7 @@ class Sector { /** * Maps the Sector link direction names to database columns. */ - protected const LINK_DIR_MAPPING = [ + protected const array LINK_DIR_MAPPING = [ 'Up' => 'link_up', 'Down' => 'link_down', 'Left' => 'link_left', diff --git a/src/lib/Smr/SectorLock.php b/src/lib/Smr/SectorLock.php index 2496ed369..3e17e1ef3 100644 --- a/src/lib/Smr/SectorLock.php +++ b/src/lib/Smr/SectorLock.php @@ -26,13 +26,13 @@ class SectorLock { * The max time (in seconds) for a lock to be active before being * considered stale. */ - private const LOCK_DURATION = 10; + private const int LOCK_DURATION = 10; /** * The max time (in seconds) to retry acquiring locks before giving up. * Should be less than LOCK_DURATION. */ - private const RETRY_DURATION = 5; + private const int RETRY_DURATION = 5; private ?int $lockID = null; private bool $failed = false; diff --git a/src/lib/Smr/Session.php b/src/lib/Smr/Session.php index 0cff32b7c..c8a640626 100644 --- a/src/lib/Smr/Session.php +++ b/src/lib/Smr/Session.php @@ -18,9 +18,9 @@ class Session { - private const TIME_BEFORE_EXPIRY = 172800; // 2 days + private const int TIME_BEFORE_EXPIRY = 172800; // 2 days - private const URL_LOAD_DELAY = [ + private const array URL_LOAD_DELAY = [ HardwareConfigure::class => .4, ForcesDrop::class => .4, ForcesDropProcessor::class => .5, diff --git a/src/lib/Smr/Ship.php b/src/lib/Smr/Ship.php index f46801e2b..26a77da81 100644 --- a/src/lib/Smr/Ship.php +++ b/src/lib/Smr/Ship.php @@ -13,7 +13,7 @@ class Ship extends AbstractShip { /** @var array> */ protected static array $CACHE_SHIPS = []; - public const SQL = 'account_id = :account_id AND game_id = :game_id'; + public const string SQL = 'account_id = :account_id AND game_id = :game_id'; /** @var array{account_id: int, game_id: int} */ public readonly array $SQLID; diff --git a/src/lib/Smr/TransactionType.php b/src/lib/Smr/TransactionType.php index 7086af586..895416fb9 100644 --- a/src/lib/Smr/TransactionType.php +++ b/src/lib/Smr/TransactionType.php @@ -21,6 +21,6 @@ public function opposite(): self { // This is *NOT* one of the enum cases, but it logistically makes sense to // include it in the same namespace as the first-class transactions. - public const STEAL = 'Steal'; + public const string STEAL = 'Steal'; } diff --git a/src/lib/Smr/Treaty.php b/src/lib/Smr/Treaty.php index e89f9f63d..28ebd6a31 100644 --- a/src/lib/Smr/Treaty.php +++ b/src/lib/Smr/Treaty.php @@ -8,7 +8,7 @@ class Treaty { * For each treaty type (as given by the columns in the alliance_treaties * database table), provides an array with a display title and description. */ - public const TYPES = [ + public const array TYPES = [ 'trader_assist' => [ 'Assist - Trader Attacks', 'Assist your ally in attacking traders.', diff --git a/src/lib/Smr/UserRanking.php b/src/lib/Smr/UserRanking.php index 68fec6c2c..38ab1da3e 100644 --- a/src/lib/Smr/UserRanking.php +++ b/src/lib/Smr/UserRanking.php @@ -18,11 +18,11 @@ enum UserRanking: int { case Master = 8; case Grandmaster = 9; - public const MIN_RANK = 1; - public const MAX_RANK = 9; + public const int MIN_RANK = 1; + public const int MAX_RANK = 9; - public const SCORE_POW = .3; - public const SCORE_POW_RANK_INCREMENT = 5.2; + public const float SCORE_POW = .3; + public const float SCORE_POW_RANK_INCREMENT = 5.2; /** * Given a score, return the associated rank diff --git a/src/lib/Smr/VoteLink.php b/src/lib/Smr/VoteLink.php index bfbbb0140..d0917969f 100644 --- a/src/lib/Smr/VoteLink.php +++ b/src/lib/Smr/VoteLink.php @@ -11,7 +11,7 @@ */ class VoteLink { - public const TIME_BETWEEN_VOTING = 84600; // 23.5 hours + public const int TIME_BETWEEN_VOTING = 84600; // 23.5 hours /** @var ?array */ private static ?array $CACHE_TIMEOUTS = null; diff --git a/src/lib/Smr/WeightedRandom.php b/src/lib/Smr/WeightedRandom.php index 42704bd23..2353b5b0c 100644 --- a/src/lib/Smr/WeightedRandom.php +++ b/src/lib/Smr/WeightedRandom.php @@ -17,7 +17,7 @@ class WeightedRandom { /** @var array>>> */ protected static array $CACHE_RANDOMS = []; - protected const WEIGHTING_CHANGE = 50; // as a percent + protected const int WEIGHTING_CHANGE = 50; // as a percent protected float $weighting; From 710d713e7bf8395d1872daeee3ff09ee2f8ca472 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 22:54:26 -0800 Subject: [PATCH 09/10] rector: add ThisCallOnStaticMethodToStaticCallRector Static methods do not need to be called on a class instance, and so they should use the static context, even if a class instance is present. --- rector.php | 2 ++ src/lib/Smr/Chess/Board.php | 4 ++-- src/lib/Smr/SocialLogin/Facebook.php | 2 +- src/lib/Smr/SocialLogin/Google.php | 2 +- src/lib/Smr/SocialLogin/SocialLogin.php | 2 +- src/lib/Smr/SocialLogin/Twitter.php | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) diff --git a/rector.php b/rector.php index 205328ac9..749710bd5 100644 --- a/rector.php +++ b/rector.php @@ -2,6 +2,7 @@ use Rector\Config\RectorConfig; use Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector; +use Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector; use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector; use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; @@ -30,6 +31,7 @@ JsonThrowOnErrorRector::class, NullCoalescingOperatorRector::class, ReadOnlyPropertyRector::class, + ThisCallOnStaticMethodToStaticCallRector::class, ]) ->withSets([ PHPUnitSetList::PHPUNIT_100, diff --git a/src/lib/Smr/Chess/Board.php b/src/lib/Smr/Chess/Board.php index 12946fac6..a27aeedcf 100644 --- a/src/lib/Smr/Chess/Board.php +++ b/src/lib/Smr/Chess/Board.php @@ -228,10 +228,10 @@ public function clearSquare(int $x, int $y): void { * @return array{Castling: ?Castling, PieceTaken: ?ChessPiece, EnPassant: bool, PawnPromotion: bool} */ public function movePiece(int $x, int $y, int $toX, int $toY, int $pawnPromotionPiece = ChessPiece::QUEEN): array { - if (!$this->isValidCoord($x, $y)) { + if (!static::isValidCoord($x, $y)) { throw new Exception('Invalid from coordinates, x=' . $x . ', y=' . $y); } - if (!$this->isValidCoord($toX, $toY)) { + if (!static::isValidCoord($toX, $toY)) { throw new Exception('Invalid to coordinates, x=' . $toX . ', y=' . $toY); } $piece = $this->getPiece($x, $y); diff --git a/src/lib/Smr/SocialLogin/Facebook.php b/src/lib/Smr/SocialLogin/Facebook.php index 7f241c9f6..08711a6bc 100644 --- a/src/lib/Smr/SocialLogin/Facebook.php +++ b/src/lib/Smr/SocialLogin/Facebook.php @@ -48,7 +48,7 @@ public function login(): SocialIdentity { ['code' => Request::get('code')], ); $userInfo = $provider->getResourceOwner($accessToken); - return new SocialIdentity($userInfo->getId(), $userInfo->getEmail(), $this->getLoginType()); + return new SocialIdentity($userInfo->getId(), $userInfo->getEmail(), static::getLoginType()); } } diff --git a/src/lib/Smr/SocialLogin/Google.php b/src/lib/Smr/SocialLogin/Google.php index 1e08d4d91..de23445fb 100644 --- a/src/lib/Smr/SocialLogin/Google.php +++ b/src/lib/Smr/SocialLogin/Google.php @@ -48,7 +48,7 @@ public function login(): SocialIdentity { ); /** @var \League\OAuth2\Client\Provider\GoogleUser $userInfo */ $userInfo = $provider->getResourceOwner($accessToken); - return new SocialIdentity($userInfo->getId(), $userInfo->getEmail(), $this->getLoginType()); + return new SocialIdentity($userInfo->getId(), $userInfo->getEmail(), static::getLoginType()); } } diff --git a/src/lib/Smr/SocialLogin/SocialLogin.php b/src/lib/Smr/SocialLogin/SocialLogin.php index f19cfff8c..8cbefa011 100644 --- a/src/lib/Smr/SocialLogin/SocialLogin.php +++ b/src/lib/Smr/SocialLogin/SocialLogin.php @@ -41,7 +41,7 @@ public function __construct() { * after authentication. */ protected function getRedirectUrl(): string { - return URL . '/login_processing.php?loginType=' . $this->getLoginType(); + return URL . '/login_processing.php?loginType=' . static::getLoginType(); } /** diff --git a/src/lib/Smr/SocialLogin/Twitter.php b/src/lib/Smr/SocialLogin/Twitter.php index 6ca5ef19d..b1f1932e7 100644 --- a/src/lib/Smr/SocialLogin/Twitter.php +++ b/src/lib/Smr/SocialLogin/Twitter.php @@ -52,7 +52,7 @@ public function login(): SocialIdentity { if ($auth->getLastHttpCode() !== 200) { throw new UserError('Connection failed, please try again.'); } - return new SocialIdentity($userInfo->id_str, $userInfo->email, $this->getLoginType()); + return new SocialIdentity($userInfo->id_str, $userInfo->email, static::getLoginType()); } } From 223fd3506934390414f299073f769cb6328ebfd3 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Tue, 24 Dec 2024 23:19:01 -0800 Subject: [PATCH 10/10] rector: add CombinedAssignRector This makes the expression safer since we only write out the variable name once. --- rector.php | 3 +++ src/lib/Default/smr.inc.php | 6 +++--- .../includes/ForceTraderTeamCombatResults.inc.php | 12 ++++++------ .../Default/includes/ForcesCombatResults.inc.php | 6 +++--- .../Default/includes/PlanetCombatResults.inc.php | 12 ++++++------ .../includes/PlanetTraderTeamCombatResults.inc.php | 12 ++++++------ .../Default/includes/PortCombatResults.inc.php | 12 ++++++------ .../includes/PortTraderTeamCombatResults.inc.php | 12 ++++++------ .../Default/includes/TraderTeamCombatResults.inc.php | 12 ++++++------ 9 files changed, 45 insertions(+), 42 deletions(-) diff --git a/rector.php b/rector.php index 749710bd5..833eb45ab 100644 --- a/rector.php +++ b/rector.php @@ -1,5 +1,6 @@ withPaths([ __DIR__ . '/test', @@ -25,6 +27,7 @@ AddOverrideAttributeToOverriddenMethodsRector::class, ClassOnObjectRector::class, ClassPropertyAssignToConstructorPromotionRector::class, + CombinedAssignRector::class, DirNameFileConstantToDirConstantRector::class, FirstClassCallableRector::class, IfIssetToCoalescingRector::class, diff --git a/src/lib/Default/smr.inc.php b/src/lib/Default/smr.inc.php index afdb15311..235dad4e3 100644 --- a/src/lib/Default/smr.inc.php +++ b/src/lib/Default/smr.inc.php @@ -729,15 +729,15 @@ function format_time(int $seconds, bool $short = false): string { $weeks = 0; if ($minutes >= 60) { $hours = floor($minutes / 60); - $minutes = $minutes % 60; + $minutes %= 60; } if ($hours >= 24) { $days = floor($hours / 24); - $hours = $hours % 24; + $hours %= 24; } if ($days >= 7) { $weeks = floor($days / 7); - $days = $days % 7; + $days %= 7; } $times = [ 'week' => $weeks, diff --git a/src/templates/Default/engine/Default/includes/ForceTraderTeamCombatResults.inc.php b/src/templates/Default/engine/Default/includes/ForceTraderTeamCombatResults.inc.php index ade4e4d89..95ec2f3b5 100644 --- a/src/templates/Default/engine/Default/includes/ForceTraderTeamCombatResults.inc.php +++ b/src/templates/Default/engine/Default/includes/ForceTraderTeamCombatResults.inc.php @@ -38,9 +38,9 @@ } else { ?> destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumSDs'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['NumMines'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumSDs'] > 0) { $DamageTypes += 1; } if ($ActualDamage['NumMines'] > 0) { ?> mines $WeaponDamage['Kamikaze']) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumSDs'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['NumMines'] > $WeaponDamage['Kamikaze']) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumSDs'] > 0) { $DamageTypes += 1; } if ($WeaponDamage['Kamikaze'] === 0) { ?> destroying destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } echo $CombatPlanet->getCombatName(); if (!isset($WeaponDamage['Launched'])) { diff --git a/src/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc.php b/src/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc.php index 115f60faf..2b6fcac29 100644 --- a/src/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc.php +++ b/src/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc.php @@ -57,9 +57,9 @@ } else { ?> destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } echo $ShootingPlayer->getDisplayName(); if (!isset($WeaponDamage['Launched'])) { diff --git a/src/templates/Default/engine/Default/includes/PortCombatResults.inc.php b/src/templates/Default/engine/Default/includes/PortCombatResults.inc.php index 732108b8c..82eb72e4d 100644 --- a/src/templates/Default/engine/Default/includes/PortCombatResults.inc.php +++ b/src/templates/Default/engine/Default/includes/PortCombatResults.inc.php @@ -51,9 +51,9 @@ } else { ?> destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields destroying 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } if ($ActualDamage['Shield'] > 0) { ?> shields 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['NumCDs'] > 0) { $DamageTypes = $DamageTypes + 1; } - if ($ActualDamage['Armour'] > 0) { $DamageTypes = $DamageTypes + 1; } + if ($ActualDamage['Shield'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['NumCDs'] > 0) { $DamageTypes += 1; } + if ($ActualDamage['Armour'] > 0) { $DamageTypes += 1; } echo $ShootingPlayer->getDisplayName(); if (!isset($WeaponDamage['Launched'])) {