Skip to content

Commit c9189a4

Browse files
authored
[DeadCode] Handle called by static call on RemoveUnusedPrivateMethodParameterRector (#6912)
* [DeadCode] Handle called by static call on RemoveUnusedPrivateMethodParameterRector * rectiry * rectiry
1 parent b38c275 commit c9189a4

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector\Fixture;
4+
5+
class RemoveOnCallerViaSelf
6+
{
7+
private static function just_a_test(
8+
string $foo,
9+
array $bar,
10+
$used_parameter
11+
): void {
12+
\external::whatever($foo, $used_parameter);
13+
}
14+
15+
public static function trigger(): void
16+
{
17+
self::just_a_test('hello', [], 'test');
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector\Fixture;
26+
27+
class RemoveOnCallerViaSelf
28+
{
29+
private static function just_a_test(
30+
string $foo,
31+
$used_parameter
32+
): void {
33+
\external::whatever($foo, $used_parameter);
34+
}
35+
36+
public static function trigger(): void
37+
{
38+
self::just_a_test('hello', 'test');
39+
}
40+
}
41+
42+
?>

rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodParameterRector.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace Rector\DeadCode\Rector\ClassMethod;
66

7+
use PHPStan\Type\ObjectType;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\MethodCall;
9-
use PhpParser\Node\Expr\Variable;
10+
use PhpParser\Node\Expr\StaticCall;
1011
use PhpParser\Node\Param;
1112
use PhpParser\Node\Stmt\Class_;
1213
use PhpParser\Node\Stmt\ClassMethod;
@@ -129,9 +130,10 @@ private function removeCallerArgs(Class_ $class, ClassMethod $classMethod, array
129130
$methodName = $this->getName($classMethod);
130131
$keysArg = array_keys($unusedParameters);
131132

133+
$classObjectType = new ObjectType((string) $this->getName($class));
132134
foreach ($classMethods as $classMethod) {
133-
/** @var MethodCall[] $callers */
134-
$callers = $this->resolveCallers($classMethod, $methodName);
135+
/** @var MethodCall[]|StaticCall[] $callers */
136+
$callers = $this->resolveCallers($classMethod, $methodName, $classObjectType);
135137
if ($callers === []) {
136138
continue;
137139
}
@@ -145,42 +147,42 @@ private function removeCallerArgs(Class_ $class, ClassMethod $classMethod, array
145147
/**
146148
* @param int[] $keysArg
147149
*/
148-
private function cleanupArgs(MethodCall $methodCall, array $keysArg): void
150+
private function cleanupArgs(MethodCall|StaticCall $call, array $keysArg): void
149151
{
150-
if ($methodCall->isFirstClassCallable()) {
152+
if ($call->isFirstClassCallable()) {
151153
return;
152154
}
153155

154-
$args = $methodCall->getArgs();
156+
$args = $call->getArgs();
155157
foreach (array_keys($args) as $key) {
156158
if (in_array($key, $keysArg, true)) {
157159
unset($args[$key]);
158160
}
159161
}
160162

161163
// reset arg keys
162-
$methodCall->args = array_values($args);
164+
$call->args = array_values($args);
163165
}
164166

165167
/**
166-
* @return MethodCall[]
168+
* @return MethodCall[]|StaticCall[]
167169
*/
168-
private function resolveCallers(ClassMethod $classMethod, string $methodName): array
170+
private function resolveCallers(ClassMethod $classMethod, string $methodName, ObjectType $classObjectType): array
169171
{
170-
return $this->betterNodeFinder->find($classMethod, function (Node $subNode) use ($methodName): bool {
171-
if (! $subNode instanceof MethodCall) {
172+
return $this->betterNodeFinder->find($classMethod, function (Node $subNode) use ($methodName, $classObjectType): bool {
173+
if (! $subNode instanceof MethodCall && ! $subNode instanceof StaticCall) {
172174
return false;
173175
}
174176

175177
if ($subNode->isFirstClassCallable()) {
176178
return false;
177179
}
178180

179-
if (! $subNode->var instanceof Variable) {
180-
return false;
181-
}
181+
$nodeToCheck = $subNode instanceof MethodCall
182+
? $subNode->var
183+
: $subNode->class;
182184

183-
if (! $this->isName($subNode->var, 'this')) {
185+
if (! $this->isObjectType($nodeToCheck, $classObjectType)) {
184186
return false;
185187
}
186188

0 commit comments

Comments
 (0)