diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_possibly_null.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_possibly_null.php.inc new file mode 100644 index 00000000000..41193f6e54a --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector/Fixture/skip_possibly_null.php.inc @@ -0,0 +1,21 @@ + 'Firstitem', + 111 => 'Seconditem', + 'second' => 'Seconditem', + ]; + + return $items[$key]; + } +} diff --git a/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php b/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php index cb7687e482a..0d8848691a4 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php @@ -13,6 +13,7 @@ use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\StaticTypeMapper\StaticTypeMapper; +use Rector\TypeDeclaration\Guard\ParamTypeAddGuard; use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder; use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -26,7 +27,8 @@ final class AddParamFromDimFetchKeyUseRector extends AbstractRector public function __construct( private readonly ArrayDimFetchFinder $arrayDimFetchFinder, private readonly StaticTypeMapper $staticTypeMapper, - private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard + private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, + private readonly ParamTypeAddGuard $paramTypeAddGuard ) { } @@ -107,6 +109,10 @@ public function refactor(Node $node): ?Node continue; } + if (! $this->paramTypeAddGuard->isLegal($param, $classMethod)) { + continue; + } + foreach ($dimFetches as $dimFetch) { $dimFetchType = $this->getType($dimFetch->var);