Skip to content

Commit

Permalink
Missing attributes for object shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 24, 2023
1 parent cff97e9 commit b5fede3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,12 +713,15 @@ private function parseObjectShape(TokenIterator $tokens): Ast\Type\ObjectShapeNo
/** @phpstan-impure */
private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectShapeItemNode
{
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();

$key = $this->parseObjectShapeKey($tokens);
$optional = $tokens->tryConsumeTokenType(Lexer::TOKEN_NULLABLE);
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
$value = $this->parse($tokens);

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

/**
Expand All @@ -727,6 +730,9 @@ private function parseObjectShapeItem(TokenIterator $tokens): Ast\Type\ObjectSha
*/
private function parseObjectShapeKey(TokenIterator $tokens)
{
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();

if ($tokens->isCurrentTokenType(Lexer::TOKEN_SINGLE_QUOTED_STRING)) {
if ($this->quoteAwareConstExprString) {
$key = new Ast\ConstExpr\QuoteAwareConstExprStringNode(StringUnescaper::unescapeString($tokens->currentTokenValue()), Ast\ConstExpr\QuoteAwareConstExprStringNode::SINGLE_QUOTED);
Expand All @@ -748,7 +754,7 @@ private function parseObjectShapeKey(TokenIterator $tokens)
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
}

return $key;
return $this->enrichWithAttributes($tokens, $key, $startLine, $startIndex);
}

}
58 changes: 58 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,64 @@ static function (TypeNode $typeNode): TypeNode {
],
],
];

yield [
'object{foo: int}',
[
[
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'object{foo: int}',
1,
1,
0,
6,
],
[
static function (ObjectShapeNode $typeNode): TypeNode {
return $typeNode->items[0];
},
'foo: int',
1,
1,
2,
5,
],
],
];

yield [
'object{}',
[
[
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'object{}',
1,
1,
0,
2,
],
],
];

yield [
'object{}[]',
[
[
static function (TypeNode $typeNode): TypeNode {
return $typeNode;
},
'object{}[]',
1,
1,
0,
4,
],
],
];
}

/**
Expand Down

0 comments on commit b5fede3

Please sign in to comment.