Rector upgrades rules for Laravel Framework
driftingly/rector-laravel is a Laravel package for rector upgrades rules for laravel framework.
It currently has 1.226 GitHub stars and 14.851.855 downloads on Packagist (latest version 2.5.0).
Install it with composer require driftingly/rector-laravel.
Discover more Laravel packages by driftingly
or browse all Laravel packages to compare alternatives.
Last updated
See all available Laravel rules here. This list includes even the rules that are not yet released, but are available under the dev-main branch.
You can also find the released rules on the Rector Find Rule page.
This package is a Rector extension developed by the Laravel community.
Rules for additional first party packages are included as well e.g. Cashier and Livewire.
Install as a dev dependency:
composer require --dev driftingly/rector-laravel
To automatically apply the correct rules depending on the version of Laravel (or other packages) you are currently on (as detected in the composer.json), use the following:
<?php declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetProvider;
return RectorConfig::configure()
->withSetProviders(LaravelSetProvider::class)
->withComposerBased(laravel: true, /** other options */);
To manually add a version set to your config, use RectorLaravel\Set\LaravelLevelSetList and pick the constant that matches your target version.
Sets for higher versions include sets for lower versions.
<?php declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelLevelSetList;
return RectorConfig::configure()
->withSets([
LaravelLevelSetList::UP_TO_LARAVEL_130,
]);
The sets in RectorLaravel\Set\LaravelSetList only contain changes related to a specific version upgrade.
For example, the rules in LaravelSetList::LARAVEL_130 apply when upgrading from Laravel 12 to Laravel 13.
To improve different aspects of your code, use the sets in RectorLaravel\Set\LaravelSetList.
<?php declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetList;
return RectorConfig::configure()
->withSets([
LaravelSetList::LARAVEL_CODE_QUALITY,
LaravelSetList::LARAVEL_COLLECTION,
...
]);
| Set | Purpose |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| LaravelSetList::LARAVEL_ARRAYACCESS_TO_METHOD_CALL | Converts uses of things like $app['config'] to $app->make('config'). |
| LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL | Converts most string and array helpers into Str and Arr Facades' static calls.
https://laravel.com/docs/12.x/facades#facades-vs-helper-functions |
| LaravelSetList::LARAVEL_CODE_QUALITY | Replaces magical call on $this->app["something"] to standalone variable with PHPDocs. |
| LaravelSetList::LARAVEL_COLLECTION | Improves the usage of Laravel Collections by using simpler, more efficient, or more readable methods. |
| LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME | Changes the string or class const used for a service container make call.
https://laravel.com/docs/12.x/container#the-make-method |
| LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER | Transforms magic method calls on Eloquent Models into corresponding Query Builder method calls.
https://laravel.com/docs/12.x/eloquent |
| LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES | Replaces Facade aliases with full Facade names.
https://laravel.com/docs/12.x/facades#facade-class-reference |
| LaravelSetList::LARAVEL_FACTORIES | Makes working with Laravel Factories easier and more IDE friendly. |
| LaravelSetList::LARAVEL_IF_HELPERS | Replaces abort(), report(), throw statements inside conditions with abort_if(), report_if(), throw_if() function calls.
https://laravel.com/docs/12.x/helpers#method-abort-if |
| LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES | Migrates Eloquent legacy model factories (with closures) into class based factories.
https://laravel.com/docs/8.x/releases#model-factory-classes |
| LaravelSetList::LARAVEL_STATIC_TO_INJECTION | Replaces Laravel's Facades with Dependency Injection.
https://tomasvotruba.com/blog/2019/03/04/how-to-turn-laravel-from-static-to-dependency-injection-in-one-day/
https://laravel.com/docs/12.x/facades#facades-vs-dependency-injection |
| LaravelSetList::LARAVEL_TESTING | Improves Laravel testing by converting deprecated methods and adding better assertions. |
| LaravelSetList::LARAVEL_TYPE_DECLARATIONS | Adds type hints and generic return types to improve Laravel code type safety. |
These rules require configuration and must be added manually to your rector.php file.
<?php declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
return RectorConfig::configure()
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
'dd', 'dump', 'var_dump'
]);
| Rule | Description |
|------|-------------|
| RemoveDumpDataDeadCodeRector | Removes debug function calls like dd(), dump(), etc. from code. Configure with an array of function names to remove (default: ['dd', 'dump']). |
| RouteActionCallableRector | Converts route action strings like 'UserController@index' to callable arrays [UserController::class, 'index']. Configure with NAMESPACE for controller namespace and ROUTES for file-specific namespaces. |
| WhereToWhereLikeRector | Converts where('column', 'like', 'value') to whereLike('column', 'value') calls. Configure with USING_POSTGRES_DRIVER boolean to handle PostgreSQL vs MySQL differences. |
These rules are more opinionated and are not included in any sets by default.
<?php declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Rector\MethodCall\ResponseHelperCallToJsonResponseRector;
return RectorConfig::configure()
->withRules([
ResponseHelperCallToJsonResponseRector::class,
]);
| Rule | Description |
|------|-------------|
| RemoveModelPropertyFromFactoriesRector | Removes the $model property from Factories. |
| ResponseHelperCallToJsonResponseRector | Converts response()->json() to new JsonResponse(). |
| MinutesToSecondsInCacheRector | Change minutes argument to seconds in cache methods. |
| UseComponentPropertyWithinCommandsRector | Use $this->components property within commands. |
| UseForwardsCallsTraitRector | Replaces the use of call_user_func and call_user_func_array method with the CallForwarding trait. |
| EmptyToBlankAndFilledFuncRector | Converts empty() to blank() and filled() |
You can create a new rule using the composer script:
composer make:rule -- YourRuleName
This will generate a new rule class in src/Rector/ along with the corresponding test files.
--configurable or -c: Create a configurable rule that implements ConfigurableRectorInterfaceYou can specify a subdirectory structure by including slashes in the rule name:
composer make:rule -- If_/ConvertIfToWhen
This will create a rule in the src/Rector/If_/ directory with the namespace RectorLaravel\Rector\If_.
Remember to always add -- before the arguments when using the composer script. This separator tells Composer that the following arguments should be passed to the script rather than being interpreted as Composer arguments.
Thank you everyone who works so hard on improving this package:
A special thank you to Caneco for designing the logo!
Rector is a tool that we develop and share for free, so anyone can automate their refactoring. But not everyone has dozens of hours to understand complexity of abstract-syntax-tree in their own time. That's why we provide commercial support - to save your time.
Would you like to apply Rector on your code base but don't have time for the struggle with your project? Hire the Rector team to get there faster.
Not everyone has time to understand Rector and AST complexity. You can speed up the process by reading the book The Power of Automated Refactoring. Not only will it help you learn and understand Rector but it supports the project as well.