diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 775d6e2..3c4b6d7 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -15,7 +15,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -27,7 +27,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: vendor key: ${{ matrix.php }}-php-${{ hashFiles('**/composer.lock') }} diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index d9a90b5..03cc6c0 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -2,10 +2,9 @@ return (new PhpCsFixer\Config()) ->setRules([ - '@PSR2' => true, + '@PSR12' => true, 'array_syntax' => ['syntax' => 'short'], 'binary_operator_spaces' => ['operators' => ['=>' => 'single_space', '=' => 'single_space']], - 'blank_line_after_opening_tag' => true, 'blank_line_before_statement' => ['statements' => ['return']], 'cast_spaces' => true, 'concat_space' => ['spacing' => 'none'], @@ -20,12 +19,10 @@ 'no_unneeded_control_parentheses' => true, 'no_unused_imports' => true, 'no_whitespace_in_blank_line' => true, - 'ordered_imports' => true, 'phpdoc_align' => true, 'phpdoc_no_access' => true, 'php_unit_fqcn_annotation' => true, 'self_accessor' => true, - 'single_blank_line_before_namespace' => true, 'single_quote' => true, 'return_type_declaration' => true, 'trailing_comma_in_multiline' => true, diff --git a/README.md b/README.md index 60bb77c..696d920 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Chess is a PHP chess library that is used for chess move generation/validation, piece placement/movement, and check/checkmate/stalemate detection - basically everything but the AI. -NOTE: this started as a port of [chess.js](https://github.com/jhlywa/chess.js) for php, froked from [ryanhs/chess.php](https://github.com/ryanhs/chess.php) +NOTE: this started as a port of [chess.js](https://github.com/jhlywa/chess.js) for php, forked from [ryanhs/chess.php](https://github.com/ryanhs/chess.php) [![Latest Stable Version](https://poser.pugx.org/p-chess/chess/v/stable)](https://packagist.org/p-chess/chess) [![buddy branch](https://app.buddy.works/akondas/chess/repository/branch/master/badge.svg?token=bfd952ec0cee0cb4db84dbd50ded487354ee6c9f37a7034f7c46425fed70dea7 "buddy branch")](https://app.buddy.works/akondas/chess/repository/branch/master) @@ -117,13 +117,13 @@ There is still a lot to do in this topic. ### Chess::move() | iteration | mean | comment | -| :-------: | :---: | ------- | +|-----------|-------|---------| | 1 | 548.819μs | initial | -| 2 | 447.973μs | replace fen with json_encode in history position (inThreefoldRepetition cache) -| 3 | 340.375μs | replace fen with json_encode in generateMoves -| 4 | 333.145μs | add boardHash calculation on make/undo move -| 5 | 25.917μs | :fire: add cache for moveToSAN method +| 2 | 447.973μs | replace fen with json_encode in history position (inThreefoldRepetition cache) | +| 3 | 340.375μs | replace fen with json_encode in generateMoves | +| 4 | 333.145μs | add boardHash calculation on make/undo move | +| 5 | 25.917μs | :fire: add cache for moveToSAN method | ## Other documentation -All classes are documented in [docs directory](docs/index.md). +All classes are documented in the [docs directory](docs/index.md). diff --git a/benchmarks/MoveBench.php b/benchmarks/MoveBench.php index d1372da..1291c7f 100644 --- a/benchmarks/MoveBench.php +++ b/benchmarks/MoveBench.php @@ -14,10 +14,7 @@ */ final class MoveBench { - /** - * @var Chess - */ - private $chess; + private Chess $chess; public function init(): void { @@ -26,6 +23,7 @@ public function init(): void /** * @Revs(1000) + * * @Iterations(5) */ public function benchMove(): void @@ -35,6 +33,7 @@ public function benchMove(): void /** * @Revs(1000) + * * @Iterations(5) */ public function benchMoves(): void diff --git a/composer.json b/composer.json index b3bdde4..5d7a142 100644 --- a/composer.json +++ b/composer.json @@ -23,12 +23,12 @@ "ext-json": "*" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.13", + "friendsofphp/php-cs-fixer": "^3.37", "imagine/imagine": "^1.3", "johnkary/phpunit-speedtrap": "^4.0", "phpbench/phpbench": "^1.2", - "phpstan/phpstan": "^1.9", - "phpunit/phpunit": "^9.5" + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.6" }, "suggest": { "imagine/imagine": "To generate board images." diff --git a/docs/board.md b/docs/board.md index 4b740d0..cbef618 100644 --- a/docs/board.md +++ b/docs/board.md @@ -27,7 +27,7 @@ with [SAN](https://en.wikipedia.org/wiki/Algebraic_notation_(chess)): * `algebraic()`: get the SAN of a square, as string (e.g. "e2"). This should be the same as calling `Board::SQUARES[$square]` -Such methods accept a integer square (see `SQUARES` constant). +Such methods accept an integer square (see `SQUARES` constant). Useful constants represent starting board (`Board::DEFAULT_POSITION`) and empty board (`Board::EMPTY`) in [FEN](https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation) notation. diff --git a/docs/output_imagine.md b/docs/output_imagine.md index 28a9804..b64fb5c 100644 --- a/docs/output_imagine.md +++ b/docs/output_imagine.md @@ -6,7 +6,7 @@ and one extension between GD and Imagick. You need to provide an instance of Imagine, and a path of images for pieces. Names of such images need to match a color/name code. For example, a black pawn image must be named `bp.png` (_b_ for black and _p_ for pawn), a -white queen image must me named `wq.png` (_w_ for white and _q_ for queen). +white queen image must be named `wq.png` (_w_ for white and _q_ for queen). Refer to `Piece` class source code to get all abbreviations. A good source for free images is [Wikimedia](https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces). @@ -52,5 +52,5 @@ public function __construct( ); ``` -In this directory, you can find an utility script, named [get-resources.sh](get-resources.sh), that downloads +In this directory, you can find a utility script, named [get-resources.sh](get-resources.sh), that downloads all pieces images from Wikimedia, plus a free font to use. diff --git a/src/Board.php b/src/Board.php index a667b43..526adcc 100644 --- a/src/Board.php +++ b/src/Board.php @@ -115,7 +115,7 @@ public function offsetUnset($offset): void public function current(): ?Piece { - return \current($this->squares) !== false ? \current($this->squares): null; + return \current($this->squares) !== false ? \current($this->squares) : null; } public function next(): void diff --git a/src/Chess.php b/src/Chess.php index 798e1e3..77087d8 100644 --- a/src/Chess.php +++ b/src/Chess.php @@ -395,7 +395,7 @@ public function undo(): ?Move */ protected function generateMoves(int $square = null, bool $legal = true): array { - $cacheKey = $this->boardHash.\json_encode($square.($legal ? '1' : '0')); + $cacheKey = $this->boardHash.\json_encode($square.($legal ? '1' : '0'), JSON_THROW_ON_ERROR); // check cache first if (isset($this->generateMovesCache[$cacheKey])) { diff --git a/src/Entry.php b/src/Entry.php index 20e3432..fbdfdcf 100644 --- a/src/Entry.php +++ b/src/Entry.php @@ -6,29 +6,23 @@ class Entry { - /** @var Move */ - public $move; + public Move $move; - /** @var string|null */ - public $position; + public ?string $position; /** @var array */ - public $kings; + public array $kings; - /** @var string */ - public $turn; + public string $turn; /** @var array */ - public $castling; + public array $castling; - /** @var int|null */ - public $epSquare; + public ?int $epSquare; - /** @var int */ - public $halfMoves; + public int $halfMoves; - /** @var int */ - public $moveNumber; + public int $moveNumber; /** * @param array $kings diff --git a/src/History.php b/src/History.php index 43348ae..42e8ec0 100644 --- a/src/History.php +++ b/src/History.php @@ -7,7 +7,7 @@ final class History { /** @var array */ - private $entries; + private array $entries; /** @param array|null $entries */ public function __construct(array $entries = null) diff --git a/src/Move.php b/src/Move.php index 41347ec..2b0f93d 100644 --- a/src/Move.php +++ b/src/Move.php @@ -16,35 +16,25 @@ final class Move implements \JsonSerializable 'QSIDE_CASTLE' => 64, ]; - /** @var string */ - public $turn; + public string $turn; - /** @var int */ - public $flags; + public int $flags; - /** @var Piece */ - public $piece; + public Piece $piece; - /** @var string */ - public $from; + public string $from; - /** @var string */ - public $to; + public string $to; - /** @var int */ - public $fromSquare; + public int $fromSquare; - /** @var int */ - public $toSquare; + public int $toSquare; - /** @var string|null */ - public $captured; + public ?string $captured; - /** @var string|null */ - public $promotion; + public ?string $promotion; - /** @var string|null */ - public $san; + public ?string $san; public function __construct(string $turn, int $flags, Piece $piece, int $from, int $to, ?string $captured = null, ?string $promotion = null) { diff --git a/src/Output/BasicOutput.php b/src/Output/BasicOutput.php index 518a952..6bcfb12 100644 --- a/src/Output/BasicOutput.php +++ b/src/Output/BasicOutput.php @@ -8,8 +8,7 @@ abstract class BasicOutput { - /** @var string */ - protected static $line = ' +---+---+---+---+---+---+---+---+'.PHP_EOL; + protected static string $line = ' +---+---+---+---+---+---+---+---+'.PHP_EOL; protected static function getLines(bool $reversed, int $position): string { diff --git a/src/Output/HtmlOutput.php b/src/Output/HtmlOutput.php index 68c8438..e33f4ef 100644 --- a/src/Output/HtmlOutput.php +++ b/src/Output/HtmlOutput.php @@ -46,7 +46,7 @@ public function render(Chess $chess, ?string $from = null, $identifier = null): } /** - * Generete an array of 64 Link objects to use in render() method. + * Generate an array of 64 Link objects to use in render() method. * Basically, you should cycle the board, assigning values to Link for pieces that can actually move. * There are two possible situations: 1) $from is null (move has to start); 2) $from is not null (move started). * In first case, you should assign a "start move" link to every piece that is allowed to move diff --git a/src/Output/Link.php b/src/Output/Link.php index e12493d..af9dd14 100644 --- a/src/Output/Link.php +++ b/src/Output/Link.php @@ -6,13 +6,13 @@ /** * This is a simple value object used by HtmlOutput to represent - * a CSS class and an URL (both can be null). + * a CSS class and a URL (both can be null). */ final class Link { - private ?string $class = null; + private ?string $class; - private ?string $url = null; + private ?string $url; public function __construct(?string $class, ?string $url) { diff --git a/src/Piece.php b/src/Piece.php index 9599eef..53a2938 100644 --- a/src/Piece.php +++ b/src/Piece.php @@ -18,6 +18,9 @@ final class Piece implements \JsonSerializable public const BLACK = 'b'; public const WHITE = 'w'; + /** + * @var array + */ public const PAWN_OFFSETS = [ self::BLACK => [16, 32, 17, 15], self::WHITE => [-16, -32, -17, -15], diff --git a/tests/FenTest.php b/tests/FenTest.php index 78dc1a4..9558772 100644 --- a/tests/FenTest.php +++ b/tests/FenTest.php @@ -27,7 +27,7 @@ public function testGetErrorString(): void /** * @return array> */ - public function provider(): array + public static function provider(): array { return [ 'default position' => [Board::DEFAULT_POSITION, 0], diff --git a/tests/MoveTest.php b/tests/MoveTest.php index 2704a28..5619158 100644 --- a/tests/MoveTest.php +++ b/tests/MoveTest.php @@ -416,7 +416,7 @@ public function testMoveToSAN(): void \array_walk($moves, static function (Move $move) use ($chess): void { $chess->moveToSANPublic($move); }); - $sans = \array_map(static function (Move $move): string { + $sans = \array_map(static function (Move $move): ?string { return $move->san; }, $moves); self::assertContains('f8=Q', $sans); @@ -461,7 +461,7 @@ public function testArrayMove(): void /** * @dataProvider gameProvider */ - public function testSANMoveFromRealGame(string $match, string $finalFen): void + public static function testSANMoveFromRealGame(string $match, string $finalFen): void { $chess = new ChessPublicator(); $moves = \explode(' ', $match); diff --git a/tests/PerftTest.php b/tests/PerftTest.php index 693e3f4..661831c 100644 --- a/tests/PerftTest.php +++ b/tests/PerftTest.php @@ -12,7 +12,7 @@ class PerftTest extends TestCase /** * @dataProvider provider */ - public function testPerft(string $fen, int $expectedDeep1, int $expectedDeep2): void + public static function testPerft(string $fen, int $expectedDeep1, int $expectedDeep2): void { $chess = new ChessPublicator($fen); self::assertEquals($expectedDeep1, $chess->perft(1));