Skip to content

Commit

Permalink
Extract token array shape into a Psalm type
Browse files Browse the repository at this point in the history
This should make things less error prone and easier on the eyes.
  • Loading branch information
greg0ire committed Feb 27, 2022
1 parent 90d8024 commit c706a7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
12 changes: 7 additions & 5 deletions lib/Doctrine/Common/Lexer/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -38,7 +40,7 @@ abstract class AbstractLexer
* - 'position' : the position of the token in the input string
*
* @var mixed[][]
* @psalm-var list<array{value: string, type: string|int|null, position: int}>
* @psalm-var list<Token>
*/
private $tokens = [];

Expand All @@ -60,15 +62,15 @@ 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;

/**
* 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;

Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down
16 changes: 10 additions & 6 deletions tests/Doctrine/Common/Lexer/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\Common\Lexer;

use Doctrine\Common\Lexer\AbstractLexer;
use PHPUnit\Framework\TestCase;

use function array_map;
Expand All @@ -12,6 +13,9 @@

use const LC_ALL;

/**
* @psalm-import-type Token from AbstractLexer
*/
class AbstractLexerTest extends TestCase
{
/** @var ConcreteLexer */
Expand All @@ -28,7 +32,7 @@ public function tearDown(): void
}

/**
* @psalm-return list<array{string, list<array{value: string|int, type: string, position: int}>}>
* @psalm-return list<array{string, list<Token>}>
*/
public function dataProvider(): array
{
Expand Down Expand Up @@ -120,7 +124,7 @@ public function testResetPosition(): void
}

/**
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @psalm-param list<Token> $expectedTokens
*
* @dataProvider dataProvider
*/
Expand Down Expand Up @@ -172,7 +176,7 @@ public function testUtf8Mismatch(): void
}

/**
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @psalm-param list<Token> $expectedTokens
*
* @dataProvider dataProvider
*/
Expand All @@ -187,7 +191,7 @@ public function testPeek(string $input, array $expectedTokens): void
}

/**
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @psalm-param list<Token> $expectedTokens
*
* @dataProvider dataProvider
*/
Expand Down Expand Up @@ -227,7 +231,7 @@ public function testGetInputUntilPosition(
}

/**
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @psalm-param list<Token> $expectedTokens
*
* @dataProvider dataProvider
*/
Expand All @@ -243,7 +247,7 @@ public function testIsNextToken(string $input, array $expectedTokens): void
}

/**
* @psalm-param list<array{value: string|int, type: string, position: int}> $expectedTokens
* @psalm-param list<Token> $expectedTokens
*
* @dataProvider dataProvider
*/
Expand Down

0 comments on commit c706a7d

Please sign in to comment.