diff --git a/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php b/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php index c5024706..57fb6c12 100644 --- a/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php +++ b/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php @@ -63,6 +63,10 @@ public function refactor(Node $node): MethodCall|StaticCall|ClassLike|null return $this->refactorClassMethod($node); } + if ($node instanceof MethodCall && $this->isName($node->name, 'validate')) { + return $this->refactorValidateCall($node); + } + return $this->refactorCall($node); } @@ -184,6 +188,30 @@ private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCal return $this->processValidationRules($rulesArgument) ? $node : null; } + private function refactorValidateCall(MethodCall $methodCall): ?MethodCall + { + if (! $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Http\Request'))) { + return null; + } + + if ($methodCall->args === []) { + return null; + } + + if (! $methodCall->args[0] instanceof Arg) { + return null; + } + + // The first argument should be the rules array + $rulesArgument = $methodCall->args[0]->value; + + if (! $rulesArgument instanceof Array_) { + return null; + } + + return $this->processValidationRules($rulesArgument) ? $methodCall : null; + } + private function refactorClassMethod(ClassLike $classLike): ?ClassLike { if (! $this->isObjectType($classLike, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) { diff --git a/tests/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector/Fixture/applies_to_http_requests.php.inc b/tests/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector/Fixture/applies_to_http_requests.php.inc new file mode 100644 index 00000000..107eec67 --- /dev/null +++ b/tests/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector/Fixture/applies_to_http_requests.php.inc @@ -0,0 +1,31 @@ +validate([ + 'name' => 'required|string', + ]); + } +} + +?> +----- +validate([ + 'name' => ['required', 'string'], + ]); + } +} + +?>