Skip to content

Commit cff97e9

Browse files
committed
Missing attributes for array shapes
1 parent 3416dc6 commit cff97e9

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/Parser/TypeParser.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
611611
/** @phpstan-impure */
612612
private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShapeItemNode
613613
{
614+
$startLine = $tokens->currentTokenLine();
615+
$startIndex = $tokens->currentTokenIndex();
614616
try {
615617
$tokens->pushSavePoint();
616618
$key = $this->parseArrayShapeKey($tokens);
@@ -619,12 +621,22 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
619621
$value = $this->parse($tokens);
620622
$tokens->dropSavePoint();
621623

622-
return new Ast\Type\ArrayShapeItemNode($key, $optional, $value);
624+
return $this->enrichWithAttributes(
625+
$tokens,
626+
new Ast\Type\ArrayShapeItemNode($key, $optional, $value),
627+
$startLine,
628+
$startIndex
629+
);
623630
} catch (ParserException $e) {
624631
$tokens->rollback();
625632
$value = $this->parse($tokens);
626633

627-
return new Ast\Type\ArrayShapeItemNode(null, false, $value);
634+
return $this->enrichWithAttributes(
635+
$tokens,
636+
new Ast\Type\ArrayShapeItemNode(null, false, $value),
637+
$startLine,
638+
$startIndex
639+
);
628640
}
629641
}
630642

@@ -634,6 +646,9 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
634646
*/
635647
private function parseArrayShapeKey(TokenIterator $tokens)
636648
{
649+
$startIndex = $tokens->currentTokenIndex();
650+
$startLine = $tokens->currentTokenLine();
651+
637652
if ($tokens->isCurrentTokenType(Lexer::TOKEN_INTEGER)) {
638653
$key = new Ast\ConstExpr\ConstExprIntegerNode($tokens->currentTokenValue());
639654
$tokens->next();
@@ -660,7 +675,12 @@ private function parseArrayShapeKey(TokenIterator $tokens)
660675
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
661676
}
662677

663-
return $key;
678+
return $this->enrichWithAttributes(
679+
$tokens,
680+
$key,
681+
$startLine,
682+
$startIndex
683+
);
664684
}
665685

666686
/**

tests/PHPStan/Parser/TypeParserTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,48 @@ static function (TypeNode $typeNode): TypeNode {
20692069
],
20702070
],
20712071
];
2072+
2073+
yield [
2074+
'array{foo: int}',
2075+
[
2076+
[
2077+
static function (TypeNode $typeNode): TypeNode {
2078+
return $typeNode;
2079+
},
2080+
'array{foo: int}',
2081+
1,
2082+
1,
2083+
0,
2084+
6,
2085+
],
2086+
[
2087+
static function (ArrayShapeNode $typeNode): TypeNode {
2088+
return $typeNode->items[0];
2089+
},
2090+
'foo: int',
2091+
1,
2092+
1,
2093+
2,
2094+
5,
2095+
],
2096+
],
2097+
];
2098+
2099+
yield [
2100+
'array{}',
2101+
[
2102+
[
2103+
static function (TypeNode $typeNode): TypeNode {
2104+
return $typeNode;
2105+
},
2106+
'array{}',
2107+
1,
2108+
1,
2109+
0,
2110+
2,
2111+
],
2112+
],
2113+
];
20722114
}
20732115

20742116
/**

0 commit comments

Comments
 (0)