Skip to content

Commit

Permalink
Support all atomic types as nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Jun 8, 2022
1 parent 7c621a2 commit 1e355a3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion doc/grammars/type.abnf
Expand Up @@ -20,7 +20,7 @@ Conditional
= 1*ByteHorizontalWs TokenIs [TokenNot] Atomic TokenNullable Atomic TokenColon Atomic

Nullable
= TokenNullable TokenIdentifier [Generic]
= TokenNullable Atomic

Atomic
= TokenIdentifier [Generic / Callable / Array]
Expand Down
14 changes: 1 addition & 13 deletions src/Parser/TypeParser.php
Expand Up @@ -282,19 +282,7 @@ private function parseNullable(TokenIterator $tokens): Ast\Type\TypeNode
{
$tokens->consumeTokenType(Lexer::TOKEN_NULLABLE);

$type = new Ast\Type\IdentifierTypeNode($tokens->currentTokenValue());
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) {
$type = $this->parseGeneric($tokens, $type);

} elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $type);
}

if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
}
$type = $this->parseAtomic($tokens);

return new Ast\Type\NullableTypeNode($type);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Expand Up @@ -1255,6 +1255,17 @@ public function provideParseData(): array
false
),
],
[
'?Currency::CURRENCY_*',
new NullableTypeNode(
new ConstTypeNode(
new ConstFetchNode(
'Currency',
'CURRENCY_*'
)
)
),
],
];
}

Expand Down

0 comments on commit 1e355a3

Please sign in to comment.