Skip to content

Commit

Permalink
ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iter…
Browse files Browse the repository at this point in the history
…able (#36)

ForbidAssignmentNotMatchingVarDocRule: fix check-shape-only with iterables
  • Loading branch information
janedbal committed Oct 10, 2022
1 parent 6f30adc commit efd369f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Rule/ForbidAssignmentNotMatchingVarDocRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function test(): void
/** @var array{id: string, value: string} $var check-shape-only */
$var = $this->returnArrayShape();

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


/** @var self $var */
$var = $this->returnSelf();
Expand Down Expand Up @@ -127,11 +130,11 @@ public function returnArrayShape(): array
}

/**
* @return iterable{ id: int, value: string }
* @return iterable<array{ id: int, value: string }>
*/
public function returnIterableWithShape(): iterable
public function returnIterableWithArrayShape(): iterable
{
return ['id' => 1, 'value' => 'foo'];
return [['id' => 1, 'value' => 'foo']];
}

public function returnInt(): int
Expand Down

0 comments on commit efd369f

Please sign in to comment.