Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Rector Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Add the package via Composer in your Laravel project:

    composer require --dev contao/rector
    

    Ensure rector/rector is also installed (required dependency).

  2. 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;
    
  3. 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.


Implementation Patterns

Workflows

  1. 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);
    
    • Targets: tl_* table references, tl_* prefix removal, tl_* to Model\* conversions.
  2. 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);
    
  3. Custom Rule Extension Extend Contao rules for project-specific needs:

    $rectorConfig->ruleWithConfiguration(
        new contao\Rector\Rename\RenameTlModelToModel(),
        [new ParameterProvider('tl_article', 'Model\Article')]
    );
    

Integration Tips

  • CI/CD Pipelines: Run Rector in a GitHub Action for pre-commit checks:
    - name: Run Rector
      run: vendor/bin/rector process --dry-run
    
  • Partial Processing: Target specific files/directories:
    $rectorConfig->paths([__DIR__ . '/app/Http/Controllers/Contao']);
    
  • Dry Runs: Always test changes first:
    vendor/bin/rector process --dry-run
    

Gotchas and Tips

Pitfalls

  1. Over-Aggressive Refactoring

    • Issue: Blindly applying CONTAO_40 rules may break custom Contao extensions.
    • Fix: Use --dry-run and review changes manually. Exclude vendor paths:
      $rectorConfig->excludePaths([__DIR__ . '/vendor']);
      
  2. PHP Version Mismatch

    • Issue: Contao rules assume PHP 8.0+. Errors may occur on PHP 7.4.
    • Fix: Set correct PHP version in config:
      $rectorConfig->phpVersion(PhpVersion::PHP_74);
      
  3. Database Schema Dependencies

    • Issue: Rules like tl_* to Model\* require matching database tables.
    • Fix: Run database migrations or mock models in tests before refactoring.

Debugging

  • Verbose Output: Enable for detailed rule execution:
    vendor/bin/rector process --verbose
    
  • Rule Isolation: Test individual rules:
    vendor/bin/rector process --rule=contao\Rector\Rename\RenameTlModelToModel
    

Extension Points

  1. 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());
    
  2. Configuration Overrides Override Contao rules via rector.php:

    $rectorConfig->ruleWithConfiguration(
        new contao\Rector\Rename\RenameTlModelToModel(),
        [new ParameterProvider('tl_', 'CustomPrefix\')]
    );
    
  3. Parallel Processing Speed up large codebases with parallel execution:

    $rectorConfig->parallel();
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor