Rector upgrades rules for Laravel
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:
withComposerBased(laravel: true, /** other options */);
This registers RectorLaravel\Set\LaravelSetList::COMPOSER_BASED, which applies the version sets of Laravel (and of Faker, Livewire, and Cashier, when installed) up to the installed version.
Deprecated: the version set lists below are deprecated in favor of
withComposerBased(laravel: true)above, which detects the installed version for you. They will be removed in a future major version.
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.
withSets([
LaravelLevelSetList::UP_TO_LARAVEL_130,
]);
The version 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.
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. |
|
| 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.
|
| LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER | Transforms magic method calls on Eloquent Models into corresponding Query Builder method calls.
|
| LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES | Replaces Facade aliases with full Facade names.
|
| 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.
|
| LaravelSetList::LARAVEL_LEGACY_FACTORIES_TO_CLASSES | Migrates Eloquent legacy model factories (with closures) into class based factories.
|
| LaravelSetList::LARAVEL_STATIC_TO_INJECTION | Replaces Laravel's Facades with 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.
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.
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:
No open issues yet, or sync has not completed.