How to configure Rector for Laravel 13 and use it to upgrade
A Rector Laravel config for Laravel 13 with rector-laravel sets, dry-run versus process, running it before Pint, CI, and a safe framework upgrade workflow.
Updated
On this page
A working Rector Laravel config needs two packages, rector/rector and driftingly/rector-laravel, and one rector.php that lists your paths, enables the PHP sets, a few prepared sets and the Laravel sets you want. Run vendor/bin/rector --dry-run to see changes and vendor/bin/rector process to apply them, always before Pint. For a framework upgrade, withComposerBased(laravel: true) applies every Laravel version set up to the version you have installed.
This guide covers install, the config, dry-run versus fix, ordering with Pint, CI and a safe upgrade workflow. Commands assume Sail.
Install Rector and rector-laravel
./vendor/bin/sail composer require --dev rector/rector driftingly/rector-laravel
rector/rector is the engine, Rector 2 at the time of writing. driftingly/rector-laravel is the community extension with Laravel-specific rules and sets, under the RectorLaravel\ namespace.
Write rector.php
A config for a fresh Laravel 13 app:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetList;
return RectorConfig::configure()
->withPaths([
__DIR__.'/app',
__DIR__.'/database',
__DIR__.'/routes',
__DIR__.'/tests',
])
->withPhpSets()
->withPreparedSets(
codeQuality: true,
deadCode: true,
earlyReturn: true,
typeDeclarations: true,
)
->withSets([
LaravelSetList::LARAVEL_CODE_QUALITY,
LaravelSetList::LARAVEL_COLLECTION,
LaravelSetList::LARAVEL_IF_HELPERS,
]);
What each part does:
| Call | Effect |
|---|---|
withPaths() |
What Rector may rewrite. Leave out config, bootstrap/cache, storage, vendor |
withPhpSets() |
Modern PHP syntax up to the version in composer.json require.php |
codeQuality |
Simplifies expressions and conditions |
deadCode |
Removes unused code, unreachable branches, useless docblocks |
earlyReturn |
Replaces nested if/else with early returns |
typeDeclarations |
Adds parameter, return and property types it can prove |
LARAVEL_CODE_QUALITY |
Laravel idioms, for example cleaner helper and facade usage |
LARAVEL_COLLECTION |
Better collection method usage |
LARAVEL_IF_HELPERS |
abort_if, throw_if and friends instead of if + abort |
withPhpSets() without arguments reads the PHP constraint from composer.json. Pin it there ("php": "^8.5") and Rector targets the right PHP version automatically.
Other Laravel sets worth knowing, opt in one at a time:
| Set | Use it when |
|---|---|
LARAVEL_TYPE_DECLARATIONS |
You want Laravel-aware type declarations added |
LARAVEL_TESTING |
You want test helpers modernized |
LARAVEL_FACTORIES |
You want factory code modernized |
LARAVEL_FACADE_ALIASES_TO_FULL_NAMES |
Code uses global facade aliases like \Cache |
LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER |
You want explicit query() calls instead of static magic |
LARAVEL_STATIC_TO_INJECTION |
You want facades replaced by constructor injection |
The last one changes architecture, not just syntax. Do not enable it on a whim.
Dry-run versus process
./vendor/bin/sail bin rector --dry-run # print a diff, change nothing
./vendor/bin/sail bin rector process # apply the changes
--dry-run exits non-zero when Rector would change something. That makes it a check: it belongs in composer quality and CI. process writes files. It belongs on a developer's machine, with a clean git tree so you can review the result as a diff.
To try a single path or a single change before running it on the whole app, pass the path on the command line:
./vendor/bin/sail bin rector process app/Models --dry-run
Two practical points:
- Run it twice on a first pass. One rewrite can make another rule apply. On a fresh skeleton or right after enabling a new set, run
processuntil the second run reports no changes. - Clear the cache after a config change. Rector caches per file. After editing
rector.phpor after another tool rewrites files, run with--clear-cache.
./vendor/bin/sail bin rector process --clear-cache
Run Rector before Pint
Order matters. Rector prints the code it changes with its own printer, which does not know your Pint rules. Pint then normalizes formatting. The reverse order leaves Rector's formatting in place and pint --test fails.
./vendor/bin/sail composer rector:fix
./vendor/bin/sail composer pint:fix
For the checks, order does not matter, since neither writes:
./vendor/bin/sail composer quality
Pint also rewrites files, so any Rector cache from before the Pint run is stale. If rector:dry after Pint is slower than expected or reports odd results, clear the Rector cache once.
Rector and Pint overlap in a few places, such as imports and strict types. Let Pint own formatting and imports (global_namespace_import, declare_strict_types in pint.json) and let Rector own code changes. See Laravel Pint config.
Composer scripts
{
"scripts": {
"rector:dry": "vendor/bin/rector --dry-run",
"rector:fix": "vendor/bin/rector process",
"pint:dry": "vendor/bin/pint --test",
"pint:fix": "vendor/bin/pint",
"phpstan": "vendor/bin/phpstan analyse",
"quality": [
"@pint:dry",
"@phpstan",
"@rector:dry"
]
}
}
quality only checks. The :fix scripts are what you run locally before committing.
Run it in CI
name: Rector
on:
push:
pull_request:
jobs:
rector:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.5'
coverage: none
- run: composer install --no-interaction --prefer-dist
- run: vendor/bin/rector --dry-run
A failing job prints the diff Rector wants to apply. The fix is always the same: run composer rector:fix and composer pint:fix locally and commit. Never let CI run process and push the result. Code changes need a human or an agent to review them.
Safe Laravel upgrade workflow
rector-laravel ships a set per Laravel version. LaravelSetList::LARAVEL_130 contains the rules for upgrading from Laravel 12 to Laravel 13. The recommended way to apply them is version detection:
return RectorConfig::configure()
->withPaths([
__DIR__.'/app',
__DIR__.'/database',
__DIR__.'/routes',
__DIR__.'/tests',
])
->withComposerBased(laravel: true)
->withPhpSets();
withComposerBased(laravel: true) reads the installed version and applies the Laravel version sets up to it. It also covers Faker, Livewire and Cashier when they are installed. The older LaravelLevelSetList::UP_TO_LARAVEL_130 style still appears in examples but is deprecated in favor of this.
The workflow:
- Start green.
composer qualityand the test suite pass on the current version. Commit. - Bump the framework in
composer.jsonand runcomposer update. Fix anything Composer refuses. - Read the official upgrade guide. Rector automates the mechanical changes. Config, behavior and dependency changes are on you.
- Run Rector with version sets.
rector --dry-runfirst, read the diff, thenrector process. - Run Pint, then commit the automated changes alone. Keep them separate from manual fixes so review stays possible.
- Run PHPStan and the tests. Fix what remains by hand.
- Commit again and repeat per major version. Do not jump two majors in one step.
Keep the version-set config in a branch or a temporary change if you do not want it running on every rector:dry. Leaving withComposerBased in place is harmless once the code is upgraded, since already-applied rules find nothing to change.
Common problems
| Symptom | Cause | Fix |
|---|---|---|
pint --test fails right after rector process |
Wrong order | Run Pint after Rector |
rector:dry fails again on a second run |
Rules chained | Run process until it converges |
| Rector rewrites a published vendor stub | It is in app/ |
It is your code now; review and keep or skip it |
| A rule makes an unwanted change | Set too broad | Add ->withSkip([RuleClass::class]) |
| Stale results after config edits | Cache | --clear-cache |
Next steps
Rector works best with a type checker behind it: PHPStan with Larastan. The full list of fresh-project steps is in the Laravel 13 setup checklist.
Skip the setup
kalimera installs rector/rector and driftingly/rector-laravel on every new app, publishes the rector.php above, adds rector:dry, rector:fix and quality scripts, then runs Rector twice over the fresh skeleton, runs Pint after it and clears Rector's cache before checking that composer quality is green.
All of it, in one unattended run.
kalimera makes these choices once, upfront, then scaffolds a fresh Laravel app with every piece wired and committed.