diff --git a/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_array_access_type.php.inc b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_array_access_type.php.inc new file mode 100644 index 00000000000..2cfddb62ace --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector/Fixture/skip_array_access_type.php.inc @@ -0,0 +1,17 @@ +> $items + */ + public function run(array $items) + { + usort( + $items, + fn ($a, $b): int => $a['key'] <=> $b['key'] + ); + } +} diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 9f8ae4c813f..9e404ea0acc 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -4,14 +4,15 @@ namespace Rector\TypeDeclaration\Rector\FuncCall; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\Instanceof_; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PHPStan\Type\ArrayType; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\Enum\NativeFuncCallPositions; @@ -85,6 +86,9 @@ public function refactor(Node $node): ?Node continue; } + $arrayPosition = $positions['array']; + $closureItems = $node->getArgs()[$arrayPosition]->value; + $isArrayVariableNames = $this->resolveIsArrayVariables($closureExpr); $instanceofVariableNames = $this->resolveInstanceofVariables($closureExpr); $skippedVariableNames = array_merge($isArrayVariableNames, $instanceofVariableNames); @@ -106,6 +110,11 @@ public function refactor(Node $node): ?Node continue; } + $type = $this->getType($closureItems); + if ($type instanceof ArrayType && $type->getItemType()->isObject()->yes()) { + continue; + } + $hasChanged = true; $closureParam->type = new Identifier('array'); }