From 6f49072f86ad2a5af8be654bdabb37f1dd9b0944 Mon Sep 17 00:00:00 2001 From: Olivier Dolbeau Date: Thu, 26 Nov 2026 11:21:18 +0100 Subject: [PATCH] [TypeDeclaration] Skip maybe object items on AddArrowFunctionParamArrayWhereDimFetchRector Co-Authored-By: Claude Opus 5.5 --- .../skip_maybe_array_access_item.php.inc | 14 ++++++++++++ ...wFunctionParamArrayWhereDimFetchRector.php | 22 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_maybe_array_access_item.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_maybe_array_access_item.php.inc b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_maybe_array_access_item.php.inc new file mode 100644 index 00000000000..f40d1cd099d --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_maybe_array_access_item.php.inc @@ -0,0 +1,14 @@ +> $items + */ + public function run(array $items) + { + return array_filter($items, fn ($item) => $item['id'] ?? null); + } +} diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index cb7f26fcfc3..9234e663d21 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -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; @@ -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; } @@ -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(); + } }