Skip to content

Commit b838cdf

Browse files
authored
[DeadCode] Allow remove named argument if position is match with param on RemoveNullArgOnNullDefaultParamRector (#7566)
* [DeadCode] Allow remove named argument if position is match on RemoveNullArgOnNullDefaultParamRector * add example for skip * fix * fix
1 parent 4f9f49c commit b838cdf

5 files changed

Lines changed: 76 additions & 4 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
6+
7+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Source\SomeExternalClass;
8+
9+
final class SkipNamedArgumentPostitionNotMatch
10+
{
11+
public function get()
12+
{
13+
SomeExternalClass::withMiddleNotNullDefault(item: null);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
6+
7+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Source\SomeExternalClass;
8+
9+
final class WithNamedArgumentPositionMatch
10+
{
11+
public function get()
12+
{
13+
$object = new SomeExternalClass(name: 'John', id: null);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
24+
25+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Source\SomeExternalClass;
26+
27+
final class WithNamedArgumentPositionMatch
28+
{
29+
public function get()
30+
{
31+
$object = new SomeExternalClass(name: 'John');
32+
}
33+
}
34+
35+
?>

rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public function resolveNullPositions(MethodCall|StaticCall|New_ $callLike): arra
4242

4343
return $nullPositions;
4444
}
45+
46+
public function resolvePositionParameterByName(MethodCall|StaticCall|New_ $callLike, string $parameterName): ?int
47+
{
48+
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
49+
if (! $methodReflection instanceof MethodReflection) {
50+
return null;
51+
}
52+
53+
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
54+
foreach ($extendedParametersAcceptor->getParameters() as $position => $extendedParameterReflection) {
55+
if ($extendedParameterReflection->getName() === $parameterName) {
56+
return $position;
57+
}
58+
}
59+
60+
return null;
61+
}
4562
}

rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Expr\New_;
1010
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Identifier;
1112
use Rector\DeadCode\NodeAnalyzer\CallLikeParamDefaultResolver;
1213
use Rector\PhpParser\Node\Value\ValueResolver;
1314
use Rector\Rector\AbstractRector;
@@ -90,7 +91,7 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null
9091

9192
$args = $node->getArgs();
9293
$lastArgPosition = count($args) - 1;
93-
for ($position = $lastArgPosition; $position >=0; --$position) {
94+
for ($position = $lastArgPosition; $position >= 0; --$position) {
9495
if (! isset($args[$position])) {
9596
continue;
9697
}
@@ -100,8 +101,12 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null
100101
break;
101102
}
102103

103-
// skip named args
104-
if ($arg->name instanceof Node) {
104+
// stop when found named arg and position not match
105+
if ($arg->name instanceof Identifier &&
106+
$position !== $this->callLikeParamDefaultResolver->resolvePositionParameterByName(
107+
$node,
108+
$arg->name->toString()
109+
)) {
105110
break;
106111
}
107112

rules/Transform/Rector/Assign/PropertyAssignToMethodCallRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Rector\Transform\Rector\Assign;
66

7-
use Rector\Exception\ShouldNotHappenException;
87
use PhpParser\Node;
98
use PhpParser\Node\Expr\Assign;
109
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
1110
use Rector\Contract\Rector\ConfigurableRectorInterface;
11+
use Rector\Exception\ShouldNotHappenException;
1212
use Rector\Rector\AbstractRector;
1313
use Rector\Transform\ValueObject\PropertyAssignToMethodCall;
1414
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;

0 commit comments

Comments
 (0)