Blog Detail

16

Nov
Rector Rules for Laravel for Refactors the Code of Your App cover image

arrow_back Rector Rules for Laravel for Refactors the Code of Your App

Rector instantly upgrades and refactors the PHP code of your application. This package is a Rector extension developed by the Laravel community.

Installation

You can install the RectorLaravel package as a dependency:

composer require driftingly/rector-laravel --dev

Use Sets

To add a set to your config, use RectorLaravel\Set\LaravelSetList class and pick one of the constants:

use RectorLaravel\Set\LaravelSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->sets([
        LaravelSetList::LARAVEL_90
    ]);
};

Laravel Rules for Rector

AddArgumentDefaultValueRector

Adds default value for arguments in defined methods.

configure it!

class: RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
use RectorLaravel\ValueObject\AddArgumentDefaultValue;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(AddArgumentDefaultValueRector::class, [
        AddArgumentDefaultValueRector::ADDED_ARGUMENTS => [
            new AddArgumentDefaultValue('SomeClass', 'someMethod', 0, false),
        ],
    ]);
};
 class SomeClass
 {
-    public function someMethod($value)
+    public function someMethod($value = false)
     {
     }
 }

AddGenericReturnTypeToRelationsRector

Add generic return type to relations in child of Illuminate\Database\Eloquent\Model

class: RectorLaravel\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector

 use App\Account;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\HasMany;

 class User extends Model
 {
+    /** @return HasMany<Account> */
     public function accounts(): HasMany
     {
         return $this->hasMany(Account::class);
     }
 }

AddGuardToLoginEventRector

Add new $guard argument to Illuminate\Auth\Events\Login

class: RectorLaravel\Rector\New_\AddGuardToLoginEventRector

 use Illuminate\Auth\Events\Login;

 final class SomeClass
 {
     public function run(): void
     {
-        $loginEvent = new Login('user', false);
+        $guard = config('auth.defaults.guard');
+        $loginEvent = new Login($guard, 'user', false);
     }
 }

AddParentRegisterToEventServiceProviderRector

Add parent::register(); call to register() class method in child of Illuminate\Foundation\Support\Providers\EventServiceProvider

class: RectorLaravel\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector

 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

 class EventServiceProvider extends ServiceProvider
 {
     public function register()
     {
+        parent::register();
     }
 }

There are 25 Laravel Rector Rules in this package. You can see all these rules on Github.

Published at : 16-11-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project