From f5c370937268b3b330ac114fc224c673e5eecd5c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 2 Oct 2025 05:07:39 +0700 Subject: [PATCH] [TypeDeclaration] Allow named arg on AddArrayFunctionClosureParamTypeRector --- .../Fixture/with_named_arg.php.inc | 29 +++++++++++++++++++ ...AddArrayFunctionClosureParamTypeRector.php | 22 ++++++++++---- 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/with_named_arg.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/with_named_arg.php.inc b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/with_named_arg.php.inc new file mode 100644 index 00000000000..9c95706707b --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector/Fixture/with_named_arg.php.inc @@ -0,0 +1,29 @@ + $item * 2, array: $array); + } +} + +?> +----- + $item * 2, array: $array); + } +} + +?> diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector.php index 17f62f4982e..ab1175f522b 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrayFunctionClosureParamTypeRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; @@ -86,17 +87,21 @@ public function refactor(Node $node): ?Node $arrayPosition = $positions['array']; $callbackPosition = $positions['callback']; - $firstArgExpr = $node->getArgs()[$callbackPosition] - ->value; - if (! $firstArgExpr instanceof ArrowFunction && ! $firstArgExpr instanceof Closure) { + $callbackArg = $node->getArg('callback', $callbackPosition); + if (! $callbackArg instanceof Arg) { continue; } - if (count($firstArgExpr->getParams()) !== 1) { + $callbackArgExpr = $callbackArg->value; + if (! $callbackArgExpr instanceof ArrowFunction && ! $callbackArgExpr instanceof Closure) { continue; } - $arrowFunction = $firstArgExpr; + if (count($callbackArgExpr->getParams()) !== 1) { + continue; + } + + $arrowFunction = $callbackArgExpr; $arrowFunctionParam = $arrowFunction->getParams()[0]; // param is known already @@ -104,7 +109,12 @@ public function refactor(Node $node): ?Node continue; } - $passedExprType = $this->getType($node->getArgs()[$arrayPosition]->value); + $arrayArg = $node->getArg('array', $arrayPosition); + if (! $arrayArg instanceof Arg) { + continue; + } + + $passedExprType = $this->getType($arrayArg->value); $singlePassedExprType = $this->resolveArrayItemType($passedExprType); if (! $singlePassedExprType instanceof Type) {