From f71165d013fe96eff703ccf374514a34fa00486e Mon Sep 17 00:00:00 2001 From: geni_jaho Date: Mon, 5 May 2025 18:37:02 +0200 Subject: [PATCH] Make ValidationRuleArrayStringValueToArrayRector work with Request object calls --- ...ationRuleArrayStringValueToArrayRector.php | 28 +++++++++++++++++ .../Fixture/applies_to_http_requests.php.inc | 31 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tests/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector/Fixture/applies_to_http_requests.php.inc diff --git a/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php b/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php index f94d773b..ee1ceb9a 100644 --- a/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php +++ b/src/Rector/MethodCall/ValidationRuleArrayStringValueToArrayRector.php @@ -60,6 +60,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); } @@ -122,6 +126,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'], + ]); + } +} + +?>