From efd369fb3a1d6abb9825c5a4091cc8e83d4de566 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Mon, 10 Oct 2022 10:58:26 +0200 Subject: [PATCH] ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iterable (#36) ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iterables --- src/Rule/ForbidAssignmentNotMatchingVarDocRule.php | 3 ++- .../data/ForbidAssignmentNotMatchingVarDocRule/code.php | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Rule/ForbidAssignmentNotMatchingVarDocRule.php b/src/Rule/ForbidAssignmentNotMatchingVarDocRule.php index 236ba55..7e64344 100644 --- a/src/Rule/ForbidAssignmentNotMatchingVarDocRule.php +++ b/src/Rule/ForbidAssignmentNotMatchingVarDocRule.php @@ -10,6 +10,7 @@ use PHPStan\Rules\Rule; use PHPStan\Type\ArrayType; use PHPStan\Type\FileTypeMapper; +use PHPStan\Type\IterableType; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use PHPStan\Type\TypeTraverser; @@ -102,7 +103,7 @@ public function processNode(Node $node, Scope $scope): array private function weakenTypeToKeepShapeOnly(Type $type): Type { return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type { - if ($type instanceof ArrayType) { + if ($type instanceof ArrayType || $type instanceof IterableType) { return $traverse($type); // keep array shapes, but forget all inner types } diff --git a/tests/Rule/data/ForbidAssignmentNotMatchingVarDocRule/code.php b/tests/Rule/data/ForbidAssignmentNotMatchingVarDocRule/code.php index 86fbb1a..77a51c5 100644 --- a/tests/Rule/data/ForbidAssignmentNotMatchingVarDocRule/code.php +++ b/tests/Rule/data/ForbidAssignmentNotMatchingVarDocRule/code.php @@ -53,6 +53,9 @@ public function test(): void /** @var array{id: string, value: string} $var check-shape-only */ $var = $this->returnArrayShape(); + /** @var iterable $var check-shape-only */ + $var = $this->returnIterableWithArrayShape(); // error: Invalid var phpdoc of $var. Cannot assign iterable to iterable + /** @var self $var */ $var = $this->returnSelf(); @@ -127,11 +130,11 @@ public function returnArrayShape(): array } /** - * @return iterable{ id: int, value: string } + * @return iterable */ - public function returnIterableWithShape(): iterable + public function returnIterableWithArrayShape(): iterable { - return ['id' => 1, 'value' => 'foo']; + return [['id' => 1, 'value' => 'foo']]; } public function returnInt(): int