From e210a6e561ff742ac0a0f381a6190c9f81dddc92 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 13 Apr 2020 18:20:24 +0200 Subject: [PATCH] Support single-quoted and double-quoted keys in array shapes --- composer.json | 2 +- src/PhpDoc/TypeNodeResolver.php | 5 ++++ .../Analyser/NodeScopeResolverTest.php | 6 +++++ .../data/array-shapes-keys-strings.php | 24 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/data/array-shapes-keys-strings.php diff --git a/composer.json b/composer.json index 06f0c9f730..cd41f8fbc3 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "nikic/php-parser": "^4.4.0", "ondram/ci-detector": "^3.1", "ondrejmirtes/better-reflection": "^4.0.1", - "phpstan/phpdoc-parser": "^0.4.3", + "phpstan/phpdoc-parser": "^0.4.4", "react/child-process": "^0.6.1", "react/event-loop": "^1.1", "react/socket": "^1.3", diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 7e38ece269..69d612ad6f 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -5,6 +5,7 @@ use PHPStan\Analyser\NameScope; use PHPStan\DependencyInjection\Container; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode; +use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode; use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; @@ -457,6 +458,10 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name $offsetType = new ConstantIntegerType((int) $itemNode->keyName->value); } elseif ($itemNode->keyName instanceof IdentifierTypeNode) { $offsetType = new ConstantStringType($itemNode->keyName->name); + } elseif ($itemNode->keyName instanceof ConstExprStringNode) { + $offsetType = new ConstantStringType($itemNode->keyName->value); + } elseif ($itemNode->keyName !== null) { + throw new \PHPStan\ShouldNotHappenException('Unsupported key node type: ' . get_class($itemNode->keyName)); } $builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope)); } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index b3453e68b5..b9bdc8729e 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -9825,6 +9825,11 @@ public function dataBug3142(): array return $this->gatherAssertTypes(__DIR__ . '/data/bug-3142.php'); } + public function dataArrayShapeKeysStrings(): array + { + return $this->gatherAssertTypes(__DIR__ . '/data/array-shapes-keys-strings.php'); + } + /** * @dataProvider dataBug2574 * @dataProvider dataBug2577 @@ -9861,6 +9866,7 @@ public function dataBug3142(): array * @dataProvider dataArrowFunctionReturnTypeInference * @dataProvider dataIsNumeric * @dataProvider dataBug3142 + * @dataProvider dataArrayShapeKeysStrings * @param ConstantStringType $expectedType * @param Type $actualType */ diff --git a/tests/PHPStan/Analyser/data/array-shapes-keys-strings.php b/tests/PHPStan/Analyser/data/array-shapes-keys-strings.php new file mode 100644 index 0000000000..996dd3e39e --- /dev/null +++ b/tests/PHPStan/Analyser/data/array-shapes-keys-strings.php @@ -0,0 +1,24 @@ + $dollar + */ + public function doFoo(array $slash, array $dollar): void + { + assertType("array('namespace/key' => string)", $slash); + assertType('array string)>', $dollar); + } + +}