From 0c951364eeb16fb70997c0995806698e2be48ec0 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 8 Jun 2022 15:17:12 +0200 Subject: [PATCH 1/2] Support all atomic types as nullable types --- doc/grammars/type.abnf | 2 +- src/Parser/TypeParser.php | 14 +------------- tests/PHPStan/Parser/TypeParserTest.php | 11 +++++++++++ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/doc/grammars/type.abnf b/doc/grammars/type.abnf index d28a768b..5af5be1f 100644 --- a/doc/grammars/type.abnf +++ b/doc/grammars/type.abnf @@ -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] diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index cdfd9a29..170b2581 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -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); } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 52402048..eaf3d9b0 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1255,6 +1255,17 @@ public function provideParseData(): array false ), ], + [ + '?Currency::CURRENCY_*', + new NullableTypeNode( + new ConstTypeNode( + new ConstFetchNode( + 'Currency', + 'CURRENCY_*' + ) + ) + ), + ] ]; } From 07514e0952571074d11feab072bc4138ff7a119e Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 8 Jun 2022 15:18:02 +0200 Subject: [PATCH 2/2] Fix CS --- tests/PHPStan/Parser/TypeParserTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index eaf3d9b0..8289488b 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -1265,7 +1265,7 @@ public function provideParseData(): array ) ) ), - ] + ], ]; }