Skip to content

Commit efd369f

Browse files
authored
ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iterable (#36)
ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iterables
1 parent 6f30adc commit efd369f

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Rule/ForbidAssignmentNotMatchingVarDocRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Type\ArrayType;
1212
use PHPStan\Type\FileTypeMapper;
13+
use PHPStan\Type\IterableType;
1314
use PHPStan\Type\MixedType;
1415
use PHPStan\Type\Type;
1516
use PHPStan\Type\TypeTraverser;
@@ -102,7 +103,7 @@ public function processNode(Node $node, Scope $scope): array
102103
private function weakenTypeToKeepShapeOnly(Type $type): Type
103104
{
104105
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
105-
if ($type instanceof ArrayType) {
106+
if ($type instanceof ArrayType || $type instanceof IterableType) {
106107
return $traverse($type); // keep array shapes, but forget all inner types
107108
}
108109

tests/Rule/data/ForbidAssignmentNotMatchingVarDocRule/code.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function test(): void
5353
/** @var array{id: string, value: string} $var check-shape-only */
5454
$var = $this->returnArrayShape();
5555

56+
/** @var iterable<array{invalid: string}> $var check-shape-only */
57+
$var = $this->returnIterableWithArrayShape(); // error: Invalid var phpdoc of $var. Cannot assign iterable<array{id: mixed, value: mixed}> to iterable<array{invalid: string}>
58+
5659

5760
/** @var self $var */
5861
$var = $this->returnSelf();
@@ -127,11 +130,11 @@ public function returnArrayShape(): array
127130
}
128131

129132
/**
130-
* @return iterable{ id: int, value: string }
133+
* @return iterable<array{ id: int, value: string }>
131134
*/
132-
public function returnIterableWithShape(): iterable
135+
public function returnIterableWithArrayShape(): iterable
133136
{
134-
return ['id' => 1, 'value' => 'foo'];
137+
return [['id' => 1, 'value' => 'foo']];
135138
}
136139

137140
public function returnInt(): int

0 commit comments

Comments
 (0)