From 7978df29468015608ea10258b7f0c4fb6817ff2f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 5 Nov 2022 13:29:17 +0100 Subject: [PATCH] Add support for strict arrays and list shapes --- src/Parser/TypeParser.php | 5 ++- tests/PHPStan/Parser/TypeParserTest.php | 54 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index 1b07c74..1dfd842 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -5,6 +5,7 @@ namespace PHPStan\PhpDocParser\Parser; use LogicException; use PHPStan\PhpDocParser\Ast; use PHPStan\PhpDocParser\Lexer\Lexer; +use function in_array; use function strpos; use function trim; @@ -123,7 +124,7 @@ class TypeParser } 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()) { + } elseif (in_array($type->name, ['array', 'list', 'strict-array', 'strict-list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { $type = $this->parseArrayShape($tokens, $type); if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) { @@ -409,7 +410,7 @@ class TypeParser 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()) { + } elseif (in_array($type->name, ['array', 'list', 'strict-array', 'strict-list'], true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) { $type = $this->parseArrayShape($tokens, $type); } } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index da21df9..b65a263 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -592,6 +592,60 @@ class TypeParserTest extends TestCase ), ]), ], + [ + 'strict-array{ + a: int, + b: string + }', + new ArrayShapeNode([ + new ArrayShapeItemNode( + new IdentifierTypeNode('a'), + false, + new IdentifierTypeNode('int') + ), + new ArrayShapeItemNode( + new IdentifierTypeNode('b'), + false, + new IdentifierTypeNode('string') + ), + ]), + ], + [ + 'strict-list{ + int, + string + }', + new ArrayShapeNode([ + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('int') + ), + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('string') + ), + ]), + ], + [ + 'list{ + int, + string + }', + new ArrayShapeNode([ + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('int') + ), + new ArrayShapeItemNode( + null, + false, + new IdentifierTypeNode('string') + ), + ]), + ], [ 'callable(): Foo', new CallableTypeNode(