diff --git a/src/Ast/Type/ArrayShapeNode.php b/src/Ast/Type/ArrayShapeNode.php index 3778436d..41941f80 100644 --- a/src/Ast/Type/ArrayShapeNode.php +++ b/src/Ast/Type/ArrayShapeNode.php @@ -8,6 +8,9 @@ class ArrayShapeNode implements TypeNode { + public const KIND_ARRAY = 'array'; + public const KIND_LIST = 'list'; + use NodeAttributes; /** @var ArrayShapeItemNode[] */ @@ -16,10 +19,17 @@ class ArrayShapeNode implements TypeNode /** @var bool */ public $sealed; - public function __construct(array $items, bool $sealed = true) + /** @var self::KIND_* */ + public $kind; + + /** + * @param self::KIND_* $kind + */ + public function __construct(array $items, bool $sealed = true, string $kind = self::KIND_ARRAY) { $this->items = $items; $this->sealed = $sealed; + $this->kind = $kind; } @@ -31,7 +41,7 @@ public function __toString(): string $items[] = '...'; } - return 'array{' . implode(', ', $items) . '}'; + return $this->kind . '{' . implode(', ', $items) . '}'; } } diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index f45aafdc..993bf873 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -5,6 +5,7 @@ use LogicException; use PHPStan\PhpDocParser\Ast; use PHPStan\PhpDocParser\Lexer\Lexer; +use function in_array; use function strpos; use function trim; @@ -123,8 +124,8 @@ private function parseAtomic(TokenIterator $tokens): Ast\Type\TypeNode } elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { $type = $this->tryParseArrayOrOffsetAccess($tokens, $type); - } elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { - $type = $this->parseArrayShape($tokens, $type); + } elseif (in_array($type->name, ['array', 'list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { + $type = $this->parseArrayShape($tokens, $type, $type->name); if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { $type = $this->tryParseArrayOrOffsetAccess($tokens, $type); @@ -439,8 +440,8 @@ private function parseCallableReturnType(TokenIterator $tokens): Ast\Type\TypeNo if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET)) { $type = $this->parseGeneric($tokens, $type); - } elseif ($type->name === 'array' && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { - $type = $this->parseArrayShape($tokens, $type); + } elseif (in_array($type->name, ['array', 'list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { + $type = $this->parseArrayShape($tokens, $type, $type->name); } } @@ -499,8 +500,11 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ } - /** @phpstan-impure */ - private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type): Ast\Type\ArrayShapeNode + /** + * @phpstan-impure + * @param Ast\Type\ArrayShapeNode::KIND_* $kind + */ + private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type, string $kind): Ast\Type\ArrayShapeNode { $tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET); @@ -528,7 +532,7 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type) $tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); $tokens->consumeTokenType(Lexer::TOKEN_CLOSE_CURLY_BRACKET); - return new Ast\Type\ArrayShapeNode($items, $sealed); + return new Ast\Type\ArrayShapeNode($items, $sealed, $kind); } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index 484823a8..1b7dc067 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -668,6 +668,28 @@ public function provideParseData(): array Lexer::TOKEN_CLOSE_CURLY_BRACKET ), ], + [ + 'list{ + int, + string + }', + new ArrayShapeNode( + [ + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('int') + ), + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('string') + ), + ], + true, + ArrayShapeNode::KIND_LIST + ), + ], [ 'callable(): Foo', new CallableTypeNode(