Skip to content

Commit d60fa73

Browse files
committed
CallableTypeNode - support object shape in return type
1 parent 2ebed2c commit d60fa73

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

Diff for: src/Parser/TypeParser.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,17 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo
542542
)
543543
);
544544

545-
} elseif (in_array($type->name, ['array', 'list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
546-
$type = $this->parseArrayShape($tokens, $this->enrichWithAttributes(
547-
$tokens,
548-
$type,
549-
$startLine,
550-
$startIndex
551-
), $type->name);
545+
} elseif (in_array($type->name, ['array', 'list', 'object'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
546+
if ($type->name === 'object') {
547+
$type = $this->parseObjectShape($tokens);
548+
} else {
549+
$type = $this->parseArrayShape($tokens, $this->enrichWithAttributes(
550+
$tokens,
551+
$type,
552+
$startLine,
553+
$startIndex
554+
), $type->name);
555+
}
552556
}
553557
}
554558

Diff for: tests/PHPStan/Parser/TypeParserTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,24 @@ public function provideParseData(): array
19391939
'callable(): ?int',
19401940
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new NullableTypeNode(new IdentifierTypeNode('int'))),
19411941
],
1942+
[
1943+
'callable(): object{foo: int}',
1944+
new CallableTypeNode(new IdentifierTypeNode('callable'), [], new ObjectShapeNode([
1945+
new ObjectShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('int')),
1946+
])),
1947+
],
1948+
[
1949+
'callable(): object{foo: int}[]',
1950+
new CallableTypeNode(
1951+
new IdentifierTypeNode('callable'),
1952+
[],
1953+
new ArrayTypeNode(
1954+
new ObjectShapeNode([
1955+
new ObjectShapeItemNode(new IdentifierTypeNode('foo'), false, new IdentifierTypeNode('int')),
1956+
])
1957+
)
1958+
),
1959+
],
19421960
];
19431961
}
19441962

0 commit comments

Comments
 (0)