Skip to content

Commit

Permalink
Support single-quoted and double-quoted keys in array shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 13, 2020
1 parent 8a894c5 commit e210a6e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -9861,6 +9866,7 @@ public function dataBug3142(): array
* @dataProvider dataArrowFunctionReturnTypeInference
* @dataProvider dataIsNumeric
* @dataProvider dataBug3142
* @dataProvider dataArrayShapeKeysStrings
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/data/array-shapes-keys-strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ArrayShapeKeysStrings;

use function PHPStan\Analyser\assertType;

class Foo
{

/**
* @param array{
* 'namespace/key': string
* } $slash
* @param array<int, array{
* "$ref": string
* }> $dollar
*/
public function doFoo(array $slash, array $dollar): void
{
assertType("array('namespace/key' => string)", $slash);
assertType('array<int, array(\'$ref\' => string)>', $dollar);
}

}

0 comments on commit e210a6e

Please sign in to comment.