Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/buddy.yml export-ignore
/.php-cs-fixer.php export-ignore
/phpstan.neon export-ignore
/phpunit.xml export-ignore
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: ["7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
php: ["8.1", "8.2", "8.3", "8.4"]

name: php-${{ matrix.php }}

Expand All @@ -21,6 +21,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ctype, gd

- name: Validate composer.json and composer.lock
run: composer validate --strict
Expand All @@ -34,7 +35,7 @@ jobs:
restore-keys: |
${{ matrix.php }}-php-
- name: Install dependencies
run: composer update --no-interaction --prefer-dist --no-progress
run: composer update --no-interaction --prefer-dist --no-progress --ansi

- name: Execute tests
run: vendor/bin/phpunit --bootstrap vendor/autoload.php tests
run: vendor/bin/phpunit --color=always
4 changes: 2 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'@PHP74Migration' => true,
'@PHP81Migration' => true,
'binary_operator_spaces' => ['operators' => ['=>' => 'single_space', '=' => 'single_space']],
'blank_line_before_statement' => ['statements' => ['return']],
'cast_spaces' => true,
Expand All @@ -23,7 +23,7 @@
'self_accessor' => true,
'single_quote' => true,
'return_type_declaration' => true,
'trailing_comma_in_multiline' => true,
'trailing_comma_in_multiline' => ['elements' => ['arguments', 'arrays', 'match', 'parameters']],
'trim_array_spaces' => true,
'void_return' => true,
])
Expand Down
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"ext-ctype": "*",
"ext-json": "*"
"php": "^8.1",
"ext-ctype": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.48",
"imagine/imagine": "^1.3",
"johnkary/phpunit-speedtrap": "^4.0",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6"
"ergebnis/phpunit-slow-test-detector": "^2.19",
"friendsofphp/php-cs-fixer": "^3.75",
"imagine/imagine": "^1.5",
"phpbench/phpbench": "^1.4",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^10.5 || ^11.5 || ^12.2"
},
"suggest": {
"imagine/imagine": "To generate board images."
Expand Down
19 changes: 6 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<coverage>
<source>
<include>
<directory>./src</directory>
</include>
</coverage>
</source>

<testsuites>
<testsuite name="Chess.php Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener"/>
</listeners>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
</phpunit>
2 changes: 1 addition & 1 deletion src/Chess.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ protected function recordMove(Move $move): int
$this->castling,
$this->epSquare,
$this->halfMoves,
$this->moveNumber
$this->moveNumber,
));

return $this->history->key();
Expand Down
39 changes: 8 additions & 31 deletions src/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,24 @@

namespace PChess\Chess;

