contao/rector
Automate Contao CMS code upgrades with Rector rules and presets. contao/rector helps refactor PHP codebases to newer Contao and PHP versions, applying safe transformations to reduce manual migration work and keep projects consistent.
Installation Add the package via Composer in your Laravel project:
composer require --dev contao/rector
Ensure rector/rector is also installed (required dependency).
Basic Setup
Create a rector.php config file in your project root (or extend an existing one):
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Symplify\EasyTesting\Parameter\ParameterProvider;
$rectorConfig = new RectorConfig();
$rectorConfig->paths([
__DIR__ . '/app',
__DIR__ . '/tests',
]);
$rectorConfig->phpVersion(PhpVersion::PHP_80);
// Load Contao-specific rules
$rectorConfig->importNames();
$rectorConfig->importShortClasses();
$rectorConfig->import(contao\Rector\Set\ContaoSetList::CONTAO_40);
return $rectorConfig;
First Use Case Run Rector to auto-fix Contao-specific code:
vendor/bin/rector process
Focus on Contao 4.x compatibility if migrating or maintaining legacy code.
Legacy Code Modernization
Use CONTAO_40 set to update Contao 3.x syntax to Contao 4.x standards:
$rectorConfig->import(contao\Rector\Set\ContaoSetList::CONTAO_40);
tl_* table references, tl_* prefix removal, tl_* to Model\* conversions.Laravel-Contao Integration
Combine with Laravel’s illuminate/support rules for hybrid projects:
$rectorConfig->import(Rector\Set\LaravelSetList::LARAVEL_80);
$rectorConfig->import(contao\Rector\Set\ContaoSetList::CONTAO_40);
Custom Rule Extension Extend Contao rules for project-specific needs:
$rectorConfig->ruleWithConfiguration(
new contao\Rector\Rename\RenameTlModelToModel(),
[new ParameterProvider('tl_article', 'Model\Article')]
);
- name: Run Rector
run: vendor/bin/rector process --dry-run
$rectorConfig->paths([__DIR__ . '/app/Http/Controllers/Contao']);
vendor/bin/rector process --dry-run
Over-Aggressive Refactoring
CONTAO_40 rules may break custom Contao extensions.--dry-run and review changes manually. Exclude vendor paths:
$rectorConfig->excludePaths([__DIR__ . '/vendor']);
PHP Version Mismatch
$rectorConfig->phpVersion(PhpVersion::PHP_74);
Database Schema Dependencies
tl_* to Model\* require matching database tables.vendor/bin/rector process --verbose
vendor/bin/rector process --rule=contao\Rector\Rename\RenameTlModelToModel
Custom Rules
Extend contao\Rector\Set\ContaoSetList by creating a new rule class:
namespace App\Rector;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
class CustomContaoRector extends AbstractRector {
public function refactor(Node $node): ?Node {
// Custom logic here
}
}
Register it in rector.php:
$rectorConfig->rule(new \App\Rector\CustomContaoRector());
Configuration Overrides
Override Contao rules via rector.php:
$rectorConfig->ruleWithConfiguration(
new contao\Rector\Rename\RenameTlModelToModel(),
[new ParameterProvider('tl_', 'CustomPrefix\')]
);
Parallel Processing Speed up large codebases with parallel execution:
$rectorConfig->parallel();
How can I help you explore Laravel packages today?