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_110,
]);
The sets in RectorLaravel\Set\LaravelSetList
only contain changes related to a specific version upgrade.
For example, the rules in LaravelSetList::LARAVEL_110
apply when upgrading from Laravel 10 to Laravel 11.
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,
...
]);
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 implementsConfigurableRectorInterface
You 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.