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

Allow doctrine/lexer 2 #460

Merged
merged 1 commit into from Dec 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -34,7 +34,7 @@
"require": {
"php": "^7.1 || ^8.0",
"ext-tokenizer": "*",
"doctrine/lexer": "1.*",
"doctrine/lexer": "^1 || ^2",
"psr/cache": "^1 || ^2 || ^3"
},
"require-dev": {
Expand Down
14 changes: 12 additions & 2 deletions lib/Doctrine/Common/Annotations/DocLexer.php
Expand Up @@ -15,6 +15,8 @@

/**
* Simple lexer for docblock annotations.
*
* @template-extends AbstractLexer<DocLexer::T_*>
*/
final class DocLexer extends AbstractLexer
{
Expand All @@ -39,7 +41,7 @@ final class DocLexer extends AbstractLexer
public const T_COLON = 112;
public const T_MINUS = 113;

/** @var array<string, int> */
/** @var array<string, self::T*> */
protected $noCase = [
'@' => self::T_AT,
',' => self::T_COMMA,
Expand All @@ -53,7 +55,7 @@ final class DocLexer extends AbstractLexer
'\\' => self::T_NAMESPACE_SEPARATOR,
];

/** @var array<string, int> */
/** @var array<string, self::T*> */
protected $withCase = [
'true' => self::T_TRUE,
'false' => self::T_FALSE,
Expand Down Expand Up @@ -126,4 +128,12 @@ protected function getType(&$value)

return $type;
}

/** @return array{value: int|string, type:self::T_*|null, position:int} */
public function peek(): array
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to also override glimpse since it returns whatever peek() returns.

{
$token = parent::peek();

return (array) $token;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the array type always be forced here? In v1.13.x that was not the case because the method was inherited from the parent class. The docblock does not state that an empty array could be returned, but that is exactly what will be returned when the parent returns null right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we should not cast null to an array. Up for a PR?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #463 🎉

}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Expand Up @@ -22,3 +22,5 @@ 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.*$#'
2 changes: 2 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php
Expand Up @@ -19,9 +19,11 @@ public function testMarkerAnnotation(): void

self::assertTrue($lexer->moveNext());
self::assertNull($lexer->token);
self::assertNotNull($lexer->lookahead);
self::assertEquals('@', $lexer->lookahead['value']);

self::assertTrue($lexer->moveNext());
self::assertNotNull($lexer->token);
self::assertEquals('@', $lexer->token['value']);
self::assertEquals('Name', $lexer->lookahead['value']);

Expand Down