From a9de9d8123959a71c46a4d8004ba52bccdcf35f4 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Sun, 8 May 2022 17:24:03 -0700 Subject: [PATCH] Add readonly keyword to write-once class properties Related to #1183. The readonly keyword helps us to enforce that fundamental properties of a class do not change (such as the ID of a player, sector, account, etc.). Once they are defined, they cannot be defined again. Readonly properties that are passed into the constructor should also use constructor promotion to further simplify their definition. Additional notes: * For attributes that are objects, readonly does _not_ mean constant. That is, while the object cannot be overwritten, its internal state can still be modified. * For readonly properties that have an associated getter function, we can (and should) remove the getter and simply make the property public. This is logically equivalent, but with less overhead. We do not do this here because it is a massive change. --- src/lib/Default/AbstractSmrAccount.php | 12 ++++------ src/lib/Default/AbstractSmrLocation.php | 13 +++++----- src/lib/Default/AbstractSmrPlayer.php | 12 +++++----- src/lib/Default/AbstractSmrPort.php | 13 +++++----- src/lib/Default/SmrAlliance.php | 12 ++++------ src/lib/Default/SmrEnhancedWeaponEvent.php | 21 ++++++++-------- src/lib/Default/SmrForce.php | 15 ++++++------ src/lib/Default/SmrGalaxy.php | 15 ++++++------ src/lib/Default/SmrGame.php | 9 ++++--- src/lib/Default/SmrInvitation.php | 12 +++++----- src/lib/Default/SmrPlanet.php | 12 +++++----- src/lib/Default/SmrPlanetStructureType.php | 4 ++-- src/lib/Default/SmrSector.php | 14 +++++------ src/lib/Default/SmrShip.php | 2 +- src/lib/Default/SmrShipType.php | 28 ++++++++++++---------- src/lib/Default/SmrWeapon.php | 9 +++---- src/lib/Default/SmrWeaponType.php | 21 ++++++++-------- src/lib/Default/WeightedRandom.php | 16 +++++-------- src/lib/Smr/Blackjack/Card.php | 8 +++---- src/lib/Smr/Blackjack/Deck.php | 2 +- src/lib/Smr/Chess/ChessGame.php | 12 ++++------ src/lib/Smr/Chess/ChessPiece.php | 2 +- src/lib/Smr/Container/DiContainer.php | 2 +- src/lib/Smr/DatabaseRecord.php | 2 +- src/lib/Smr/DatabaseResult.php | 2 +- src/lib/Smr/Epoch.php | 4 ++-- src/lib/Smr/Routes/MultiplePortRoute.php | 4 ++-- src/lib/Smr/Routes/OneWayRoute.php | 16 ++++++------- src/lib/Smr/VoteSite.php | 6 ++--- 29 files changed, 146 insertions(+), 154 deletions(-) diff --git a/src/lib/Default/AbstractSmrAccount.php b/src/lib/Default/AbstractSmrAccount.php index 459231bff..9970e4eec 100644 --- a/src/lib/Default/AbstractSmrAccount.php +++ b/src/lib/Default/AbstractSmrAccount.php @@ -33,8 +33,8 @@ abstract class AbstractSmrAccount { ]; protected Smr\Database $db; + protected readonly string $SQL; - protected int $account_id; protected string $login; protected string $passwordHash; protected string $email; @@ -74,7 +74,6 @@ abstract class AbstractSmrAccount { protected string $friendlyColour; protected string $neutralColour; protected string $enemyColour; - protected string $SQL; protected bool $npc; @@ -202,14 +201,13 @@ public static function getUserScoreCaseStatement(Smr\Database $db): array { return ['CASE' => $case, 'IN' => implode(',', $userRankingTypes)]; } - protected function __construct(int $accountID) { + protected function __construct(protected readonly int $accountID) { $this->db = Smr\Database::getInstance(); $this->SQL = 'account_id = ' . $this->db->escapeNumber($accountID); $dbResult = $this->db->read('SELECT * FROM account WHERE ' . $this->SQL . ' LIMIT 1'); if ($dbResult->hasRecord()) { $dbRecord = $dbResult->record(); - $this->account_id = $dbRecord->getInt('account_id'); $this->login = $dbRecord->getField('login'); $this->passwordHash = $dbRecord->getField('password'); @@ -353,7 +351,7 @@ public function updateIP(): void { // save...first make sure there isn't one for these keys (someone could double click and get error) $this->db->replace('account_has_ip', [ - 'account_id' => $this->db->escapeNumber($this->account_id), + 'account_id' => $this->db->escapeNumber($this->accountID), 'time' => $this->db->escapeNumber(Smr\Epoch::time()), 'ip' => $this->db->escapeString($curr_ip), 'host' => $this->db->escapeString($host), @@ -510,7 +508,7 @@ public function getReferrer(): SmrAccount { public function log(int $log_type_id, string $msg, int $sector_id = 0): void { if ($this->isLoggingEnabled()) { $this->db->insert('account_has_logs', [ - 'account_id' => $this->db->escapeNumber($this->account_id), + 'account_id' => $this->db->escapeNumber($this->accountID), 'microtime' => $this->db->escapeMicrotime(Smr\Epoch::microtime()), 'log_type_id' => $this->db->escapeNumber($log_type_id), 'message' => $this->db->escapeString($msg), @@ -661,7 +659,7 @@ public function equals(self $other): bool { } public function getAccountID(): int { - return $this->account_id; + return $this->accountID; } /** diff --git a/src/lib/Default/AbstractSmrLocation.php b/src/lib/Default/AbstractSmrLocation.php index 19821526c..47d8ad1d2 100644 --- a/src/lib/Default/AbstractSmrLocation.php +++ b/src/lib/Default/AbstractSmrLocation.php @@ -7,9 +7,8 @@ class AbstractSmrLocation { protected static array $CACHE_SECTOR_LOCATIONS = []; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $typeID; protected string $name; protected ?string $processor; protected string $image; @@ -114,9 +113,12 @@ public static function getLocation(int $locationTypeID, bool $forceUpdate = fals return self::$CACHE_LOCATIONS[$locationTypeID]; } - protected function __construct(int $locationTypeID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $typeID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); - $this->SQL = 'location_type_id = ' . $this->db->escapeNumber($locationTypeID); + $this->SQL = 'location_type_id = ' . $this->db->escapeNumber($typeID); if ($dbRecord === null) { $dbResult = $this->db->read('SELECT * FROM location_type WHERE ' . $this->SQL . ' LIMIT 1'); @@ -127,12 +129,11 @@ protected function __construct(int $locationTypeID, Smr\DatabaseRecord $dbRecord $locationExists = $dbRecord !== null; if ($locationExists) { - $this->typeID = $dbRecord->getInt('location_type_id'); $this->name = $dbRecord->getField('location_name'); $this->processor = $dbRecord->getField('location_processor'); $this->image = $dbRecord->getField('location_image'); } else { - throw new Exception('Cannot find location: ' . $locationTypeID); + throw new Exception('Cannot find location: ' . $typeID); } } diff --git a/src/lib/Default/AbstractSmrPlayer.php b/src/lib/Default/AbstractSmrPlayer.php index a61797f97..2a488aef9 100644 --- a/src/lib/Default/AbstractSmrPlayer.php +++ b/src/lib/Default/AbstractSmrPlayer.php @@ -19,10 +19,8 @@ abstract class AbstractSmrPlayer { protected static array $CACHE_PLAYERS = []; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $accountID; - protected int $gameID; protected string $playerName; protected int $playerID; protected int $sectorID; @@ -199,7 +197,11 @@ public static function getPlayerByPlayerName(string $playerName, int $gameID, bo throw new Smr\Exceptions\PlayerNotFound('Player Name not found.'); } - protected function __construct(int $gameID, int $accountID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $accountID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); $this->SQL = 'account_id = ' . $this->db->escapeNumber($accountID) . ' AND game_id = ' . $this->db->escapeNumber($gameID); @@ -213,8 +215,6 @@ protected function __construct(int $gameID, int $accountID, Smr\DatabaseRecord $ throw new Smr\Exceptions\PlayerNotFound('Invalid accountID: ' . $accountID . ' OR gameID: ' . $gameID); } - $this->accountID = $accountID; - $this->gameID = $gameID; $this->playerName = $dbRecord->getField('player_name'); $this->playerID = $dbRecord->getInt('player_id'); $this->sectorID = $dbRecord->getInt('sector_id'); diff --git a/src/lib/Default/AbstractSmrPort.php b/src/lib/Default/AbstractSmrPort.php index 552fdbb92..afb986852 100644 --- a/src/lib/Default/AbstractSmrPort.php +++ b/src/lib/Default/AbstractSmrPort.php @@ -30,9 +30,8 @@ class AbstractSmrPort { public const RAZE_PAYOUT = 0.75; // fraction of base payout for razing protected Smr\Database $db; + protected readonly string $SQL; - protected int $gameID; - protected int $sectorID; protected int $shields; protected int $combatDrones; protected int $armour; @@ -53,8 +52,6 @@ class AbstractSmrPort { protected int $cachedTime; protected bool $cacheIsValid = true; - protected string $SQL; - protected bool $hasChanged = false; protected bool $isNew = false; @@ -119,7 +116,11 @@ public static function getBaseExperience(int $cargo, int $distance): float { return ($cargo / 13) * $distance; } - protected function __construct(int $gameID, int $sectorID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $sectorID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->cachedTime = Smr\Epoch::time(); $this->db = Smr\Database::getInstance(); $this->SQL = 'sector_id = ' . $this->db->escapeNumber($sectorID) . ' AND game_id = ' . $this->db->escapeNumber($gameID); @@ -132,8 +133,6 @@ protected function __construct(int $gameID, int $sectorID, Smr\DatabaseRecord $d } $this->isNew = $dbRecord === null; - $this->gameID = $gameID; - $this->sectorID = $sectorID; if (!$this->isNew) { $this->shields = $dbRecord->getInt('shields'); $this->combatDrones = $dbRecord->getInt('combat_drones'); diff --git a/src/lib/Default/SmrAlliance.php b/src/lib/Default/SmrAlliance.php index 7d010ddbc..8e99f9640 100644 --- a/src/lib/Default/SmrAlliance.php +++ b/src/lib/Default/SmrAlliance.php @@ -5,10 +5,8 @@ class SmrAlliance { protected static array $CACHE_ALLIANCES = []; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $gameID; - protected int $allianceID; protected string $allianceName; protected ?string $description; protected string $password; @@ -73,11 +71,11 @@ public static function getAllianceByName(string $name, int $gameID, bool $forceU throw new Smr\Exceptions\AllianceNotFound('Alliance name not found'); } - protected function __construct(int $allianceID, int $gameID) { + protected function __construct( + protected readonly int $allianceID, + protected readonly int $gameID + ) { $this->db = Smr\Database::getInstance(); - - $this->allianceID = $allianceID; - $this->gameID = $gameID; $this->SQL = 'alliance_id=' . $this->db->escapeNumber($allianceID) . ' AND game_id=' . $this->db->escapeNumber($gameID); if ($allianceID != 0) { diff --git a/src/lib/Default/SmrEnhancedWeaponEvent.php b/src/lib/Default/SmrEnhancedWeaponEvent.php index b1a68f91b..1ef26e640 100644 --- a/src/lib/Default/SmrEnhancedWeaponEvent.php +++ b/src/lib/Default/SmrEnhancedWeaponEvent.php @@ -8,11 +8,7 @@ class SmrEnhancedWeaponEvent { protected const GRACE_PERIOD = 3600; // 1 hour protected const DURATION = 21600; // 6 hours - protected int $gameID; - protected int $sectorID; - protected int $locationTypeID; - protected int $expires; - protected SmrWeapon $weapon; + protected readonly SmrWeapon $weapon; /** * Return all the valid events for the given location in a sector. @@ -114,12 +110,15 @@ private static function getEventFromDatabase(Smr\DatabaseRecord $dbRecord): self ); } - protected function __construct(int $gameID, int $weaponTypeID, int $locationTypeID, int $sectorID, int $expires, bool $bonusAccuracy, bool $bonusDamage) { - $this->gameID = $gameID; - $this->locationTypeID = $locationTypeID; - $this->sectorID = $sectorID; - $this->expires = $expires; - + protected function __construct( + protected readonly int $gameID, + protected readonly int $weaponTypeID, + protected readonly int $locationTypeID, + protected readonly int $sectorID, + protected readonly int $expires, + bool $bonusAccuracy, + bool $bonusDamage + ) { $this->weapon = SmrWeapon::getWeapon($weaponTypeID); $this->weapon->setBonusDamage($bonusDamage); $this->weapon->setBonusAccuracy($bonusAccuracy); diff --git a/src/lib/Default/SmrForce.php b/src/lib/Default/SmrForce.php index 9c216ef1b..57ee9f1a2 100644 --- a/src/lib/Default/SmrForce.php +++ b/src/lib/Default/SmrForce.php @@ -18,11 +18,8 @@ class SmrForce { public const MAX_SDS = 5; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $ownerID; - protected int $sectorID; - protected int $gameID; protected int $combatDrones = 0; protected int $scoutDrones = 0; protected int $mines = 0; @@ -103,7 +100,12 @@ public static function tidyUpForces(SmrGalaxy $galaxyToTidy): void { } } - protected function __construct(int $gameID, int $sectorID, int $ownerID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $sectorID, + protected readonly int $ownerID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); $this->SQL = 'game_id = ' . $this->db->escapeNumber($gameID) . ' AND sector_id = ' . $this->db->escapeNumber($sectorID) . ' @@ -117,9 +119,6 @@ protected function __construct(int $gameID, int $sectorID, int $ownerID, Smr\Dat } $this->isNew = $dbRecord === null; - $this->gameID = $gameID; - $this->ownerID = $ownerID; - $this->sectorID = $sectorID; if (!$this->isNew) { $this->combatDrones = $dbRecord->getInt('combat_drones'); $this->scoutDrones = $dbRecord->getInt('scout_drones'); diff --git a/src/lib/Default/SmrGalaxy.php b/src/lib/Default/SmrGalaxy.php index 8d2e117f3..0338d0d8b 100644 --- a/src/lib/Default/SmrGalaxy.php +++ b/src/lib/Default/SmrGalaxy.php @@ -10,10 +10,8 @@ class SmrGalaxy { public const TYPES = [self::TYPE_RACIAL, self::TYPE_NEUTRAL, self::TYPE_PLANET]; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $gameID; - protected int $galaxyID; protected string $name; protected int $width; protected int $height; @@ -23,7 +21,7 @@ class SmrGalaxy { protected int $startSector; protected bool $hasChanged = false; - protected bool $isNew = false; + protected bool $isNew; public static function clearCache(): void { self::$CACHE_GALAXIES = []; @@ -68,7 +66,12 @@ public static function createGalaxy(int $gameID, int $galaxyID): self { return self::$CACHE_GALAXIES[$gameID][$galaxyID]; } - protected function __construct(int $gameID, int $galaxyID, bool $create = false, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $galaxyID, + bool $create = false, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); $this->SQL = 'game_id = ' . $this->db->escapeNumber($gameID) . ' AND galaxy_id = ' . $this->db->escapeNumber($galaxyID); @@ -81,8 +84,6 @@ protected function __construct(int $gameID, int $galaxyID, bool $create = false, } $this->isNew = $dbRecord === null; - $this->gameID = $gameID; - $this->galaxyID = $galaxyID; if (!$this->isNew) { $this->name = $dbRecord->getField('galaxy_name'); $this->width = $dbRecord->getInt('width'); diff --git a/src/lib/Default/SmrGame.php b/src/lib/Default/SmrGame.php index 95ec108fe..fffdefb79 100644 --- a/src/lib/Default/SmrGame.php +++ b/src/lib/Default/SmrGame.php @@ -6,7 +6,6 @@ class SmrGame { protected Smr\Database $db; - protected int $gameID; protected string $name; protected string $description; protected int $joinTime; @@ -81,13 +80,15 @@ public static function createGame(int $gameID): self { return self::$CACHE_GAMES[$gameID]; } - protected function __construct(int $gameID, bool $create = false) { + protected function __construct( + protected readonly int $gameID, + bool $create = false + ) { $this->db = Smr\Database::getInstance(); $dbResult = $this->db->read('SELECT * FROM game WHERE game_id = ' . $this->db->escapeNumber($gameID) . ' LIMIT 1'); if ($dbResult->hasRecord()) { $dbRecord = $dbResult->record(); - $this->gameID = $dbRecord->getInt('game_id'); $this->name = $dbRecord->getField('game_name'); $this->description = $dbRecord->getField('game_description'); $this->joinTime = $dbRecord->getInt('join_time'); @@ -105,9 +106,7 @@ protected function __construct(int $gameID, bool $create = false) { $this->allianceMaxVets = $dbRecord->getInt('alliance_max_vets'); $this->startingCredits = $dbRecord->getInt('starting_credits'); } elseif ($create === true) { - $this->gameID = $gameID; $this->isNew = true; - return; } else { throw new Smr\Exceptions\GameNotFound('No such game: ' . $gameID); } diff --git a/src/lib/Default/SmrInvitation.php b/src/lib/Default/SmrInvitation.php index c9a41370d..1a39fb102 100644 --- a/src/lib/Default/SmrInvitation.php +++ b/src/lib/Default/SmrInvitation.php @@ -5,12 +5,12 @@ */ class SmrInvitation { - private int $allianceID; - private int $gameID; - private int $receiverAccountID; - private int $senderAccountID; - private int $messageID; - private int $expires; + private readonly int $allianceID; + private readonly int $gameID; + private readonly int $receiverAccountID; + private readonly int $senderAccountID; + private readonly int $messageID; + private readonly int $expires; public static function send(int $allianceID, int $gameID, int $receiverAccountID, int $senderAccountID, int $messageID, int $expires): void { $db = Smr\Database::getInstance(); diff --git a/src/lib/Default/SmrPlanet.php b/src/lib/Default/SmrPlanet.php index 606fd73b2..56f7c2086 100644 --- a/src/lib/Default/SmrPlanet.php +++ b/src/lib/Default/SmrPlanet.php @@ -13,11 +13,9 @@ class SmrPlanet { public const MAX_STOCKPILE = 600; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; protected bool $exists; - protected int $sectorID; - protected int $gameID; protected string $planetName; protected int $ownerID; protected string $password; @@ -113,7 +111,11 @@ public static function removePlanet(int $gameID, int $sectorID): void { unset(self::$CACHE_PLANETS[$gameID][$sectorID]); } - protected function __construct(int $gameID, int $sectorID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $sectorID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); $this->SQL = 'game_id = ' . $this->db->escapeNumber($gameID) . ' AND sector_id = ' . $this->db->escapeNumber($sectorID); @@ -126,8 +128,6 @@ protected function __construct(int $gameID, int $sectorID, Smr\DatabaseRecord $d $this->exists = $dbRecord !== null; if ($this->exists) { - $this->gameID = $gameID; - $this->sectorID = $sectorID; $this->planetName = $dbRecord->getString('planet_name'); $this->ownerID = $dbRecord->getInt('owner_id'); $this->password = $dbRecord->getField('password'); diff --git a/src/lib/Default/SmrPlanetStructureType.php b/src/lib/Default/SmrPlanetStructureType.php index 3108c4d4e..aa679f870 100644 --- a/src/lib/Default/SmrPlanetStructureType.php +++ b/src/lib/Default/SmrPlanetStructureType.php @@ -6,8 +6,8 @@ class SmrPlanetStructureType { public function __construct( - private int $ID, - private array $planetTypeInfo, + private readonly int $ID, + private readonly array $planetTypeInfo, ) {} /** diff --git a/src/lib/Default/SmrSector.php b/src/lib/Default/SmrSector.php index bbd89712e..a9a456832 100644 --- a/src/lib/Default/SmrSector.php +++ b/src/lib/Default/SmrSector.php @@ -7,10 +7,8 @@ class SmrSector { protected static array $CACHE_LOCATION_SECTORS = []; protected Smr\Database $db; - protected string $SQL; + protected readonly string $SQL; - protected int $gameID; - protected int $sectorID; protected int $battles; protected int $galaxyID; protected array $visited = []; @@ -100,7 +98,12 @@ public static function createSector(int $gameID, int $sectorID): self { return self::$CACHE_SECTORS[$gameID][$sectorID]; } - protected function __construct(int $gameID, int $sectorID, bool $create = false, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $gameID, + protected readonly int $sectorID, + bool $create = false, + Smr\DatabaseRecord $dbRecord = null + ) { $this->db = Smr\Database::getInstance(); $this->SQL = 'game_id = ' . $this->db->escapeNumber($gameID) . ' AND sector_id = ' . $this->db->escapeNumber($sectorID); @@ -113,9 +116,6 @@ protected function __construct(int $gameID, int $sectorID, bool $create = false, } $sectorExists = $dbRecord !== null; - $this->gameID = $gameID; - $this->sectorID = $sectorID; - if ($sectorExists) { $this->galaxyID = $dbRecord->getInt('galaxy_id'); $this->battles = $dbRecord->getInt('battles'); diff --git a/src/lib/Default/SmrShip.php b/src/lib/Default/SmrShip.php index 927b594a1..5551a668d 100644 --- a/src/lib/Default/SmrShip.php +++ b/src/lib/Default/SmrShip.php @@ -8,7 +8,7 @@ class SmrShip extends AbstractSmrShip { protected static array $CACHE_SHIPS = []; - protected string $SQL; + protected readonly string $SQL; public static function clearCache(): void { self::$CACHE_SHIPS = []; diff --git a/src/lib/Default/SmrShipType.php b/src/lib/Default/SmrShipType.php index d5e4e3ec1..89832f7b0 100644 --- a/src/lib/Default/SmrShipType.php +++ b/src/lib/Default/SmrShipType.php @@ -9,18 +9,18 @@ class SmrShipType { private static array $CACHE_SHIP_TYPES = []; - private string $name; - private int $typeID; - private int $classID; - private int $hardpoints; - private int $speed; - private int $cost; - private int $restriction; - private int $levelNeeded; - - private int $maxPower = 0; - private array $maxHardware = []; - private int $baseManeuverability; + private readonly string $name; + private readonly int $typeID; + private readonly int $classID; + private readonly int $hardpoints; + private readonly int $speed; + private readonly int $cost; + private readonly int $restriction; + private readonly int $levelNeeded; + + private readonly int $maxPower; + private readonly array $maxHardware; + private readonly int $baseManeuverability; public static function clearCache(): void { self::$CACHE_SHIP_TYPES = []; @@ -83,10 +83,12 @@ protected function __construct(Smr\DatabaseRecord $dbRecord) { $dbResult = $db->read('SELECT hardware_type_id, max_amount FROM ship_type_support_hardware ' . 'WHERE ship_type_id = ' . $db->escapeNumber($this->typeID) . ' ORDER BY hardware_type_id'); + $maxHardware = []; foreach ($dbResult->records() as $dbRecord2) { // adding hardware to array - $this->maxHardware[$dbRecord2->getInt('hardware_type_id')] = $dbRecord2->getInt('max_amount'); + $maxHardware[$dbRecord2->getInt('hardware_type_id')] = $dbRecord2->getInt('max_amount'); } + $this->maxHardware = $maxHardware; $this->baseManeuverability = IRound( 700 - diff --git a/src/lib/Default/SmrWeapon.php b/src/lib/Default/SmrWeapon.php index b2f3ad754..663aba744 100644 --- a/src/lib/Default/SmrWeapon.php +++ b/src/lib/Default/SmrWeapon.php @@ -12,8 +12,7 @@ class SmrWeapon extends AbstractSmrCombatWeapon { protected const HIGHEST_POWER_LEVEL = 5; // must track the highest power level in db - protected int $weaponTypeID; - protected SmrWeaponType $weaponType; + protected readonly SmrWeaponType $weaponType; protected bool $bonusAccuracy = false; // default protected bool $bonusDamage = false; // default protected bool $damageRollover = false; // fixed for all SmrWeapons @@ -22,9 +21,11 @@ public static function getWeapon(int $weaponTypeID, Smr\DatabaseRecord $dbRecord return new self($weaponTypeID, $dbRecord); } - protected function __construct(int $weaponTypeID, Smr\DatabaseRecord $dbRecord = null) { + protected function __construct( + protected readonly int $weaponTypeID, + Smr\DatabaseRecord $dbRecord = null + ) { $this->weaponType = SmrWeaponType::getWeaponType($weaponTypeID, $dbRecord); - $this->weaponTypeID = $weaponTypeID; $this->raceID = $this->weaponType->getRaceID(); } diff --git a/src/lib/Default/SmrWeaponType.php b/src/lib/Default/SmrWeaponType.php index 33dcaa017..c7541c1b9 100644 --- a/src/lib/Default/SmrWeaponType.php +++ b/src/lib/Default/SmrWeaponType.php @@ -9,14 +9,13 @@ class SmrWeaponType { protected static array $CACHE_WEAPON_TYPES = []; - protected int $weaponTypeID; - protected string $name; - protected int $cost; - protected int $shieldDamage; - protected int $armourDamage; - protected int $accuracy; - protected int $powerLevel; - protected int $buyerRestriction; + protected readonly string $name; + protected readonly int $cost; + protected readonly int $shieldDamage; + protected readonly int $armourDamage; + protected readonly int $accuracy; + protected readonly int $powerLevel; + protected readonly int $buyerRestriction; public static function getWeaponType(int $weaponTypeID, Smr\DatabaseRecord $dbRecord = null): self { if (!isset(self::$CACHE_WEAPON_TYPES[$weaponTypeID])) { @@ -56,8 +55,10 @@ public static function getAllSoldWeaponTypes(int $gameID): array { return $weapons; } - protected function __construct(int $weaponTypeID, Smr\DatabaseRecord $dbRecord) { - $this->weaponTypeID = $weaponTypeID; + protected function __construct( + protected readonly int $weaponTypeID, + Smr\DatabaseRecord $dbRecord + ) { $this->name = $dbRecord->getField('weapon_name'); $this->raceID = $dbRecord->getInt('race_id'); $this->cost = $dbRecord->getInt('cost'); diff --git a/src/lib/Default/WeightedRandom.php b/src/lib/Default/WeightedRandom.php index 8376b4bc9..dc9da5e33 100644 --- a/src/lib/Default/WeightedRandom.php +++ b/src/lib/Default/WeightedRandom.php @@ -18,10 +18,6 @@ class WeightedRandom { protected Smr\Database $db; - protected int $gameID; - protected int $accountID; - protected string $type; - protected int $typeID; protected float $weighting; protected bool $hasChanged = false; @@ -49,12 +45,12 @@ public static function saveWeightedRandoms(): void { } } - protected function __construct(int $gameID, int $accountID, string $type, int $typeID) { - $this->gameID = $gameID; - $this->accountID = $accountID; - $this->type = $type; - $this->typeID = $typeID; - + protected function __construct( + protected readonly int $gameID, + protected readonly int $accountID, + protected readonly string $type, + protected readonly int $typeID + ) { $this->db = Smr\Database::getInstance(); $dbResult = $this->db->read('SELECT weighting FROM weighted_random WHERE game_id = ' . $this->db->escapeNumber($gameID) . ' AND account_id = ' . $this->db->escapeNumber($accountID) . ' AND type = ' . $this->db->escapeString($type) . ' AND type_id = ' . $this->db->escapeNumber($typeID) . ' LIMIT 1'); if ($dbResult->hasRecord()) { diff --git a/src/lib/Smr/Blackjack/Card.php b/src/lib/Smr/Blackjack/Card.php index 107e42ef8..ca18912b8 100644 --- a/src/lib/Smr/Blackjack/Card.php +++ b/src/lib/Smr/Blackjack/Card.php @@ -15,14 +15,14 @@ class Card { private const SUITS = ['hearts', 'clubs', 'diamonds', 'spades']; - private int $cardID; // unique ID in all the decks (0-indexed) - private int $rank; // non-unique rank of the card (1-indexed) + private readonly int $rank; // non-unique rank of the card (1-indexed) /** * Create a specific card in the deck. */ - public function __construct(int $cardID) { - $this->cardID = $cardID; + public function __construct( + private readonly int $cardID // unique ID in all the decks (0-indexed) + ) { // 52 cards per deck, 13 cards per suit $this->rank = ($this->cardID % 52) % 13 + 1; } diff --git a/src/lib/Smr/Blackjack/Deck.php b/src/lib/Smr/Blackjack/Deck.php index e3463476a..28862f86b 100644 --- a/src/lib/Smr/Blackjack/Deck.php +++ b/src/lib/Smr/Blackjack/Deck.php @@ -13,7 +13,7 @@ class Deck { public const NUM_DECKS = 1; public const MAX_CARDS = 52 * self::NUM_DECKS; - private array $cardIDs = []; + private array $cardIDs; public function __construct() { $this->cardIDs = range(0, self::MAX_CARDS - 1); diff --git a/src/lib/Smr/Chess/ChessGame.php b/src/lib/Smr/Chess/ChessGame.php index 5f5a4d6a2..a990808e4 100644 --- a/src/lib/Smr/Chess/ChessGame.php +++ b/src/lib/Smr/Chess/ChessGame.php @@ -19,13 +19,12 @@ class ChessGame { private Smr\Database $db; - private int $chessGameID; - private int $gameID; - private int $startDate; + private readonly int $whiteID; + private readonly int $blackID; + private readonly int $gameID; + private readonly int $startDate; private int $endDate; private int $winner; - private int $whiteID; - private int $blackID; private array $hasMoved; private array $board; @@ -77,7 +76,7 @@ public static function getChessGame(int $chessGameID, bool $forceUpdate = false) return self::$CACHE_CHESS_GAMES[$chessGameID]; } - public function __construct(int $chessGameID) { + public function __construct(private readonly int $chessGameID) { $this->db = Database::getInstance(); $dbResult = $this->db->read('SELECT * FROM chess_game @@ -86,7 +85,6 @@ public function __construct(int $chessGameID) { throw new Exception('Chess game not found: ' . $chessGameID); } $dbRecord = $dbResult->record(); - $this->chessGameID = $chessGameID; $this->gameID = $dbRecord->getInt('game_id'); $this->startDate = $dbRecord->getInt('start_time'); $this->endDate = $dbRecord->getInt('end_time'); diff --git a/src/lib/Smr/Chess/ChessPiece.php b/src/lib/Smr/Chess/ChessPiece.php index 46b9cf5d4..25d2e4da7 100644 --- a/src/lib/Smr/Chess/ChessPiece.php +++ b/src/lib/Smr/Chess/ChessPiece.php @@ -12,7 +12,7 @@ class ChessPiece { public const PAWN = 6; public function __construct( - public string $colour, + public readonly string $colour, public int $pieceID, public int $x, public int $y, diff --git a/src/lib/Smr/Container/DiContainer.php b/src/lib/Smr/Container/DiContainer.php index b003556dd..62e8d3fe5 100644 --- a/src/lib/Smr/Container/DiContainer.php +++ b/src/lib/Smr/Container/DiContainer.php @@ -21,7 +21,7 @@ class DiContainer { private static DiContainer $instance; - private Container $container; + private readonly Container $container; private function __construct(bool $enableCompilation) { $this->container = $this->buildContainer($enableCompilation); diff --git a/src/lib/Smr/DatabaseRecord.php b/src/lib/Smr/DatabaseRecord.php index 50eab839c..160f54d51 100644 --- a/src/lib/Smr/DatabaseRecord.php +++ b/src/lib/Smr/DatabaseRecord.php @@ -8,7 +8,7 @@ class DatabaseRecord { * @param array $dbRecord A record from a DatabaseResult. */ public function __construct( - private array $dbRecord + private readonly array $dbRecord ) {} public function hasField(string $name): bool { diff --git a/src/lib/Smr/DatabaseResult.php b/src/lib/Smr/DatabaseResult.php index f0be90a82..2f59fbfc7 100644 --- a/src/lib/Smr/DatabaseResult.php +++ b/src/lib/Smr/DatabaseResult.php @@ -12,7 +12,7 @@ class DatabaseResult { public function __construct( - private mysqli_result $dbResult + private readonly mysqli_result $dbResult ) {} /** diff --git a/src/lib/Smr/Epoch.php b/src/lib/Smr/Epoch.php index 65284924b..5dc3f5a34 100644 --- a/src/lib/Smr/Epoch.php +++ b/src/lib/Smr/Epoch.php @@ -17,8 +17,8 @@ */ class Epoch { - private float $microtime; - private int $time; + private readonly float $microtime; + private readonly int $time; public function __construct() { $this->microtime = microtime(true); diff --git a/src/lib/Smr/Routes/MultiplePortRoute.php b/src/lib/Smr/Routes/MultiplePortRoute.php index 3c6c7a999..b4297c4d9 100644 --- a/src/lib/Smr/Routes/MultiplePortRoute.php +++ b/src/lib/Smr/Routes/MultiplePortRoute.php @@ -5,8 +5,8 @@ class MultiplePortRoute extends Route { public function __construct( - private Route $forwardRoute, - private OneWayRoute $returnRoute, + private readonly Route $forwardRoute, + private readonly OneWayRoute $returnRoute, ) {} public function getForwardRoute(): Route { diff --git a/src/lib/Smr/Routes/OneWayRoute.php b/src/lib/Smr/Routes/OneWayRoute.php index 99aafc43b..29ef74bc6 100644 --- a/src/lib/Smr/Routes/OneWayRoute.php +++ b/src/lib/Smr/Routes/OneWayRoute.php @@ -15,14 +15,14 @@ class OneWayRoute extends Route { * NOTE: Transactions are from the perspective of the player (not the port). */ public function __construct( - private int $buySectorId, - private int $sellSectorId, - private int $buyPortRace, - private int $sellPortRace, - private int $buyDi, - private int $sellDi, - private Path $path, - private int $goodId, + private readonly int $buySectorId, + private readonly int $sellSectorId, + private readonly int $buyPortRace, + private readonly int $sellPortRace, + private readonly int $buyDi, + private readonly int $sellDi, + private readonly Path $path, + private readonly int $goodId, ) {} public function getSellSectorId(): int { diff --git a/src/lib/Smr/VoteSite.php b/src/lib/Smr/VoteSite.php index 8420cb0ca..4cd17de78 100644 --- a/src/lib/Smr/VoteSite.php +++ b/src/lib/Smr/VoteSite.php @@ -86,9 +86,9 @@ public static function getMinTimeUntilFreeTurns(int $accountID): int { } private function __construct( - private int $linkID, - private int $accountID, - private array $data) {} + private readonly int $linkID, + private readonly int $accountID, + private readonly array $data) {} /** * Does this VoteSite have a voting callback that can be used