ergebnis/rector-rules
A curated set of custom Rector rules from ergebnis to automate PHP refactoring and style consistency. Includes rules for sorting arrays and match arms, simplifying call arguments, Faker updates, namespace symbol references, and PHPUnit attribute-to-prefix changes.
Installation:
composer require --dev ergebnis/rector-rules
Add the package to your rector.php config under imports:
use Ergebnis\Rector\Rules;
First Use Case:
Run Rector with a single rule (e.g., RemoveNamedArgumentForSingleParameterRector) on a specific file or directory:
vendor/bin/rector process src --dry-run --rules="[\Ergebnis\Rector\Rules\Expressions\CallLikes\RemoveNamedArgumentForSingleParameterRector]"
Key Files to Review:
rector.php (main config file)doc/rules/Expressions/CallLikes/RemoveNamedArgumentForSingleParameterRector.md)Incremental Adoption:
SortAssociativeArrayByKeyRector or SortMatchArmsByConditionalRector).--dry-run to preview changes before applying them:
vendor/bin/rector process src --dry-run
Rule Grouping:
rector.php config:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Expressions\Arrays\SortAssociativeArrayByKeyRector::class,
\Ergebnis\Rector\Rules\Expressions\Matches\SortMatchArmsByConditionalRector::class,
]);
PHPUnit Migration:
#[Test] attributes with test* prefixes in bulk:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\PHPUnit\ReplaceTestAttributeWithTestPrefixRector::class,
]);
Namespace Optimization:
use statements for large codebases:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Files\ReferenceNamespacedSymbolsRelativeToNamespacePrefixRector::class,
])
->withConfiguration([
'namespacePrefixes' => ['App\Domain', 'App\Infrastructure'],
]);
Faker Updates:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Faker\GeneratorPropertyFetchToMethodCallRector::class,
]);
CI/CD Pipeline: Add Rector as a pre-commit or pre-merge step to enforce consistency:
# .github/workflows/rector.yml
jobs:
rector:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: composer require --dev ergebnis/rector-rules
- run: vendor/bin/rector process src --dry-run
Custom Rule Sets:
Extend the package by creating custom rule sets in rector.php:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Expressions\CallLikes\RemoveNamedArgumentForSingleParameterRector::class,
// Add custom rules here
]);
Parallel Processing: Use Rector’s parallel processing for large codebases:
vendor/bin/rector process src --parallel
Namespace Conflicts:
ReferenceNamespacedSymbolsRelativeToNamespacePrefixRector may cause conflicts if multiple prefixes overlap. Test with --dry-run first.namespacePrefixes to avoid ambiguity.Breaking Changes:
ReplaceTestAttributeWithTestPrefixRector or GeneratorPropertyFetchToMethodCallRector modify method signatures or API usage. Run tests thoroughly after applying them.Performance Overhead:
SortMatchArmsByConditionalRector or SortAssociativeArrayByKeyRector may slow down processing for large files. Use --parallel to mitigate this.False Positives:
RemoveNamedArgumentForSingleParameterRector might incorrectly modify named arguments in multi-parameter calls if not configured properly.PHP Version Mismatches:
rector.php specifies compatible PHP versions:
return static::configure()
->withPhpVersion(810); // PHP 8.1
Dry Runs:
Always use --dry-run to preview changes:
vendor/bin/rector process src --dry-run --rules="[\Ergebnis\Rector\Rules\Files\ReferenceNamespacedSymbolsRelativeToNamespacePrefixRector]"
Verbose Output: Enable verbose logging to debug rule application:
vendor/bin/rector process src --verbose
Excluding Files/Directories: Exclude problematic files or directories from processing:
return static::configure()
->withPaths([
__DIR__.'/src',
])
->withExcludedPaths([
__DIR__.'/tests',
__DIR__.'/vendor',
]);
Rule-Specific Config:
Misconfigured rules (e.g., discoverNamespacePrefixes: true with incorrect parentNamespacePrefixes) may lead to unexpected behavior. Validate configurations in the rule’s docs.
Leverage discoverNamespacePrefixes:
Automatically discover namespace prefixes for large projects:
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Files\ReferenceNamespacedSymbolsRelativeToNamespacePrefixRector::class,
])
->withConfiguration([
'discoverNamespacePrefixes' => true,
'parentNamespacePrefixes' => ['App', 'Domain'],
]);
Combine Rules: Chain rules for comprehensive refactoring (e.g., sort arrays + remove named arguments):
return static::configure()
->withRules([
\Ergebnis\Rector\Rules\Expressions\Arrays\SortAssociativeArrayByKeyRector::class,
\Ergebnis\Rector\Rules\Expressions\CallLikes\RemoveNamedArgumentForSingleParameterRector::class,
]);
Custom Rule Sets:
Create reusable rule sets in rector.php for different contexts (e.g., test, production):
return static::configure()
->withRuleSets([
'test' => [
\Ergebnis\Rector\Rules\PHPUnit\ReplaceTestAttributeWithTestPrefixRector::class,
],
'production' => [
\Ergebnis\Rector\Rules\Expressions\CallLikes\RemoveNamedArgumentForSingleParameterRector::class,
],
]);
Git Integration: Use Rector in combination with Git to track refactoring changes:
git add -p # Review changes interactively
vendor/bin/rector process src --commit
Document Rule Usage: Add comments in your codebase to explain Rector-driven changes:
// Rector: RemoveNamedArgumentForSingleParameterRector
strlen('hello');
How can I help you explore Laravel packages today?