From cc61e9d87767a4fecc8063e438a1cb8fe6a72e97 Mon Sep 17 00:00:00 2001 From: Massimiliano Arione Date: Mon, 22 Jan 2024 17:18:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20improve=20code=20and=20analysis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .php-cs-fixer.php | 9 ++------- README.md | 17 ++++++++--------- composer.json | 6 +++--- src/Board.php | 2 +- tests/ChessPublicator.php | 20 ++++++++++---------- tests/MoveTest.php | 16 +++++++++++++++- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index d9a90b5..472277c 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -2,10 +2,9 @@ return (new PhpCsFixer\Config()) ->setRules([ - '@PSR2' => true, - 'array_syntax' => ['syntax' => 'short'], + '@PSR12' => true, + '@PHP74Migration' => true, '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'], @@ -13,19 +12,15 @@ 'fully_qualified_strict_types' => true, 'phpdoc_separation' => true, 'native_function_invocation' => ['include' => ['@all']], - 'new_with_braces' => true, - 'no_blank_lines_after_class_opening' => true, 'no_extra_blank_lines' => true, 'no_spaces_around_offset' => ['positions' => ['inside', 'outside']], '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..2112c5a 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,9 @@ 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) [![MIT License](https://poser.pugx.org/p-chess/chess/license)](https://packagist.org/packages/p-chess/chess) ## Installation @@ -116,13 +115,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 +| 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 | ## Other documentation diff --git a/composer.json b/composer.json index b3bdde4..9cf8d00 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.48", "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/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/tests/ChessPublicator.php b/tests/ChessPublicator.php index b613c42..4b308fc 100644 --- a/tests/ChessPublicator.php +++ b/tests/ChessPublicator.php @@ -56,7 +56,7 @@ public function undoMovePublic(): ?Move return $this->undoMove(); } - public function moveToSANPublic(?Move $move): void + public function moveToSANPublic(Move $move): void { $this->moveToSAN($move); } @@ -81,17 +81,17 @@ public function perft(int $depth, bool $full = false) if (!$this->kingAttacked($color)) { if ($depth - 1 > 0) { - $childs = $this->perft($depth - 1, true); - if (!\is_array($childs)) { + $children = $this->perft($depth - 1, true); + if (!\is_array($children)) { continue; } - $nodes += $childs['nodes']; - $captures += $childs['captures']; - $enPassants += $childs['enPassants']; - $castles += $childs['castles']; - $promotions += $childs['promotions']; - $checks += $childs['checks']; - $checkmates += $childs['checkmates']; + $nodes += $children['nodes']; + $captures += $children['captures']; + $enPassants += $children['enPassants']; + $castles += $children['castles']; + $promotions += $children['promotions']; + $checks += $children['checks']; + $checkmates += $children['checkmates']; } else { ++$nodes; } diff --git a/tests/MoveTest.php b/tests/MoveTest.php index 2704a28..80e6787 100644 --- a/tests/MoveTest.php +++ b/tests/MoveTest.php @@ -237,6 +237,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('e4', $undo->san); self::assertEquals('e4', (string) $undo); @@ -251,6 +252,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Nf6', $undo->san); // normal pawn capture @@ -264,6 +266,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('exd5', $undo->san); // en passant capture @@ -277,6 +280,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('dxe6', $undo->san); // normal knight capture @@ -290,6 +294,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Nxd5', $undo->san); // promotion @@ -304,6 +309,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('d8=R', $undo->san); // check @@ -317,6 +323,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Rf8+', $undo->san); // checkmate @@ -330,6 +337,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Rb8#', $undo->san); // ambiguous moves: row @@ -343,6 +351,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('N4xd6', $undo->san); // ambiguous moves: rank > 0 & file > 0 @@ -356,6 +365,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Qd5xd4', $undo->san); // ambiguous moves: col @@ -369,6 +379,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Nexd6', $undo->san); // ambiguous moves: col @@ -382,6 +393,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Ncxd6', $undo->san); // ambiguous moves: normal capture @@ -395,6 +407,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Nxd6', $undo->san); // ambiguous moves: normal capture @@ -408,6 +421,7 @@ public function testMoveToSAN(): void )); $chess->makeMovePublic($move); $undo = $chess->undo(); + self::assertNotNull($undo); self::assertEquals('Rxd6', $undo->san); // generate moves test @@ -416,7 +430,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);