Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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'))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ValidationRuleArrayStringValueToArrayRector\Fixture;

class AppliesToHttpRequests
{
public function store(\Illuminate\Http\Request $request)
{
$request->validate([
'name' => 'required|string',
]);
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ValidationRuleArrayStringValueToArrayRector\Fixture;

class AppliesToHttpRequests
{
public function store(\Illuminate\Http\Request $request)
{
$request->validate([
'name' => ['required', 'string'],
]);
}
}

?>