Skip to content

Commit

Permalink
rector: add AddTypeToConstRector
Browse files Browse the repository at this point in the history
Explicitly declare types for all class constants.

Related to smrealms#1775.
  • Loading branch information
hemberger committed Dec 25, 2024
1 parent 75f8d9f commit f3397dd
Show file tree
Hide file tree
Showing 43 changed files with 137 additions and 135 deletions.
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +20,7 @@
])
->withImportNames(true, false)
->withRules([
AddTypeToConstRector::class,
AddOverrideAttributeToOverriddenMethodsRector::class,
ClassOnObjectRector::class,
ClassPropertyAssignToConstructorPromotionRector::class,
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Smr/AbstractPlayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, array<int, array<int, Player>>> */
protected static array $CACHE_SECTOR_PLAYERS = [];
Expand All @@ -41,7 +41,7 @@ abstract class AbstractPlayer {
/** @var array<int, array<int, Player>> */
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;

Expand Down
10 changes: 5 additions & 5 deletions src/lib/Smr/AbstractShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Smr/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -22,7 +22,7 @@ class Account {

/** @var array<int, self> */
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'],
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/Smr/AdminPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Smr/Alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Alliance {
/** @var array<int, array<int, self>> */
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;

Expand All @@ -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 = [];
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Smr/BarDrink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.',
];
Expand Down
10 changes: 5 additions & 5 deletions src/lib/Smr/Blackjack/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Smr/Bounty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Smr/Chess/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<value-of<Colour>, array<Castling>> */
private array $canCastle;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Smr/Chess/ChessGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, self> */
protected static array $CACHE_CHESS_GAMES = [];
Expand Down
12 changes: 6 additions & 6 deletions src/lib/Smr/Chess/ChessPiece.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Smr/Combat/Weapon/AbstractWeapon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/Smr/Combat/Weapon/CombatDrones.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Smr/Combat/Weapon/Mines.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Smr/Combat/Weapon/Weapon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Smr/EnhancedWeaponEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 12 additions & 12 deletions src/lib/Smr/Force.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class Force {
/** @var array<int, array<int, bool>> */
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;

Expand Down
10 changes: 5 additions & 5 deletions src/lib/Smr/Galaxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Galaxy {
/** @var array<int, array<int, self>> */
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;

Expand Down
14 changes: 7 additions & 7 deletions src/lib/Smr/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Smr/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Location {
/** @var array<int, array<int, array<int, self>>> */
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;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/Smr/Lotto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
12 changes: 6 additions & 6 deletions src/lib/Smr/Planet.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class Planet {
/** @var array<int, array<int, self>> */
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;

Expand Down
Loading

0 comments on commit f3397dd

Please sign in to comment.