Extend allowed types for conditional for parameter

This commit is contained in:
Richard van Velzen 2022-04-22 13:11:01 +02:00 committed by GitHub
parent d8e9fd97ca
commit 129a63b3bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -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);
}

View File

@ -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
),
],
];
}