From 8bb65442de13d799b375d05e743245c020d6d02f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 26 Nov 2026 11:28:42 +0100 Subject: [PATCH] [TypeDeclaration] Skip non-array and template items on AddArrowFunctionParamArrayWhereDimFetchRector --- .../Fixture/skip_string_items.php.inc | 14 ++++++++++++++ .../Fixture/skip_template_items.php.inc | 15 +++++++++++++++ ...ArrowFunctionParamArrayWhereDimFetchRector.php | 12 +++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_string_items.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_template_items.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_string_items.php.inc b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_string_items.php.inc new file mode 100644 index 00000000000..1720812624c --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_string_items.php.inc @@ -0,0 +1,14 @@ + $items + */ + public function run(array $items) + { + return array_map(fn ($item) => $item[0], $items); + } +} diff --git a/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_template_items.php.inc b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_template_items.php.inc new file mode 100644 index 00000000000..54751c5afce --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_template_items.php.inc @@ -0,0 +1,15 @@ + $items + */ + public function run(array $items) + { + return array_map(fn ($item) => $item['id'], $items); + } +} diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 9234e663d21..9defe2a2399 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PHPStan\Type\Generic\TemplateType; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\PhpParser\Node\BetterNodeFinder; @@ -98,7 +99,7 @@ public function refactor(Node $node): ?Node // the items type does not change per closure param, resolve it once $type = $this->getType($closureItems); - if ($this->hasMaybeObjectItems($type)) { + if ($this->hasNonArrayItems($type)) { continue; } @@ -214,19 +215,20 @@ private function resolveInstanceofVariables(Closure|ArrowFunction $closureExpr): } /** - * Items that can be objects, e.g. ArrayAccess, must not be typed as array + * Items that are not always arrays, e.g. ArrayAccess, string or template, must not be typed as array */ - private function hasMaybeObjectItems(Type $type): bool + private function hasNonArrayItems(Type $type): bool { if (! $type->isArray()->yes()) { return false; } $itemType = $type->getIterableValueType(); - if ($itemType instanceof MixedType) { + // unknown items are typed as before, except templates that can be anything + if ($itemType instanceof MixedType && ! $itemType instanceof TemplateType) { return false; } - return ! $itemType->isObject()->no(); + return ! $itemType->isArray()->yes(); } }