Rector is a PHP tool that you can run on any PHP project to instantly upgrade or automate refactoring. Rector for Laravel is a Rector extension developed by the Laravel community with some Laravel-specific rules that you can use on your Laravel applications.
To apply the correct rules automatically depending on the version of Laravel used in your application, you can use:
<?php declare(strict_types=1); use Rector\Config\RectorConfig;use RectorLaravel\Set\LaravelSetProvider; return RectorConfig::configure() ->withSetProviders(LaravelSetProvider::class) ->withComposerBased(laravel: true, /** other options */);
However, if you'd like to do step-by-step upgrades and add only specific rulesets, then you can manually add the target version set (e.g., Laravel v11 to v12) to your Rector config:
<?php declare(strict_types=1); use Rector\Config\RectorConfig;use RectorLaravel\Set\LaravelLevelSetList; return RectorConfig::configure() ->withSets([ LaravelLevelSetList::UP_TO_LARAVEL_120, ]);
Improving your Laravel code with specific sets in LaravelSetList is also possible:
<?php declare(strict_types=1); use Rector\Config\RectorConfig;use RectorLaravel\Set\LaravelSetList; return RectorConfig::configure() ->withSets([ LaravelSetList::LARAVEL_IF_HELPERS, LaravelSetList::LARAVEL_TYPE_DECLARATIONS, ... ]);
For example, LaravelSetList::LARAVEL_IF_HELPERS will replace abort(), report(), throw statements inside conditions with abort_if(), report_if(), throw_if() function calls. While LaravelSetList::LARAVEL_TYPE_DECLARATIONS will add type hints and generic return types to improve Laravel code type safety. And there are many more.
Additionally, there are numerous opinionated and configurable rules that you can apply.
A list of all the rules (even those not yet released) is available in the documentation.
Learn more about this package and view the source code on GitHub.