|
9 | 9 | use PhpParser\Node\Expr\MethodCall;
|
10 | 10 | use PhpParser\Node\Expr\StaticCall;
|
11 | 11 | use PhpParser\Node\Scalar\String_;
|
12 |
| -use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\Node\Stmt\ClassLike; |
13 | 13 | use PhpParser\Node\Stmt\Return_;
|
14 | 14 | use PhpParser\NodeTraverser;
|
15 | 15 | use PHPStan\Type\ObjectType;
|
@@ -48,15 +48,15 @@ public function getRuleDefinition(): RuleDefinition
|
48 | 48 |
|
49 | 49 | public function getNodeTypes(): array
|
50 | 50 | {
|
51 |
| - return [MethodCall::class, StaticCall::class, ClassMethod::class]; |
| 51 | + return [MethodCall::class, StaticCall::class, ClassLike::class]; |
52 | 52 | }
|
53 | 53 |
|
54 | 54 | /**
|
55 |
| - * @param MethodCall|StaticCall|ClassMethod $node |
| 55 | + * @param MethodCall|StaticCall|ClassLike $node |
56 | 56 | */
|
57 |
| - public function refactor(Node $node): MethodCall|StaticCall|ClassMethod|null |
| 57 | + public function refactor(Node $node): MethodCall|StaticCall|ClassLike|null |
58 | 58 | {
|
59 |
| - if ($node instanceof ClassMethod) { |
| 59 | + if ($node instanceof ClassLike) { |
60 | 60 | return $this->refactorClassMethod($node);
|
61 | 61 | }
|
62 | 62 |
|
@@ -122,42 +122,47 @@ private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCal
|
122 | 122 | return $this->processValidationRules($rulesArgument) ? $node : null;
|
123 | 123 | }
|
124 | 124 |
|
125 |
| - private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod |
| 125 | + private function refactorClassMethod(ClassLike $classLike): ?ClassLike |
126 | 126 | {
|
127 |
| - if (! $this->isObjectType($classMethod, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) { |
| 127 | + if (! $this->isObjectType($classLike, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) { |
128 | 128 | return null;
|
129 | 129 | }
|
130 | 130 |
|
131 |
| - if (! $this->isName($classMethod, 'rules')) { |
132 |
| - return null; |
133 |
| - } |
| 131 | + $hasChanged = false; |
| 132 | + foreach ($classLike->getMethods() as $classMethod) { |
| 133 | + if (! $this->isName($classMethod, 'rules')) { |
| 134 | + continue; |
| 135 | + } |
134 | 136 |
|
135 |
| - $changed = false; |
| 137 | + $changed = false; |
| 138 | + $this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed, &$hasChanged): Return_|int|null { |
| 139 | + if ($changed) { |
| 140 | + $hasChanged = true; |
136 | 141 |
|
137 |
| - $this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed): Return_|int|null { |
138 |
| - if ($changed) { |
139 |
| - return NodeTraverser::STOP_TRAVERSAL; |
140 |
| - } |
| 142 | + return NodeTraverser::STOP_TRAVERSAL; |
| 143 | + } |
141 | 144 |
|
142 |
| - if (! $node instanceof Return_) { |
143 |
| - return null; |
144 |
| - } |
| 145 | + if (! $node instanceof Return_) { |
| 146 | + return null; |
| 147 | + } |
145 | 148 |
|
146 |
| - if (! $node->expr instanceof Array_) { |
147 |
| - return null; |
148 |
| - } |
| 149 | + if (! $node->expr instanceof Array_) { |
| 150 | + return null; |
| 151 | + } |
149 | 152 |
|
150 |
| - if ($this->processValidationRules($node->expr)) { |
151 |
| - $changed = true; |
| 153 | + if ($this->processValidationRules($node->expr)) { |
| 154 | + $hasChanged = true; |
| 155 | + $changed = true; |
152 | 156 |
|
153 |
| - return $node; |
154 |
| - } |
| 157 | + return $node; |
| 158 | + } |
155 | 159 |
|
156 |
| - return null; |
157 |
| - }); |
| 160 | + return null; |
| 161 | + }); |
| 162 | + } |
158 | 163 |
|
159 |
| - if ($changed) { |
160 |
| - return $classMethod; |
| 164 | + if ($hasChanged) { |
| 165 | + return $classLike; |
161 | 166 | }
|
162 | 167 |
|
163 | 168 | return null;
|
|
0 commit comments