Skip to content

Support constant fetch in array key #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Ast/Type/ArrayShapeItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeAttributes;
use function sprintf;
Expand All @@ -13,15 +14,15 @@ class ArrayShapeItemNode implements Node

use NodeAttributes;

/** @var ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null */
/** @var ConstExprIntegerNode|ConstExprStringNode|ConstFetchNode|IdentifierTypeNode|null */
public $keyName;

public bool $optional;

public TypeNode $valueType;

/**
* @param ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null $keyName
* @param ConstExprIntegerNode|ConstExprStringNode|ConstFetchNode|IdentifierTypeNode|null $keyName
*/
public function __construct($keyName, bool $optional, TypeNode $valueType)
{
Expand Down
13 changes: 11 additions & 2 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape

/**
* @phpstan-impure
* @return Ast\ConstExpr\ConstExprIntegerNode|Ast\ConstExpr\ConstExprStringNode|Ast\Type\IdentifierTypeNode
* @return Ast\ConstExpr\ConstExprIntegerNode|Ast\ConstExpr\ConstExprStringNode|Ast\ConstExpr\ConstFetchNode|Ast\Type\IdentifierTypeNode
*/
private function parseArrayShapeKey(TokenIterator $tokens)
{
Expand All @@ -991,8 +991,17 @@ private function parseArrayShapeKey(TokenIterator $tokens)
$tokens->next();

} else {
$key = new Ast\Type\IdentifierTypeNode($tokens->currentTokenValue());
$identifier = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

if ($tokens->tryConsumeTokenType(Lexer::TOKEN_DOUBLE_COLON)) {
$classConstantName = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);

$key = new Ast\ConstExpr\ConstFetchNode($identifier, $classConstantName);
} else {
$key = new Ast\Type\IdentifierTypeNode($identifier);
}
}

return $this->enrichWithAttributes(
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Ast/ToString/TypeToStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
Expand Down Expand Up @@ -75,6 +76,13 @@ public static function provideArrayCases(): Generator
new ArrayShapeItemNode(new ConstExprIntegerNode('1'), false, new IdentifierTypeNode('Baz')),
]),
],
[
'array{Foo::BAR: Foo, Bar::FOO?: Bar}',
ArrayShapeNode::createSealed([
new ArrayShapeItemNode(new ConstFetchNode('Foo', 'BAR'), false, new IdentifierTypeNode('Foo')),
new ArrayShapeItemNode(new ConstFetchNode('Bar', 'FOO'), true, new IdentifierTypeNode('Bar')),
]),
],
['list{}', ArrayShapeNode::createSealed([], 'list')],
['list{...}', ArrayShapeNode::createUnsealed([], null, 'list')],
[
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,16 @@ public function provideParseData(): array
new IdentifierTypeNode('MongoCollection'),
Lexer::TOKEN_OPEN_ANGLE_BRACKET,
],
[
'array{Foo::BAR: int}',
ArrayShapeNode::createSealed([
new ArrayShapeItemNode(
new ConstFetchNode('Foo', 'BAR'),
false,
new IdentifierTypeNode('int'),
),
]),
],
];
}

Expand Down
Loading