Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rector updates #1989

Merged
merged 10 commits into from
Dec 25, 2024
24 changes: 24 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"test": [
"@phpstan",
"@phpunit",
"@phpcs"
"@phpcs",
"@rector"
],
"phpunit": [
"docker compose --env-file test/env run --rm phpunit"
Expand All @@ -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"
Expand Down
43 changes: 29 additions & 14 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
<?php declare(strict_types=1);

use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
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;
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\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
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([
AddTypeToConstRector::class,
AddOverrideAttributeToOverriddenMethodsRector::class,
ClassOnObjectRector::class,
ClassPropertyAssignToConstructorPromotionRector::class,
CombinedAssignRector::class,
DirNameFileConstantToDirConstantRector::class,
FirstClassCallableRector::class,
IfIssetToCoalescingRector::class,
JsonThrowOnErrorRector::class,
NullCoalescingOperatorRector::class,
ReadOnlyPropertyRector::class,
ThisCallOnStaticMethodToStaticCallRector::class,
])
->withSets([
PHPUnitSetList::PHPUNIT_100,
]);
};
])
;
2 changes: 1 addition & 1 deletion src/htdocs/imprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Default/smr.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,15 @@
$weeks = 0;
if ($minutes >= 60) {
$hours = floor($minutes / 60);
$minutes = $minutes % 60;
$minutes %= 60;

Check warning on line 732 in src/lib/Default/smr.inc.php

View check run for this annotation

Codecov / codecov/patch

src/lib/Default/smr.inc.php#L732

Added line #L732 was not covered by tests
}
if ($hours >= 24) {
$days = floor($hours / 24);
$hours = $hours % 24;
$hours %= 24;

Check warning on line 736 in src/lib/Default/smr.inc.php

View check run for this annotation

Codecov / codecov/patch

src/lib/Default/smr.inc.php#L736

Added line #L736 was not covered by tests
}
if ($days >= 7) {
$weeks = floor($days / 7);
$days = $days % 7;
$days %= 7;

Check warning on line 740 in src/lib/Default/smr.inc.php

View check run for this annotation

Codecov / codecov/patch

src/lib/Default/smr.inc.php#L740

Added line #L740 was not covered by tests
}
$times = [
'week' => $weeks,
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
17 changes: 7 additions & 10 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 All @@ -30,8 +30,6 @@ class AbstractShip {
RACE_NIJARIN => SHIP_TYPE_REDEEMER,
];

protected AbstractPlayer $player;

protected int $gameID;
protected ShipType $shipType;

Expand All @@ -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();
}

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/Blackjack/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class Table {

private Deck $deck;
private readonly Deck $deck;
public Hand $playerHand;
public Hand $dealerHand;

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
8 changes: 4 additions & 4 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 Expand Up @@ -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);
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
Loading
Loading