Skip to content

Commit

Permalink
CallableTypeNode - support object shape in return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 2, 2023
1 parent 2ebed2c commit d60fa73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,17 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
)
);

} elseif (in_array($type->name, ['array', 'list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
$type = $this->parseArrayShape($tokens, $this->enrichWithAttributes(
$tokens,
$type,
$startLine,
$startIndex
), $type->name);
} elseif (in_array($type->name, ['array', 'list', 'object'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
if ($type->name === 'object') {
$type = $this->parseObjectShape($tokens);
} else {
$type = $this->parseArrayShape($tokens, $this->enrichWithAttributes(
$tokens,
$type,
$startLine,
$startIndex
), $type->name);
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,24 @@ public function provideParseData(): array
'callable(): ?int',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new NullableTypeNode(new IdentifierTypeNode('int'))),
],
[
'callable(): object{foo: int}',
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new ObjectShapeNode([
new ObjectShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('int')),
])),
],
[
'callable(): object{foo: int}[]',
new CallableTypeNode(
new IdentifierTypeNode('callable'),
[],
new ArrayTypeNode(
new ObjectShapeNode([
new ObjectShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('int')),
])
)
),
],
];
}

Expand Down

0 comments on commit d60fa73

Please sign in to comment.