Skip to content

Commit

Permalink
Extract token array shapes into Psalm types
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 50de489
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
24 changes: 13 additions & 11 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: int|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 @@ -148,25 +150,25 @@ public function getInputUntilPosition($position)
/**
* Checks whether a given token matches the current lookahead.
*
* @param int|string $token
* @param int|string $type
*
* @return bool
*/
public function isNextToken($token)
public function isNextToken($type)
{
return $this->lookahead !== null && $this->lookahead['type'] === $token;
return $this->lookahead !== null && $this->lookahead['type'] === $type;
}

/**
* Checks whether any of the given tokens matches the current lookahead.
*
* @param string[] $tokens
* @param list<int|string> $type
*
* @return bool
*/
public function isNextTokenAny(array $tokens)
public function isNextTokenAny(array $type)
{
return $this->lookahead !== null && in_array($this->lookahead['type'], $tokens, true);
return $this->lookahead !== null && in_array($this->lookahead['type'], $type, true);
}

/**
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
15 changes: 9 additions & 6 deletions tests/Doctrine/Common/Lexer/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

use const LC_ALL;

/**
* @psalm-type ValidToken = array{value: string|int, type: string|int, position: int}
*/
class AbstractLexerTest extends TestCase
{
/** @var ConcreteLexer */
Expand All @@ -28,7 +31,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<ValidToken>}>
*/
public function dataProvider(): array
{
Expand Down Expand Up @@ -120,7 +123,7 @@ public function testResetPosition(): void
}

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

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

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

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

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

0 comments on commit 50de489

Please sign in to comment.