|
6 | 6 |
|
7 | 7 | use PhpParser\Node;
|
8 | 8 | use PhpParser\Node\Expr\Variable;
|
| 9 | +use PhpParser\Node\Stmt\Class_; |
9 | 10 | use PhpParser\Node\Stmt\ClassMethod;
|
| 11 | +use PHPStan\Analyser\Scope; |
10 | 12 | use PHPStan\Reflection\ClassReflection;
|
11 | 13 | use PHPStan\Type\ObjectType;
|
| 14 | +use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer; |
12 | 15 | use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
|
13 | 16 | use Rector\PhpParser\Node\BetterNodeFinder;
|
14 |
| -use Rector\Rector\AbstractRector; |
| 17 | +use Rector\Rector\AbstractScopeAwareRector; |
15 | 18 | use Rector\Reflection\ReflectionResolver;
|
16 | 19 | use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;
|
17 | 20 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
20 | 23 | /**
|
21 | 24 | * @see \Rector\Symfony\Tests\CodeQuality\Rector\ClassMethod\RemoveUnusedRequestParamRector\RemoveUnusedRequestParamRectorTest
|
22 | 25 | */
|
23 |
| -final class RemoveUnusedRequestParamRector extends AbstractRector |
| 26 | +final class RemoveUnusedRequestParamRector extends AbstractScopeAwareRector |
24 | 27 | {
|
25 | 28 | public function __construct(
|
26 | 29 | private readonly ControllerAnalyzer $controllerAnalyzer,
|
27 | 30 | private readonly ReflectionResolver $reflectionResolver,
|
28 | 31 | private readonly ClassChildAnalyzer $classChildAnalyzer,
|
29 | 32 | private readonly BetterNodeFinder $betterNodeFinder,
|
| 33 | + private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer |
30 | 34 | ) {
|
31 | 35 | }
|
32 | 36 |
|
@@ -69,56 +73,68 @@ public function run(int $id)
|
69 | 73 | */
|
70 | 74 | public function getNodeTypes(): array
|
71 | 75 | {
|
72 |
| - return [ClassMethod::class]; |
| 76 | + return [Class_::class]; |
73 | 77 | }
|
74 | 78 |
|
75 | 79 | /**
|
76 |
| - * @param ClassMethod $node |
| 80 | + * @param Class_ $node |
77 | 81 | */
|
78 |
| - public function refactor(Node $node): ?Node |
| 82 | + public function refactorWithScope(Node $node, Scope $scope): ?Node |
79 | 83 | {
|
80 |
| - if (! $node->isPublic()) { |
81 |
| - return null; |
82 |
| - } |
83 |
| - |
84 |
| - if ($node->isAbstract() || $this->hasAbstractParentClassMethod($node)) { |
85 |
| - return null; |
86 |
| - } |
87 |
| - |
88 | 84 | if (! $this->controllerAnalyzer->isInsideController($node)) {
|
89 | 85 | return null;
|
90 | 86 | }
|
91 | 87 |
|
92 |
| - if ($node->getParams() === []) { |
93 |
| - return null; |
94 |
| - } |
| 88 | + $hasChanged = false; |
95 | 89 |
|
96 |
| - // skip empty method |
97 |
| - if ($node->stmts === null) { |
98 |
| - return null; |
99 |
| - } |
| 90 | + foreach ($node->getMethods() as $classMethod) { |
| 91 | + if (! $classMethod->isPublic()) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + if ($classMethod->isAbstract() || $this->hasAbstractParentClassMethod($classMethod)) { |
| 96 | + continue; |
| 97 | + } |
100 | 98 |
|
101 |
| - foreach ($node->getParams() as $paramPosition => $param) { |
102 |
| - if (! $param->type instanceof Node) { |
| 99 | + if ($classMethod->getParams() === []) { |
103 | 100 | continue;
|
104 | 101 | }
|
105 | 102 |
|
106 |
| - if (! $this->isObjectType($param->type, new ObjectType('Symfony\Component\HttpFoundation\Request'))) { |
| 103 | + // skip empty method |
| 104 | + if ($classMethod->stmts === null) { |
107 | 105 | continue;
|
108 | 106 | }
|
109 | 107 |
|
110 |
| - /** @var string $requestParamName */ |
111 |
| - $requestParamName = $this->getName($param); |
| 108 | + foreach ($classMethod->getParams() as $paramPosition => $param) { |
| 109 | + if (! $param->type instanceof Node) { |
| 110 | + continue; |
| 111 | + } |
112 | 112 |
|
113 |
| - // we have request param here |
114 |
| - $requestVariable = $this->betterNodeFinder->findVariableOfName($node->stmts, $requestParamName); |
| 113 | + if (! $this->isObjectType($param->type, new ObjectType('Symfony\Component\HttpFoundation\Request'))) { |
| 114 | + continue; |
| 115 | + } |
115 | 116 |
|
116 |
| - // is variable used? |
117 |
| - if ($requestVariable instanceof Variable) { |
118 |
| - return null; |
| 117 | + /** @var string $requestParamName */ |
| 118 | + $requestParamName = $this->getName($param); |
| 119 | + |
| 120 | + // we have request param here |
| 121 | + $requestVariable = $this->betterNodeFinder->findVariableOfName($classMethod->stmts, $requestParamName); |
| 122 | + |
| 123 | + // is variable used? |
| 124 | + if ($requestVariable instanceof Variable) { |
| 125 | + continue 2; |
| 126 | + } |
| 127 | + |
| 128 | + if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $classMethod, $scope)) { |
| 129 | + continue 2; |
| 130 | + } |
| 131 | + |
| 132 | + unset($classMethod->params[$paramPosition]); |
| 133 | + $hasChanged = true; |
119 | 134 | }
|
| 135 | + } |
120 | 136 |
|
121 |
| - unset($node->params[$paramPosition]); |
| 137 | + if ($hasChanged) { |
122 | 138 | return $node;
|
123 | 139 | }
|
124 | 140 |
|
|
0 commit comments