Skip to content

Commit 231e318

Browse files
authored
Improve static keyword conflict resolution in @method
1 parent c23674d commit 231e318

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Parser/PhpDocParser.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -924,20 +924,24 @@ private function parsePropertyTagValue(TokenIterator $tokens): Ast\PhpDoc\Proper
924924

925925
private function parseMethodTagValue(TokenIterator $tokens): Ast\PhpDoc\MethodTagValueNode
926926
{
927-
$isStatic = $tokens->tryConsumeTokenValue('static');
928-
$startLine = $tokens->currentTokenLine();
929-
$startIndex = $tokens->currentTokenIndex();
930-
$returnTypeOrMethodName = $this->typeParser->parse($tokens);
927+
$staticKeywordOrReturnTypeOrMethodName = $this->typeParser->parse($tokens);
928+
929+
if ($staticKeywordOrReturnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode && $staticKeywordOrReturnTypeOrMethodName->name === 'static') {
930+
$isStatic = true;
931+
$returnTypeOrMethodName = $this->typeParser->parse($tokens);
932+
933+
} else {
934+
$isStatic = false;
935+
$returnTypeOrMethodName = $staticKeywordOrReturnTypeOrMethodName;
936+
}
931937

932938
if ($tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
933939
$returnType = $returnTypeOrMethodName;
934940
$methodName = $tokens->currentTokenValue();
935941
$tokens->next();
936942

937943
} elseif ($returnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode) {
938-
$returnType = $isStatic
939-
? $this->typeParser->enrichWithAttributes($tokens, new Ast\Type\IdentifierTypeNode('static'), $startLine, $startIndex)
940-
: null;
944+
$returnType = $isStatic ? $staticKeywordOrReturnTypeOrMethodName : null;
941945
$methodName = $returnTypeOrMethodName->name;
942946
$isStatic = false;
943947

tests/PHPStan/Parser/PhpDocParserTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -2626,6 +2626,26 @@ public function provideMethodTagsData(): Iterator
26262626
),
26272627
]),
26282628
];
2629+
2630+
yield [
2631+
'OK non-static with return type that starts with static type',
2632+
'/** @method static|null foo() */',
2633+
new PhpDocNode([
2634+
new PhpDocTagNode(
2635+
'@method',
2636+
new MethodTagValueNode(
2637+
false,
2638+
new UnionTypeNode([
2639+
new IdentifierTypeNode('static'),
2640+
new IdentifierTypeNode('null'),
2641+
]),
2642+
'foo',
2643+
[],
2644+
''
2645+
)
2646+
),
2647+
]),
2648+
];
26292649
}
26302650

26312651

0 commit comments

Comments
 (0)