Skip to content

Commit 019c5c6

Browse files
authored
[DeadCode] Allow FuncCall on RemoveNullArgOnNullDefaultParamRector (#7567)
* [DeadCode] Allow FuncCall on RemoveNullArgOnNullDefaultParamRector * update fixture * implemetned * implemetned * implemetned * rectify * rectify
1 parent b838cdf commit 019c5c6

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
6+
7+
function SomeFunctionWithDefaultNullArg(?string $someClass = null)
8+
{
9+
}
10+
11+
SomeFunctionWithDefaultNullArg(null);
12+
13+
?>
14+
-----
15+
<?php
16+
17+
declare(strict_types=1);
18+
19+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
20+
21+
function SomeFunctionWithDefaultNullArg(?string $someClass = null)
22+
{
23+
}
24+
25+
SomeFunctionWithDefaultNullArg();
26+
27+
?>

rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Rector\DeadCode\NodeAnalyzer;
66

7+
use PhpParser\Node\Expr\FuncCall;
78
use PhpParser\Node\Expr\MethodCall;
89
use PhpParser\Node\Expr\New_;
910
use PhpParser\Node\Expr\StaticCall;
11+
use PHPStan\Reflection\FunctionReflection;
1012
use PHPStan\Reflection\MethodReflection;
1113
use PHPStan\Reflection\ParametersAcceptorSelector;
1214
use PHPStan\Type\NullType;
@@ -22,16 +24,16 @@ public function __construct(
2224
/**
2325
* @return int[]
2426
*/
25-
public function resolveNullPositions(MethodCall|StaticCall|New_ $callLike): array
27+
public function resolveNullPositions(MethodCall|StaticCall|New_|FuncCall $callLike): array
2628
{
27-
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
28-
if (! $methodReflection instanceof MethodReflection) {
29+
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
30+
if (! $reflection instanceof MethodReflection && ! $reflection instanceof FunctionReflection) {
2931
return [];
3032
}
3133

3234
$nullPositions = [];
3335

34-
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
36+
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($reflection->getVariants());
3537
foreach ($extendedParametersAcceptor->getParameters() as $position => $extendedParameterReflection) {
3638
if (! $extendedParameterReflection->getDefaultValue() instanceof NullType) {
3739
continue;
@@ -43,14 +45,16 @@ public function resolveNullPositions(MethodCall|StaticCall|New_ $callLike): arra
4345
return $nullPositions;
4446
}
4547

46-
public function resolvePositionParameterByName(MethodCall|StaticCall|New_ $callLike, string $parameterName): ?int
47-
{
48-
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
49-
if (! $methodReflection instanceof MethodReflection) {
48+
public function resolvePositionParameterByName(
49+
MethodCall|StaticCall|New_|FuncCall $callLike,
50+
string $parameterName
51+
): ?int {
52+
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
53+
if (! $reflection instanceof MethodReflection && ! $reflection instanceof FunctionReflection) {
5054
return null;
5155
}
5256

53-
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants());
57+
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($reflection->getVariants());
5458
foreach ($extendedParametersAcceptor->getParameters() as $position => $extendedParameterReflection) {
5559
if ($extendedParameterReflection->getName() === $parameterName) {
5660
return $position;

rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\DeadCode\Rector\MethodCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\FuncCall;
89
use PhpParser\Node\Expr\MethodCall;
910
use PhpParser\Node\Expr\New_;
1011
use PhpParser\Node\Expr\StaticCall;
@@ -71,13 +72,13 @@ public function execute(?SomeClass $someClass = null)
7172

7273
public function getNodeTypes(): array
7374
{
74-
return [MethodCall::class, StaticCall::class, New_::class];
75+
return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class];
7576
}
7677

7778
/**
78-
* @param MethodCall|StaticCall|New_ $node
79+
* @param MethodCall|StaticCall|New_|FuncCall $node
7980
*/
80-
public function refactor(Node $node): StaticCall|MethodCall|New_|null
81+
public function refactor(Node $node): StaticCall|MethodCall|New_|FuncCall|null
8182
{
8283
if ($node->isFirstClassCallable()) {
8384
return null;
@@ -87,8 +88,12 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null
8788
return null;
8889
}
8990

90-
$hasChanged = false;
91+
$nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node);
92+
if ($nullPositions === []) {
93+
return null;
94+
}
9195

96+
$hasChanged = false;
9297
$args = $node->getArgs();
9398
$lastArgPosition = count($args) - 1;
9499
for ($position = $lastArgPosition; $position >= 0; --$position) {
@@ -114,8 +119,7 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null
114119
break;
115120
}
116121

117-
$nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node);
118-
if (! in_array($position, $nullPositions)) {
122+
if (! in_array($position, $nullPositions, true)) {
119123
break;
120124
}
121125

0 commit comments

Comments
 (0)