Skip to content

Commit

Permalink
Improve static keyword conflict resolution in @method
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Feb 23, 2024
1 parent c23674d commit 231e318
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,20 +924,24 @@ private function parsePropertyTagValue(TokenIterator $tokens): Ast\PhpDoc\Proper

private function parseMethodTagValue(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueNode
{
$isStatic = $tokens->tryConsumeTokenValue('static');
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();
$returnTypeOrMethodName = $this->typeParser->parse($tokens);
$staticKeywordOrReturnTypeOrMethodName = $this->typeParser->parse($tokens);

if ($staticKeywordOrReturnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode && $staticKeywordOrReturnTypeOrMethodName->name === 'static') {
$isStatic = true;
$returnTypeOrMethodName = $this->typeParser->parse($tokens);

} else {
$isStatic = false;
$returnTypeOrMethodName = $staticKeywordOrReturnTypeOrMethodName;
}

if ($tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
$returnType = $returnTypeOrMethodName;
$methodName = $tokens->currentTokenValue();
$tokens->next();

} elseif ($returnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode) {
$returnType = $isStatic
? $this->typeParser->enrichWithAttributes($tokens, new Ast\Type\IdentifierTypeNode('static'), $startLine, $startIndex)
: null;
$returnType = $isStatic ? $staticKeywordOrReturnTypeOrMethodName : null;
$methodName = $returnTypeOrMethodName->name;
$isStatic = false;

Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2626,6 +2626,26 @@ public function provideMethodTagsData(): Iterator
),
]),
];

yield [
'OK non-static with return type that starts with static type',
'/** @method static|null foo() */',
new PhpDocNode([
new PhpDocTagNode(
'@method',
new MethodTagValueNode(
false,
new UnionTypeNode([
new IdentifierTypeNode('static'),
new IdentifierTypeNode('null'),
]),
'foo',
[],
''
)
),
]),
];
}


Expand Down

0 comments on commit 231e318

Please sign in to comment.