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

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

final class SkipArrayAccessType
{
/**
* @param array<int, \ArrayAccess<string, int>> $items
*/
public function run(array $items)
{
usort(
$items,
fn ($a, $b): int => $a['key'] <=> $b['key']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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');
}
Expand Down
Loading