|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Rector\Config\RectorConfig; |
| 6 | +use Rector\Set\ValueObject\LevelSetList; |
| 7 | +use Rector\Set\ValueObject\SetList; |
| 8 | +use Rector\Symfony\Set\SymfonySetList; |
| 9 | +use Rector\Doctrine\Set\DoctrineSetList; |
| 10 | +use Rector\PHPUnit\Set\PHPUnitSetList; |
| 11 | + |
| 12 | +return RectorConfig::configure() |
| 13 | + // Define paths to be analyzed by Rector |
| 14 | + ->withPaths([ |
| 15 | + __DIR__ . '/src', |
| 16 | + __DIR__ . '/tests', |
| 17 | + ]) |
| 18 | + |
| 19 | + // For Symfony projects, load the container XML to enable smarter refactoring based on service definitions. |
| 20 | + // Adjust the path according to your Symfony version and environment. |
| 21 | + // Typically, it's located at var/cache/{env}/App_Kernel{Env}DebugContainer.xml |
| 22 | + ->withSymfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml') |
| 23 | + |
| 24 | + // Apply framework-specific rules based on Composer dependencies. |
| 25 | + // This enables automatic refactoring for frameworks like Symfony, Doctrine, etc., including version upgrades. |
| 26 | + ->withComposerBased( |
| 27 | + symfony: true, |
| 28 | + doctrine: true, |
| 29 | + phpunit: true, |
| 30 | + twig: true |
| 31 | + ) |
| 32 | + |
| 33 | + // Apply common code quality improvements and PHP version upgrade sets. |
| 34 | + // withPreparedSets() offers a concise way to enable frequently used rule sets. |
| 35 | + ->withPreparedSets( |
| 36 | + deadCode: true, |
| 37 | + codeQuality: true, |
| 38 | + codingStyle: true, |
| 39 | + typeDeclarations: true, |
| 40 | + privatization: true, |
| 41 | + ) |
| 42 | + |
| 43 | + // Apply rule sets tailored to the latest PHP version. |
| 44 | + // Choose the appropriate LevelSetList for your project's PHP version (e.g., LevelSetList::UP_TO_PHP_84). |
| 45 | + // withSets() is used to explicitly add specific sets not covered by withPreparedSets(). |
| 46 | + ->withSets([ |
| 47 | + LevelSetList::UP_TO_PHP_84, |
| 48 | + // SetList::EARLY_RETURN, // Example: Enable early return refactoring |
| 49 | + // SetList::INSTANCEOF, // Example: Optimize instanceof checks |
| 50 | + // SetList::STRICT_BOOLEANS, // Example: Enforce strict boolean usage |
| 51 | + // --------------------------------------------- |
| 52 | + SymfonySetList::CONFIGS, |
| 53 | + SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES, // Example: Convert Symfony annotations to PHP attributes |
| 54 | + SymfonySetList::SYMFONY_CODE_QUALITY, |
| 55 | + SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION, |
| 56 | + // SymfonySetList::SYMFONY_73, // Example: Specific Symfony 7.3 upgrade rules if needed |
| 57 | + // --------------------------------------------- |
| 58 | + DoctrineSetList::DOCTRINE_CODE_QUALITY, |
| 59 | + // --------------------------------------------- |
| 60 | + PHPUnitSetList::PHPUNIT_120, // Example: PHPUnit 12.0 upgrade rules |
| 61 | + ]) |
| 62 | + |
| 63 | + // Convert annotations to native PHP attributes (for PHP 8.0+). |
| 64 | + // This is useful for migrating from Doctrine or Symfony annotations. |
| 65 | + ->withAttributesSets( |
| 66 | + doctrine: true, |
| 67 | + symfony: true, |
| 68 | + ) |
| 69 | + |
| 70 | + // Add individual Rector rules as needed |
| 71 | + // ->withRules([ |
| 72 | + // YourCustomRectorRule::class, |
| 73 | + // ]) |
| 74 | + |
| 75 | + // Optimize import statements (sort and remove unused 'use' statements) |
| 76 | + ->withImportNames() |
| 77 | + |
| 78 | + // Exclude specific files or directories from analysis |
| 79 | + ->withSkip([ |
| 80 | + // __DIR__ . '/src/SomeLegacyCode.php', |
| 81 | + // SetList::DEAD_CODE => [ |
| 82 | + // __DIR__ . '/src/SpecificFileWithFalsePositive.php', |
| 83 | + // ], |
| 84 | + ]); |
0 commit comments