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,29 @@
<?php

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

final class WithNamedArg
{
public function run()
{
$array = [1, 2, 3];
$result = array_filter(callback: fn ($item) => $item * 2, array: $array);
}
}

?>
-----
<?php

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

final class WithNamedArg
{
public function run()
{
$array = [1, 2, 3];
$result = array_filter(callback: fn (int $item) => $item * 2, array: $array);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,25 +87,34 @@ 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
if ($arrowFunctionParam->type instanceof 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) {
Expand Down