Find the best Rector rule to solve your problem. Searching through 792 rules.
Found 24 rules:
Changes orderBy() to latest() or oldest()
use Illuminate\Database\Eloquent\Builder;
$column = 'tested_at';
-$builder->orderBy('created_at');
-$builder->orderBy('created_at', 'desc');
-$builder->orderBy('submitted_at');
-$builder->orderByDesc('submitted_at');
-$builder->orderBy($allowed_variable_name);
+$builder->oldest();
+$builder->latest();
+$builder->oldest('submitted_at');
+$builder->latest('submitted_at');
+$builder->oldest($allowed_variable_name);
$builder->orderBy($unallowed_variable_name);
$builder->orderBy('unallowed_column_name');
Reverse conditionable method calls
-$conditionable->when(!$condition, function () {});
+$conditionable->unless($condition, function () {});
Replace redirect()->route("home") and Redirect::route("home") with to_route("home")
use Illuminate\Support\Facades\Redirect;
class MyController
{
public function store()
{
- return redirect()->route('home')->with('error', 'Incorrect Details.')
+ return to_route('home')->with('error', 'Incorrect Details.')
}
public function update()
{
- return Redirect::route('home')->with('error', 'Incorrect Details.')
+ return to_route('home')->with('error', 'Incorrect Details.')
}
}
Convert string validation rules into arrays for Laravel's Validator.
Validator::make($data, [
- 'field' => 'required|nullable|string|max:255',
+ 'field' => ['required', 'nullable', 'string', 'max:255'],
]);
Replace redirect()->back() and Redirect::back() with back()
use Illuminate\Support\Facades\Redirect;
class MyController
{
public function store()
{
- return redirect()->back()->with('error', 'Incorrect Details.')
+ return back()->with('error', 'Incorrect Details.')
}
public function update()
{
- return Redirect::back()->with('error', 'Incorrect Details.')
+ return back()->with('error', 'Incorrect Details.')
}
}
Use Sleep::sleep() and Sleep::usleep() instead of the sleep() and usleep() function.
-sleep(5);
+\Illuminate\Support\Sleep::sleep(5);
Removes redundant with helper calls
-with(new Object())->something();
+(new Object())->something();
Use today() instead of now()->startOfDay()
-$now = now()->startOfDay();
+$now = today();
changes use of a new throw instance to class string
-throw_if($condition, new MyException('custom message'));
+throw_if($condition, MyException::class, 'custom message');
Removes redundant value helper calls
-value(new Object())->something();
+(new Object())->something();
Swap the use of NotBooleans used with filled() and blank() to the correct helper.
-!filled([]);
-!blank([]);
+blank([]);
+filled([]);
Refactor Carbon static method calls to use the Date facade instead.
-use Carbon\Carbon;
+use Illuminate\Support\Facades\Date;
-Carbon::now();
-Carbon::parse('2024-01-01');
+Date::now();
+Date::parse('2024-01-01');
Use the event or dispatch helpers instead of the static dispatch method.
-ExampleEvent::dispatch($email);
+event(new ExampleEvent($email));
Convert simple calls to optional helper to use the nullsafe operator
-optional($user)->getKey();
-optional($user)->id;
+$user?->getKey();
+$user?->id;
// macro methods
optional($user)->present()->getKey();
Replace magical call on $this->app["something"] to standalone type assign variable
class SomeClass
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
private $app;
public function run()
{
- $validator = $this->app['validator']->make('...');
+ /** @var \Illuminate\Validation\Factory $validationFactory */
+ $validationFactory = $this->app['validator'];
+ $validator = $validationFactory->make('...');
}
}
Use Str::startsWith() or Str::endsWith() instead of substr() === $str
-if (substr($str, 0, 3) === 'foo') {
+if (Str::startsWith($str, 'foo')) {
// do something
}
Replace app environment comparison with parameter or method call
-$app->environment() === 'local';
-$app->environment() !== 'production';
-$app->environment() === 'testing';
-in_array($app->environment(), ['local', 'testing']);
+$app->isLocal();
+! $app->isProduction();
+$app->environment('testing');
+$app->environment(['local', 'testing']);
Apply default instead of null coalesce
-custom_helper('app.name') ?? 'Laravel';
+custom_helper('app.name', 'Laravel');
Convert migrations to anonymous classes.
use Illuminate\Database\Migrations\Migration;
-class CreateUsersTable extends Migration
+return new class extends Migration
{
// ...
-}
+};
Makes Model attributes and scopes protected
class User extends Model
{
- public function foo(): Attribute
+ protected function foo(): Attribute
{
return Attribute::get(fn () => $this->bar);
}
#[Scope]
- public function active(Builder $query): Builder
+ protected function active(Builder $query): Builder
{
return $query->where('active', true);
}
}
Change PHP session usage to Session Facade methods
-$_SESSION['key'];
-$_SESSION['key'] = 'value';
-$_SESSION;
-session_regenerate_id();
-session_unset();
-session_destroy();
-session_start();
-unset($_SESSION['key']);
-isset($_SESSION['key'])
+\Illuminate\Support\Facades\Session::get('key');
+\Illuminate\Support\Facades\Session::put('key', 'value');
+\Illuminate\Support\Facades\Session::all();
+\Illuminate\Support\Facades\Session::regenerate();
+\Illuminate\Support\Facades\Session::flush();
+\Illuminate\Support\Facades\Session::destroy();
+\Illuminate\Support\Facades\Session::start();
+\Illuminate\Support\Facades\Session::forget('key');
+\Illuminate\Support\Facades\Session::has('key');
Change server variable to Request facade's server method
-$_SERVER['VARIABLE'];
+\Illuminate\Support\Facade\Request::server('VARIABLE');
Change env variable to env static call
-$_ENV['APP_NAME'];
+\Illuminate\Support\Env::get('APP_NAME');
Change request variable definition in Facade
-$_GET['value'];
-$_POST['value'];
-$_REQUEST['value'];
-$_POST;
-$_GET;
-$_REQUEST;
-isset($_GET['value']);
-isset($_POST['value']);
-isset($_REQUEST['value']);
+\Illuminate\Support\Facades\Request::query('value');
+\Illuminate\Support\Facades\Request::post('value');
+\Illuminate\Support\Facades\Request::input('value');
+\Illuminate\Support\Facades\Request::query();
+\Illuminate\Support\Facades\Request::post();
+\Illuminate\Support\Facades\Request::all();
+\Illuminate\Support\Facades\Request::query('value') !== null;
+\Illuminate\Support\Facades\Request::post('value') !== null;
+\Illuminate\Support\Facades\Request::exists('value');