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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

use ArrayAccess;

class SkipArrayAccess
{
/**
* @param ArrayAccess $item
*/
public function resolve($item)
{
return $item['name'];
}

/**
* @param ArrayAccess|array<string, string> $item
*/
public function resolve($item)
{
return $item['name'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

class SkipTyped
{
public function resolve(object $item)
{
return $item['name'];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeVisitor;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\Rector\AbstractRector;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -38,7 +42,9 @@
final class StrictArrayParamDimFetchRector extends AbstractRector
{
public function __construct(
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly TypeComparator $typeComparator,
private readonly TypeFactory $typeFactory
) {
}

Expand Down Expand Up @@ -170,6 +176,20 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
return null;
}

$variableType = $this->typeFactory->createMixedPassedOrUnionType([$variableType]);
if ($variableType instanceof UnionType) {
$isParamAccessedArrayDimFetch = false;
return NodeVisitor::STOP_TRAVERSAL;
}

if ($variableType instanceof ObjectType && $this->typeComparator->isSubtype(
$variableType,
new ObjectType('ArrayAccess')
)) {
$isParamAccessedArrayDimFetch = false;
return NodeVisitor::STOP_TRAVERSAL;
}

$isParamAccessedArrayDimFetch = true;
return null;
});
Expand Down