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

Support all atomic types as nullable types #129

Merged
merged 2 commits into from Jun 8, 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 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