From 129a63b3bc7caeb593c224c41f420675e63cfefc Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Fri, 22 Apr 2022 13:11:01 +0200 Subject: [PATCH] Extend allowed types for conditional for parameter --- src/Parser/TypeParser.php | 10 +++++-- tests/PHPStan/Parser/TypeParserTest.php | 37 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 95825ca..b2f1072 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -259,15 +259,19 @@ class TypeParser $tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER); } - $targetType = $this->parseAtomic($tokens); + $targetType = $this->parse($tokens); + $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); $tokens->consumeTokenType(Lexer::TOKEN_NULLABLE); + $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); - $ifType = $this->parseAtomic($tokens); + $ifType = $this->parse($tokens); + $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); $tokens->consumeTokenType(Lexer::TOKEN_COLON); + $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); - $elseType = $this->parseAtomic($tokens); + $elseType = $this->parse($tokens); return new Ast\Type\ConditionalTypeForParameterNode($parameterName, $targetType, $ifType, $elseType, $negated); } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 8d9ebf2..46c342c 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -12,6 +12,7 @@ use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode; +use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeForParameterNode; use PHPStan\PhpDocParser\Ast\Type\ConditionalTypeNode; use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode; use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; @@ -1210,6 +1211,42 @@ class TypeParserTest extends TestCase false ), ], + [ + '($foo is Bar|Baz ? never : int|string)', + new ConditionalTypeForParameterNode( + '$foo', + new UnionTypeNode([ + new IdentifierTypeNode('Bar'), + new IdentifierTypeNode('Baz'), + ]), + new IdentifierTypeNode('never'), + new UnionTypeNode([ + new IdentifierTypeNode('int'), + new IdentifierTypeNode('string'), + ]), + false + ), + ], + [ + '(' . PHP_EOL . + ' $foo is Bar|Baz' . PHP_EOL . + ' ? never' . PHP_EOL . + ' : int|string' . PHP_EOL . + ')', + new ConditionalTypeForParameterNode( + '$foo', + new UnionTypeNode([ + new IdentifierTypeNode('Bar'), + new IdentifierTypeNode('Baz'), + ]), + new IdentifierTypeNode('never'), + new UnionTypeNode([ + new IdentifierTypeNode('int'), + new IdentifierTypeNode('string'), + ]), + false + ), + ], ]; }