class Entry
class Entry implements \Stringable
{
public Move $move;

public ?string $position;

/** @var array<string, ?int> */
public array $kings;

public string $turn;

/** @var array<string, ?int> */
public array $castling;

public ?int $epSquare;

public int $halfMoves;

public int $moveNumber;

/**
* @param array<string, ?int> $kings
* @param array<string, ?int> $castling
*/
public function __construct(
Move $move,
?string $position,
array $kings,
array $castling,
?int $epSquare,
int $halfMoves,
int $moveNumber
public Move $move,
public ?string $position,
public array $kings,
public array $castling,
public ?int $epSquare,
public int $halfMoves,
public int $moveNumber,
) {
$this->move = $move;
$this->position = $position;
$this->kings = $kings;
$this->turn = $move->turn;
$this->castling = $castling;
$this->epSquare = $epSquare;
$this->halfMoves = $halfMoves;
$this->moveNumber = $moveNumber;
}

public function __toString(): string
Expand Down
8 changes: 2 additions & 6 deletions src/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@

final class History
{
/** @var array<int, Entry> */
private array $entries;

/** @param array<int, Entry>|null $entries */
public function __construct(?array $entries = null)
/** @param array<int, Entry> $entries */
public function __construct(private array $entries = [])
{
$this->entries = $entries ?? [];
}

public function add(Entry $entry): void
Expand Down
32 changes: 12 additions & 20 deletions src/Move.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PChess\Chess;

final class Move implements \JsonSerializable
final class Move implements \JsonSerializable, \Stringable
{
public const BITS = [
'NORMAL' => 1,
Expand All @@ -16,12 +16,6 @@ final class Move implements \JsonSerializable
'QSIDE_CASTLE' => 64,
];

public string $turn;

public int $flags;

public Piece $piece;

public string $from;

public string $to;
Expand All @@ -30,23 +24,21 @@ final class Move implements \JsonSerializable

public int $toSquare;

public ?string $captured;

public ?string $promotion;

public ?string $san = null;

public function __construct(string $turn, int $flags, Piece $piece, int $from, int $to, ?string $captured = null, ?string $promotion = null)
{
$this->turn = $turn;
$this->flags = $flags;
$this->piece = $piece;
public function __construct(
public string $turn,
public int $flags,
public Piece $piece,
int $from,
int $to,
public ?string $captured = null,
public ?string $promotion = null,
) {
$this->fromSquare = $from;
$this->toSquare = $to;
$this->from = self::getSquare($from);
$this->to = self::getSquare($to);
$this->captured = $captured;
$this->promotion = $promotion;
}

public function __toString(): string
Expand All @@ -60,7 +52,7 @@ public static function buildMove(
int $from,
int $to,
int $flags,
?string $promotion = null
?string $promotion = null,
): self {
$captured = null;
if ($board[$to] !== null) {
Expand All @@ -82,7 +74,7 @@ public static function buildMove(
$from,
$to,
$captured,
$promotion
$promotion,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Output/ImagineOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
int $size = 400,
bool $coords = false,
string $darkSquareColor = '#8ca2ad',
string $liteSquareColor = '#dee3e6'
string $liteSquareColor = '#dee3e6',
) {
$this->imagine = $imagine;
$this->spritesPath = $spritesPath;
Expand Down
8 changes: 1 addition & 7 deletions src/Output/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@
*/
final class Link
{
private ?string $class;

private ?string $url;

public function __construct(?string $class, ?string $url)
public function __construct(public ?string $class, public ?string $url)
{
$this->class = $class;
$this->url = $url;
}

public function getClass(): ?string
Expand Down
10 changes: 2 additions & 8 deletions src/Piece.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PChess\Chess;

final class Piece implements \JsonSerializable
final class Piece implements \JsonSerializable, \Stringable
{
public const SYMBOLS = 'pnbrqkPNBRQK';

Expand Down Expand Up @@ -43,10 +43,6 @@ final class Piece implements \JsonSerializable
self::KING => 5,
];

private string $type;

private string $color;

/** @var array<string> */
private static array $types = [
self::PAWN,
Expand Down Expand Up @@ -80,16 +76,14 @@ final class Piece implements \JsonSerializable
],
];

public function __construct(string $type, string $color)
public function __construct(private string $type, private string $color)
{
if (!\in_array($type, self::$types, true)) {
throw new \InvalidArgumentException('Invalid piece type');
}
if (!\in_array($color, self::$colors, true)) {
throw new \InvalidArgumentException('Invalid piece color');
}
$this->type = $type;
$this->color = $color;
}

public function __toString(): string
Expand Down
2 changes: 1 addition & 1 deletion tests/AttackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PChess\Chess\Piece;
use PHPUnit\Framework\TestCase;

class AttackTest extends TestCase
final class AttackTest extends TestCase
{
public function testAttackedPawn(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/ChessPublicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PChess\Chess\Move;

// a proxy for testing protected method
class ChessPublicator extends Chess
final class ChessPublicator extends Chess
{
public function getBoard(): Board
{
Expand Down Expand Up @@ -41,7 +41,7 @@ public static function buildMovePublic(
int $from,
int $to,
int $flags,
?string $promotion = null
?string $promotion = null,
): Move {
return Move::buildMove($turn, $board, $from, $to, $flags, $promotion);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use PChess\Chess\Piece;
use PHPUnit\Framework\TestCase;

class ConstructorTest extends TestCase
final class ConstructorTest extends TestCase
{
public function testDefaultPosition(): void
{
Expand Down
5 changes: 2 additions & 3 deletions tests/FenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

use PChess\Chess\Board;
use PChess\Chess\Validation;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class FenTest extends TestCase
{
/**
* @dataProvider provider
*/
#[DataProvider('provider')]
public function testValidate(string $fen, int $code): void
{
self::assertEquals($code, Validation::validateFen($fen));
Expand Down
2 changes: 1 addition & 1 deletion tests/GameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PChess\Chess\Chess;
use PHPUnit\Framework\TestCase;

class GameTest extends TestCase
final class GameTest extends TestCase
{
/**
* TODO re-check this test.
Expand Down
Loading