Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FuncCall\AddArrowFunctionParamArrayWhereDimFetchRector\Fixture;

final class SkipMaybeArrayAccessItem
{
/**
* @param array<int, array{id: int}|\ArrayAccess<string, int>> $items
*/
public function run(array $items)
{
return array_filter($items, fn ($item) => $item['id'] ?? null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function refactor(Node $node): ?Node

// the items type does not change per closure param, resolve it once
$type = $this->getType($closureItems);
if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) {
if ($this->hasMaybeObjectItems($type)) {
continue;
}

Expand Down Expand Up @@ -211,4 +212,21 @@ private function resolveInstanceofVariables(Closure|ArrowFunction $closureExpr):

return $variableNames;
}

/**
* Items that can be objects, e.g. ArrayAccess, must not be typed as array
*/
private function hasMaybeObjectItems(Type $type): bool
{
if (! $type->isArray()->yes()) {
return false;
}

$itemType = $type->getIterableValueType();
if ($itemType instanceof MixedType) {
return false;
}

return ! $itemType->isObject()->no();
}
}