From 030195a395e8e7f3f1147ed866fe834c3b51a9f0 Mon Sep 17 00:00:00 2001 From: orklah Date: Tue, 4 Jan 2022 10:48:29 +0100 Subject: [PATCH 1/2] parse array{} into an empty array --- src/Psalm/Internal/Type/ParseTreeCreator.php | 7 +++++++ src/Psalm/Internal/Type/TypeParser.php | 4 ++-- tests/TypeParseTest.php | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Type/ParseTreeCreator.php b/src/Psalm/Internal/Type/ParseTreeCreator.php index edad53cb928..15256e5dedc 100644 --- a/src/Psalm/Internal/Type/ParseTreeCreator.php +++ b/src/Psalm/Internal/Type/ParseTreeCreator.php @@ -751,6 +751,13 @@ private function handleValue(array $type_token): void $new_parent ); ++$this->t; + + $nexter_token = $this->t + 1 < $this->type_token_count ? $this->type_tokens[$this->t + 1] : null; + + if ($nexter_token !== null && $nexter_token[0] === '}') { + $new_leaf->terminated = true; + ++$this->t; + } break; case '(': diff --git a/src/Psalm/Internal/Type/TypeParser.php b/src/Psalm/Internal/Type/TypeParser.php index 26e2658ca4b..d7b9f8268e6 100644 --- a/src/Psalm/Internal/Type/TypeParser.php +++ b/src/Psalm/Internal/Type/TypeParser.php @@ -1235,7 +1235,7 @@ private static function getTypeFromIndexAccessTree( /** * @param array> $template_type_map * @param array $type_aliases - * @return TCallableKeyedArray|TKeyedArray|TObjectWithProperties + * @return TCallableKeyedArray|TKeyedArray|TObjectWithProperties|TArray * @throws TypeParseTreeException */ private static function getTypeFromKeyedArrayTree( @@ -1314,7 +1314,7 @@ private static function getTypeFromKeyedArrayTree( } if (!$properties) { - throw new TypeParseTreeException('No properties supplied for TKeyedArray'); + return new TArray([Type::getNever(), Type::getNever()]); } if ($type === 'object') { diff --git a/tests/TypeParseTest.php b/tests/TypeParseTest.php index 30ea08a39a0..d2f29297ecc 100644 --- a/tests/TypeParseTest.php +++ b/tests/TypeParseTest.php @@ -893,6 +893,14 @@ public function testSingleLiteralString(): void ); } + public function testEmptyArrayShape(): void + { + $this->assertSame( + 'array', + (string)Type::parseString('array{}') + ); + } + public function testSingleLiteralInt(): void { $this->assertSame( From 9d3a51db6a06a7a5ffc8efa95a17db44f47211dd Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 5 Jan 2022 23:02:35 +0100 Subject: [PATCH 2/2] remove obsolete test and fix a missing exception --- src/Psalm/Internal/Type/ParseTreeCreator.php | 3 +++ tests/AnnotationTest.php | 8 -------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Psalm/Internal/Type/ParseTreeCreator.php b/src/Psalm/Internal/Type/ParseTreeCreator.php index 15256e5dedc..0ee5d9b329f 100644 --- a/src/Psalm/Internal/Type/ParseTreeCreator.php +++ b/src/Psalm/Internal/Type/ParseTreeCreator.php @@ -757,7 +757,10 @@ private function handleValue(array $type_token): void if ($nexter_token !== null && $nexter_token[0] === '}') { $new_leaf->terminated = true; ++$this->t; + } elseif ($nexter_token === null) { + throw new TypeParseTreeException('Unclosed bracket in keyed array'); } + break; case '(': diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index 2f7ec306d08..506524aa215 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -1490,14 +1490,6 @@ public function bar() { ', 'error_message' => 'UndefinedDocblockClass', ], - 'preventBadTKeyedArrayFormat' => [ - ' 'InvalidDocblock', - ], 'noPhpStormAnnotationsThankYou' => [ '