Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract token array shape into a Psalm type #64

Merged
merged 1 commit into from
Feb 28, 2022
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
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> $types
*
* @return bool
*/
public function isNextTokenAny(array $tokens)
public function isNextTokenAny(array $types)
{
return $this->lookahead !== null && in_array($this->lookahead['type'], $tokens, true);
return $this->lookahead !== null && in_array($this->lookahead['type'], $types, 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