Skip to content

Commit

Permalink
Extend allowed types for conditional for parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Apr 22, 2022
1 parent d8e9fd9 commit 129a63b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,19 @@ private function parseConditionalForParameter(TokenIterator $tokens, string $par
$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);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -1210,6 +1211,42 @@ public function provideParseData(): array
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
),
],
];
}

Expand Down

0 comments on commit 129a63b

Please sign in to comment.