Skip to content

Commit e54360a

Browse files
authored
[TypeDeclaration] Skip possibly null on AddParamFromDimFetchKeyUseRector (#7516)
* [TypeDeclaration] Skip possibly null on AddParamFromDimFetchKeyUseRector * fix
1 parent 2956e97 commit e54360a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector\Fixture;
4+
5+
final class SkipPossiblyNull
6+
{
7+
public function process($key)
8+
{
9+
if ($key === null) {
10+
return;
11+
}
12+
13+
$items = [
14+
'first' => 'Firstitem',
15+
111 => 'Seconditem',
16+
'second' => 'Seconditem',
17+
];
18+
19+
return $items[$key];
20+
}
21+
}

rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
1414
use Rector\Rector\AbstractRector;
1515
use Rector\StaticTypeMapper\StaticTypeMapper;
16+
use Rector\TypeDeclaration\Guard\ParamTypeAddGuard;
1617
use Rector\TypeDeclarationDocblocks\NodeFinder\ArrayDimFetchFinder;
1718
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
1819
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -26,7 +27,8 @@ final class AddParamFromDimFetchKeyUseRector extends AbstractRector
2627
public function __construct(
2728
private readonly ArrayDimFetchFinder $arrayDimFetchFinder,
2829
private readonly StaticTypeMapper $staticTypeMapper,
29-
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
30+
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
31+
private readonly ParamTypeAddGuard $paramTypeAddGuard
3032
) {
3133
}
3234

@@ -107,6 +109,10 @@ public function refactor(Node $node): ?Node
107109
continue;
108110
}
109111

112+
if (! $this->paramTypeAddGuard->isLegal($param, $classMethod)) {
113+
continue;
114+
}
115+
110116
foreach ($dimFetches as $dimFetch) {
111117
$dimFetchType = $this->getType($dimFetch->var);
112118

0 commit comments

Comments
 (0)