Skip to content

Commit

Permalink
Allow conditional type in callable return type
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen authored Sep 26, 2023
1 parent 9f854d2 commit bcad8d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
return $this->parseNullable($tokens);

} elseif ($tokens->tryConsumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
$type = $this->parse($tokens);
$type = $this->subParse($tokens);
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_PARENTHESES);
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,18 @@ public function provideParseData(): array
]),
]),
],
[
'Closure(Container):($serviceId is class-string<TService> ? TService : mixed)',
new CallableTypeNode(new IdentifierTypeNode('Closure'), [
new CallableTypeParameterNode(new IdentifierTypeNode('Container'), false, false, '', false),
], new ConditionalTypeForParameterNode(
'$serviceId',
new GenericTypeNode(new IdentifierTypeNode('class-string'), [new IdentifierTypeNode('TService')], ['invariant']),
new IdentifierTypeNode('TService'),
new IdentifierTypeNode('mixed'),
false
)),
],
];
}

Expand Down

0 comments on commit bcad8d9

Please sign in to comment.