Skip to content

Commit

Permalink
Allow parsing class constants with more than one wildcard in the cons…
Browse files Browse the repository at this point in the history
…tant name
  • Loading branch information
Seldaek authored and ondrejmirtes committed Sep 12, 2021
1 parent fac8615 commit 816e826
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/Parser/ConstExprParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,29 @@ public function parse(TokenIterator $tokens, bool $trimStrings = false): Ast\Con

if ($tokens->tryConsumeTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
$classConstantName = '';
if ($tokens->currentTokenType() === Lexer::TOKEN_IDENTIFIER) {
$classConstantName .= $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_WILDCARD)) {
$lastType = null;
while (true) {
if ($lastType !== Lexer::TOKEN_IDENTIFIER && $tokens->currentTokenType() === Lexer::TOKEN_IDENTIFIER) {
$classConstantName .= $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
$lastType = Lexer::TOKEN_IDENTIFIER;

continue;
}

if ($lastType !== Lexer::TOKEN_WILDCARD && $tokens->tryConsumeTokenType(Lexer::TOKEN_WILDCARD)) {
$classConstantName .= '*';
$lastType = Lexer::TOKEN_WILDCARD;

continue;
}
} else {
$tokens->consumeTokenType(Lexer::TOKEN_WILDCARD);
$classConstantName .= '*';

if ($lastType === null) {
// trigger parse error if nothing valid was consumed
$tokens->consumeTokenType(Lexer::TOKEN_WILDCARD);
}

break;
}

return new Ast\ConstExpr\ConstFetchNode($identifier, $classConstantName);
Expand Down
19 changes: 17 additions & 2 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,22 @@ public function provideParseData(): array
'Foo::FOO_*',
new ConstTypeNode(new ConstFetchNode('Foo', 'FOO_*')),
],
[
'Foo::FOO_*BAR',
new ConstTypeNode(new ConstFetchNode('Foo', 'FOO_*BAR')),
],
[
'Foo::*FOO*',
new ConstTypeNode(new ConstFetchNode('Foo', '*FOO*')),
],
[
'Foo::A*B*C',
new ConstTypeNode(new ConstFetchNode('Foo', 'A*B*C')),
],
[
'self::*BAR',
new ConstTypeNode(new ConstFetchNode('self', '*BAR')),
],
[
'Foo::*',
new ConstTypeNode(new ConstFetchNode('Foo', '*')),
Expand All @@ -869,8 +885,7 @@ public function provideParseData(): array
],
[
'Foo::*a',
new ConstTypeNode(new ConstFetchNode('Foo', '*')), // fails later in PhpDocParser
Lexer::TOKEN_IDENTIFIER,
new ConstTypeNode(new ConstFetchNode('Foo', '*a')),
],
[
'( "foo" | Foo::FOO_* )',
Expand Down

0 comments on commit 816e826

Please sign in to comment.