Skip to content

Commit

Permalink
Do not cast null to array
Browse files Browse the repository at this point in the history
The possibility that null could be returned was overlooked in
2286f7b .
  • Loading branch information
greg0ire committed Dec 12, 2022
1 parent 3587ab5 commit fd04499
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/Doctrine/Common/Annotations/DocLexer.php
Expand Up @@ -130,10 +130,14 @@ protected function getType(&$value)
}

/** @return array{value: int|string, type:self::T_*|null, position:int} */
public function peek(): array
public function peek(): ?array
{
$token = parent::peek();

if ($token === null) {
return null;
}

return (array) $token;
}
}
2 changes: 0 additions & 2 deletions phpstan.neon
Expand Up @@ -22,5 +22,3 @@ parameters:

# That tag is empty on purpose
- '#PHPDoc tag @var has invalid value \(\)\: Unexpected token "\*/", expected type at offset 9#'
# Backwards-compatibility
- '#^Return type.*of method.*DocLexer::peek.*should be compatible.*$#'
5 changes: 5 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php
Expand Up @@ -320,4 +320,9 @@ public function testTokenAdjacency(): void
self::assertFalse($lexer->nextTokenIsAdjacent());
self::assertFalse($lexer->moveNext());
}

public function testItReturnsNullWhenThereIsNothingToParse(): void
{
self::assertNull((new DocLexer())->peek());
}
}

0 comments on commit fd04499

Please sign in to comment.