From c706a7d78c57e34e3634f1a65f574d837eadfbc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 27 Feb 2022 15:33:54 +0100 Subject: [PATCH] Extract token array shape into a Psalm type This should make things less error prone and easier on the eyes. --- lib/Doctrine/Common/Lexer/AbstractLexer.php | 12 +++++++----- .../Doctrine/Common/Lexer/AbstractLexerTest.php | 16 ++++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/Doctrine/Common/Lexer/AbstractLexer.php b/lib/Doctrine/Common/Lexer/AbstractLexer.php index fe01ab0..cfc9b63 100644 --- a/lib/Doctrine/Common/Lexer/AbstractLexer.php +++ b/lib/Doctrine/Common/Lexer/AbstractLexer.php @@ -18,6 +18,8 @@ /** * Base class for writing simple lexers, i.e. for creating small DSLs. + * + * @psalm-type Token = array{value: string, type:string|int|null, position:int} */ abstract class AbstractLexer { @@ -38,7 +40,7 @@ abstract class AbstractLexer * - 'position' : the position of the token in the input string * * @var mixed[][] - * @psalm-var list + * @psalm-var list */ private $tokens = []; @@ -60,7 +62,7 @@ abstract class AbstractLexer * The next token in the input. * * @var mixed[]|null - * @psalm-var array{value: string, type: string|int|null, position: int}|null + * @psalm-var Token|null */ public $lookahead; @@ -68,7 +70,7 @@ abstract class AbstractLexer * The last matched/seen token. * * @var mixed[]|null - * @psalm-var array{value: string, type: string|int|null, position: int}|null + * @psalm-var Token|null */ public $token; @@ -215,7 +217,7 @@ public function isA($value, $token) * Moves the lookahead token forward. * * @return mixed[]|null The next token or NULL if there are no more tokens ahead. - * @psalm-return array{value: string, type: string|int|null, position: int}|null + * @psalm-return Token|null */ public function peek() { @@ -230,7 +232,7 @@ public function peek() * Peeks at the next token, returns it and immediately resets the peek. * * @return mixed[]|null The next token or NULL if there are no more tokens ahead. - * @psalm-return array{value: string, type: string|int|null, position: int}|null + * @psalm-return Token|null */ public function glimpse() { diff --git a/tests/Doctrine/Common/Lexer/AbstractLexerTest.php b/tests/Doctrine/Common/Lexer/AbstractLexerTest.php index b5cdde1..814656c 100644 --- a/tests/Doctrine/Common/Lexer/AbstractLexerTest.php +++ b/tests/Doctrine/Common/Lexer/AbstractLexerTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\Common\Lexer; +use Doctrine\Common\Lexer\AbstractLexer; use PHPUnit\Framework\TestCase; use function array_map; @@ -12,6 +13,9 @@ use const LC_ALL; +/** + * @psalm-import-type Token from AbstractLexer + */ class AbstractLexerTest extends TestCase { /** @var ConcreteLexer */ @@ -28,7 +32,7 @@ public function tearDown(): void } /** - * @psalm-return list}> + * @psalm-return list}> */ public function dataProvider(): array { @@ -120,7 +124,7 @@ public function testResetPosition(): void } /** - * @psalm-param list $expectedTokens + * @psalm-param list $expectedTokens * * @dataProvider dataProvider */ @@ -172,7 +176,7 @@ public function testUtf8Mismatch(): void } /** - * @psalm-param list $expectedTokens + * @psalm-param list $expectedTokens * * @dataProvider dataProvider */ @@ -187,7 +191,7 @@ public function testPeek(string $input, array $expectedTokens): void } /** - * @psalm-param list $expectedTokens + * @psalm-param list $expectedTokens * * @dataProvider dataProvider */ @@ -227,7 +231,7 @@ public function testGetInputUntilPosition( } /** - * @psalm-param list $expectedTokens + * @psalm-param list $expectedTokens * * @dataProvider dataProvider */ @@ -243,7 +247,7 @@ public function testIsNextToken(string $input, array $expectedTokens): void } /** - * @psalm-param list $expectedTokens + * @psalm-param list $expectedTokens * * @dataProvider dataProvider */