Skip to content

Commit 1e17a0b

Browse files
authored
Make ValidationRuleArrayStringValueToArrayRector work with Request object calls (#340)
1 parent 95f4c1d commit 1e17a0b

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function refactor(Node $node): MethodCall|StaticCall|ClassLike|null
6363
return $this->refactorClassMethod($node);
6464
}
6565

66+
if ($node instanceof MethodCall && $this->isName($node->name, 'validate')) {
67+
return $this->refactorValidateCall($node);
68+
}
69+
6670
return $this->refactorCall($node);
6771
}
6872

@@ -184,6 +188,30 @@ private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCal
184188
return $this->processValidationRules($rulesArgument) ? $node : null;
185189
}
186190

191+
private function refactorValidateCall(MethodCall $methodCall): ?MethodCall
192+
{
193+
if (! $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Http\Request'))) {
194+
return null;
195+
}
196+
197+
if ($methodCall->args === []) {
198+
return null;
199+
}
200+
201+
if (! $methodCall->args[0] instanceof Arg) {
202+
return null;
203+
}
204+
205+
// The first argument should be the rules array
206+
$rulesArgument = $methodCall->args[0]->value;
207+
208+
if (! $rulesArgument instanceof Array_) {
209+
return null;
210+
}
211+
212+
return $this->processValidationRules($rulesArgument) ? $methodCall : null;
213+
}
214+
187215
private function refactorClassMethod(ClassLike $classLike): ?ClassLike
188216
{
189217
if (! $this->isObjectType($classLike, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\MethodCall\ValidationRuleArrayStringValueToArrayRector\Fixture;
4+
5+
class AppliesToHttpRequests
6+
{
7+
public function store(\Illuminate\Http\Request $request)
8+
{
9+
$request->validate([
10+
'name' => 'required|string',
11+
]);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace RectorLaravel\Tests\Rector\MethodCall\ValidationRuleArrayStringValueToArrayRector\Fixture;
20+
21+
class AppliesToHttpRequests
22+
{
23+
public function store(\Illuminate\Http\Request $request)
24+
{
25+
$request->validate([
26+
'name' => ['required', 'string'],
27+
]);
28+
}
29+
}
30+
31+
?>

0 commit comments

Comments
 (0)