contao/contao-rector
Automate Contao upgrades with Rector. This package provides ready-made Rector rules and configuration to refactor Contao projects for newer versions, helping you modernize code, remove deprecations, and apply consistent changes safely across your codebase.
Installation Add the package to your Laravel project via Composer:
composer require --dev contao/contao-rector
Ensure rector/rector is also installed (required dependency).
Basic Setup
Configure Rector in rector.php (create if missing):
use Contao\Rector\ContaoSetList;
use Rector\Core\Configuration\Option;
return [
Option::SET_LISTS => [
ContaoSetList::CONTAO_4_13,
],
Option::AUTO_IMPORT_NAMES => true,
];
First Use Case
Run Rector on a Contao-specific file (e.g., a custom tl_content class):
vendor/bin/rector process src/Contao
Verify changes with Git diff or git status.
Pre-Commit Hook
Add Rector to your CI/CD or Git hooks (e.g., .github/workflows/rector.yml):
jobs:
rector:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer require --dev contao/contao-rector
- run: vendor/bin/rector process --dry-run
Laravel-Specific Patterns
AppServiceProvider:
// Before
$this->app->bind('contao.model', function() { ... });
// After (auto-updated by Rector)
$this->app->singleton('contao.model', function() { ... });
onLoad callbacks to Laravel’s booted events:
// Before
System::registerCallback(['MyClass', 'handleEvent']);
// After (Rector-generated)
Event::listen('contao.load', [MyClass::class, 'handleEvent']);
Partial Runs Target specific files/directories for incremental updates:
vendor/bin/rector process src/Contao/Modules --dry-run
Contao Version Mismatch
CONTAO_4_13). Running on CONTAO_4_12 may fail or produce incorrect output.ContaoSetList in rector.php.False Positives in Legacy Code
system/config/autoload.php) as outdated.Option::EXCLUDE_PATHS:
Option::EXCLUDE_PATHS => [__DIR__.'/vendor/contao/core'],
Database Awareness
tl_*) or DCA configurations. Manual review is required for schema changes.--dry-run first to preview changes:
vendor/bin/rector process --dry-run
vendor/bin/rector process -vvv
Custom Rules Extend Rector with Contao-specific rules by creating a custom rule set:
// config/rector.php
Option::RULES => [
new Set\RectorSet([new Rule\YourCustomContaoRule()]),
];
Laravel Contao Hybrid Projects
Use Rector to bridge Contao’s tl_* table access with Laravel’s Eloquent:
// Before (Contao)
$model = Model::findByPk(42);
// After (Rector + custom rule)
$model = \App\Models\Contao\Page::find(42);
Configuration Quirks
rector.php:
vendor/bin/rector clear-cache
How can I help you explore Laravel packages today?