Skip to content

Commit

Permalink
Add psalm assertions
Browse files Browse the repository at this point in the history
These should help avoiding null checks in downstream libraries.
  • Loading branch information
greg0ire committed Dec 11, 2022
1 parent 3cf140b commit 10c22f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/AbstractLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function getInputUntilPosition($position)
* @param T $type
*
* @return bool
*
* @psalm-assert-if-true !null $this->lookahead
*/
public function isNextToken($type)
{
Expand All @@ -159,6 +161,8 @@ public function isNextToken($type)
* @param list<T> $types
*
* @return bool
*
* @psalm-assert-if-true !null $this->lookahead
*/
public function isNextTokenAny(array $types)
{
Expand All @@ -169,6 +173,9 @@ public function isNextTokenAny(array $types)
* Moves to the next token in the input string.
*
* @return bool
*
* @psalm-assert-if-true !null $this->lookahead
* @psalm-assert-if-false null $this->lookahead
*/
public function moveNext()
{
Expand Down
9 changes: 0 additions & 9 deletions tests/AbstractLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Lexer\Token;
use PHPUnit\Framework\TestCase;

use function array_map;
use function assert;
use function count;
use function setlocale;
Expand Down Expand Up @@ -219,19 +218,12 @@ public function testIsNextToken(string $input, array $expectedTokens): void
*/
public function testIsNextTokenAny(string $input, array $expectedTokens): void
{
$allTokenTypes = array_map(static function ($token): string {
assert($token->type !== null);

return $token->type;
}, $expectedTokens);

$this->concreteLexer->setInput($input);

$this->concreteLexer->moveNext();
for ($i = 0; $i < count($expectedTokens); $i++) {
assert($expectedTokens[$i]->type !== null);
$this->assertTrue($this->concreteLexer->isNextTokenAny([$expectedTokens[$i]->type]));
$this->assertTrue($this->concreteLexer->isNextTokenAny($allTokenTypes));
$this->concreteLexer->moveNext();
}
}
Expand Down Expand Up @@ -296,7 +288,6 @@ public function testMarkerAnnotationLocaleTr(): void
self::assertNull($mutableLexer->lookahead);
self::assertTrue($mutableLexer->moveNext());
self::assertNull($mutableLexer->token);
self::assertNotNull($mutableLexer->lookahead);
self::assertEquals('@', $mutableLexer->lookahead->value);
self::assertTrue($mutableLexer->moveNext());
self::assertNotNull($mutableLexer->token);
Expand Down

0 comments on commit 10c22f5

Please sign in to comment.