create-rector-config
Rector Configuration Generator
Generates optimized Rector configurations for automated PHP refactoring.
Generated Files
rector.php # Main configuration
Configuration by Use Case
PHP Version Upgrade
<?php
// rector.php
declare(strict_types=1);
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSkip([
__DIR__ . '/src/Infrastructure/Legacy',
__DIR__ . '/src/Infrastructure/Migrations',
])
// PHP 8.4 upgrade
->withPhpSets(php84: true)
// Prepared sets
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
);
Code Quality Focus
<?php
// rector.php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
])
->withPhpSets(php84: true)
->withSets([
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::PRIVATIZATION,
SetList::TYPE_DECLARATION,
SetList::INSTANCEOF,
])
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true,
strictBooleans: true,
);
PHPUnit Migration
<?php
// rector.php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/tests',
])
->withPhpSets(php84: true)
->withSets([
PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
])
->withPreparedSets(
phpunitCodeQuality: true,
);
Symfony Migration
<?php
// rector.php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Doctrine\Set\DoctrineSetList;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(php84: true)
->withSets([
// Symfony 7.x
SymfonySetList::SYMFONY_70,
SymfonySetList::SYMFONY_CODE_QUALITY,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
// Doctrine
DoctrineSetList::DOCTRINE_ORM_30,
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
])
->withSymfonyContainerXml(__DIR__ . '/var/cache/dev/App_KernelDevDebugContainer.xml');
DDD Project Configuration
<?php
// rector.php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src/Domain',
__DIR__ . '/src/Application',
__DIR__ . '/src/Infrastructure',
__DIR__ . '/src/Api',
])
->withSkip([
__DIR__ . '/src/Infrastructure/Migrations',
__DIR__ . '/src/Infrastructure/Legacy',
// Skip certain rules for specific paths
RemoveUnusedPrivateMethodRector::class => [
__DIR__ . '/src/Domain/*/Event/*', // Event handlers may seem unused
],
])
->withPhpSets(php84: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
privatization: true,
earlyReturn: true,
)
// Strict for Domain layer
->withRules([
ReturnTypeFromStrictNativeCallRector::class,
ReturnTypeFromStrictScalarReturnExprRector::class,
])
// Configure individual rules
->withConfiguredRule(
\Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector::class,
[
// Custom naming rules
]
);
Available Set Lists
PHP Version Sets
->withPhpSets(
php53: true, // PHP 5.3 features
php54: true, // PHP 5.4 features
php55: true, // ...
php56: true,
php70: true,
php71: true,
php72: true,
php73: true,
php74: true,
php80: true,
php81: true,
php82: true,
php83: true,
php84: true, // Latest
)
Quality Sets
use Rector\Set\ValueObject\SetList;
->withSets([
SetList::CODE_QUALITY, // General code quality
SetList::CODING_STYLE, // Coding style improvements
SetList::DEAD_CODE, // Remove dead code
SetList::EARLY_RETURN, // Early return patterns
SetList::PRIVATIZATION, // Privatize where possible
SetList::TYPE_DECLARATION, // Add type declarations
SetList::INSTANCEOF, // Instanceof improvements
SetList::STRICT_BOOLEANS, // Strict boolean comparisons
])
Framework Sets
// Symfony
use Rector\Symfony\Set\SymfonySetList;
SymfonySetList::SYMFONY_60
SymfonySetList::SYMFONY_70
SymfonySetList::SYMFONY_CODE_QUALITY
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES
// Doctrine
use Rector\Doctrine\Set\DoctrineSetList;
DoctrineSetList::DOCTRINE_ORM_29
DoctrineSetList::DOCTRINE_ORM_30
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES
// PHPUnit
use Rector\PHPUnit\Set\PHPUnitSetList;
PHPUnitSetList::PHPUNIT_100
PHPUnitSetList::PHPUNIT_CODE_QUALITY
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES
Skip Configuration
Skip Paths
->withSkip([
__DIR__ . '/src/Legacy/*',
__DIR__ . '/src/Infrastructure/Migrations/*',
__DIR__ . '/var/*',
__DIR__ . '/vendor/*',
])
Skip Rules
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
->withSkip([
// Skip globally
RemoveUnusedPrivateMethodRector::class,
// Skip for specific files
FinalizeClassesWithoutChildrenRector::class => [
__DIR__ . '/src/Domain/*/Entity/*',
],
])
CI Integration
Dry Run (Preview)
# Preview changes without applying
vendor/bin/rector process --dry-run
# Output diff format
vendor/bin/rector process --dry-run --output-format=json
GitHub Actions
rector:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer install
- name: Check Rector
run: vendor/bin/rector process --dry-run --ansi
# Optional: Auto-fix in PR
- name: Apply Rector
if: github.event_name == 'pull_request'
run: |
vendor/bin/rector process
git diff --quiet || (git add -A && git commit -m "Apply Rector fixes")
GitLab CI
rector:
script:
- vendor/bin/rector process --dry-run
allow_failure: true
rector:fix:
script:
- vendor/bin/rector process
- git diff --quiet || exit 1
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
Common Transformations
PHP 8.4 Features
// Before: Null coalescing
$value = isset($array['key']) ? $array['key'] : 'default';
// After: Null coalescing operator
$value = $array['key'] ?? 'default';
// Before: Constructor property promotion
class User {
private string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
// After: Property promotion
class User {
public function __construct(
private string $name,
) {}
}
Dead Code Removal
// Removes:
// - Unused private methods
// - Unused private properties
// - Unreachable code after return/throw
// - Empty methods that do nothing
// - Unused parameters (configurable)
Type Declarations
// Adds return types from:
// - Strict native calls (strlen, count, etc.)
// - Scalar return expressions
// - Property types from constructor
// Before
public function getName() {
return $this->name;
}
// After
public function getName(): string {
return $this->name;
}
Generation Instructions
-
Analyze project:
- Check current PHP version
- Check target PHP version
- Identify frameworks (Symfony, Laravel)
- Check for PHPUnit
-
Select sets:
- PHP version upgrade sets
- Framework migration sets
- Code quality sets
-
Configure skips:
- Legacy directories
- Generated code
- Migrations
-
Generate config:
- Create rector.php
- Add appropriate sets
- Configure skips
Usage
Provide:
- Current PHP version
- Target PHP version
- Frameworks used
- Directories to process
- Directories to skip
The generator will:
- Create rector.php configuration
- Include appropriate version sets
- Include framework sets
- Configure quality improvements
- Set up proper skips
More from dykyi-roman/awesome-claude-code
psr-overview-knowledge
PHP Standards Recommendations (PSR) overview knowledge base. Provides comprehensive reference for all accepted PSRs including PSR-1,3,4,6,7,11,12,13,14,15,16,17,18,20. Use for PSR selection decisions and compliance audits.
22detect-code-smells
Detects code smells in PHP codebases. Identifies God Class, Feature Envy, Data Clumps, Long Parameter List, Long Method, Primitive Obsession, Message Chains, Inappropriate Intimacy. Generates actionable reports with refactoring recommendations.
15clean-arch-knowledge
Clean Architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Clean Architecture and Hexagonal Architecture audits.
15ddd-knowledge
DDD architecture knowledge base. Provides patterns, antipatterns, and PHP-specific guidelines for Domain-Driven Design audits.
14testing-knowledge
Testing knowledge base for PHP 8.4 projects. Provides testing pyramid, AAA pattern, naming conventions, isolation principles, DDD testing guidelines, and PHPUnit patterns.
12bug-root-cause-finder
Root cause analysis methods for PHP bugs. Provides 5 Whys technique, fault tree analysis, git bisect guidance, and stack trace parsing.
12