Skip to content

Commit b5fede3

Browse files
committed
Missing attributes for object shapes
1 parent cff97e9 commit b5fede3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/Parser/TypeParser.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,15 @@ private function parseObjectShape(TokenIterator $tokens): Ast\Type\ObjectShapeNo
713713
/** @phpstan-impure */
714714
private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectShapeItemNode
715715
{
716+
$startLine = $tokens->currentTokenLine();
717+
$startIndex = $tokens->currentTokenIndex();
718+
716719
$key = $this->parseObjectShapeKey($tokens);
717720
$optional = $tokens->tryConsumeTokenType(Lexer::TOKEN_NULLABLE);
718721
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
719722
$value = $this->parse($tokens);
720723

721-
return new Ast\Type\ObjectShapeItemNode($key, $optional, $value);
724+
return $this->enrichWithAttributes($tokens, new Ast\Type\ObjectShapeItemNode($key, $optional, $value), $startLine, $startIndex);
722725
}
723726

724727
/**
@@ -727,6 +730,9 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
727730
*/
728731
private function parseObjectShapeKey(TokenIterator $tokens)
729732
{
733+
$startLine = $tokens->currentTokenLine();
734+
$startIndex = $tokens->currentTokenIndex();
735+
730736
if ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) {
731737
if ($this->quoteAwareConstExprString) {
732738
$key = new Ast\ConstExpr\QuoteAwareConstExprStringNode(StringUnescaper::unescapeString($tokens->currentTokenValue()), Ast\ConstExpr\QuoteAwareConstExprStringNode::SINGLE_QUOTED);
@@ -748,7 +754,7 @@ private function parseObjectShapeKey(TokenIterator $tokens)
748754
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
749755
}
750756

751-
return $key;
757+
return $this->enrichWithAttributes($tokens, $key, $startLine, $startIndex);
752758
}
753759

754760
}

tests/PHPStan/Parser/TypeParserTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,64 @@ static function (TypeNode $typeNode): TypeNode {
21112111
],
21122112
],
21132113
];
2114+
2115+
yield [
2116+
'object{foo: int}',
2117+
[
2118+
[
2119+
static function (TypeNode $typeNode): TypeNode {
2120+
return $typeNode;
2121+
},
2122+
'object{foo: int}',
2123+
1,
2124+
1,
2125+
0,
2126+
6,
2127+
],
2128+
[
2129+
static function (ObjectShapeNode $typeNode): TypeNode {
2130+
return $typeNode->items[0];
2131+
},
2132+
'foo: int',
2133+
1,
2134+
1,
2135+
2,
2136+
5,
2137+
],
2138+
],
2139+
];
2140+
2141+
yield [
2142+
'object{}',
2143+
[
2144+
[
2145+
static function (TypeNode $typeNode): TypeNode {
2146+
return $typeNode;
2147+
},
2148+
'object{}',
2149+
1,
2150+
1,
2151+
0,
2152+
2,
2153+
],
2154+
],
2155+
];
2156+
2157+
yield [
2158+
'object{}[]',
2159+
[
2160+
[
2161+
static function (TypeNode $typeNode): TypeNode {
2162+
return $typeNode;
2163+
},
2164+
'object{}[]',
2165+
1,
2166+
1,
2167+
0,
2168+
4,
2169+
],
2170+
],
2171+
];
21142172
}
21152173

21162174
/**

0 commit comments

Comments
 (0)