ergebnis/rector-rules
Custom Rector rules from Ergebnis to standardize and modernize PHP code. Includes sorting associative arrays and match arms, converting Faker generator property fetches to method calls, and fixing namespaced symbol references. Install via Composer for dev.
PHPUnit\ReplaceTestAttributeWithTestPrefixRector rule extends the package’s focus on modernizing PHP test suites, aligning with Laravel’s heavy reliance on PHPUnit/Pest for testing. This is particularly relevant for Laravel projects adopting PHPUnit 9+ or Pest 2+, where test attributes (e.g., @test) are being replaced with #[Test] syntax.laravel/pint, nunomaduro/collision) often uses PHPUnit; this rule automates migration to modern test syntax, reducing manual effort during PHPUnit upgrades.test_ or it_), improving readability in Laravel’s test suites (e.g., tests/Feature/).dev-dependency, with no impact on production. Add via:
composer require --dev ergebnis/rector-rules:^1.17.0
rector.php to include the new rule:
use Ergebnis\Rector\Rules;
return static function (RectorConfig $config): void {
$config->rule(Rules\PHPUnit\ReplaceTestAttributeWithTestPrefixRector::class);
};
rector:refactor command to include test directories:
php artisan rector:refactor --dir=tests
- name: Migrate Test Attributes
run: composer rector -- --dir=tests
@group, @dataProvider). Mitigation: Exclude non-standard annotations via rule configuration or .rectorignore.it(), test()). Mitigation: Disable the rule for Pest files or use Pest’s native migration tools.#[Test] support. Mitigation: Add a composer.json constraint:
"require-dev": {
"phpunit/phpunit": "^9.0"
}
tests/Feature/ with 1,000+ files) may slow CI. Mitigation: Run incrementally or cache results.tests/Unit/)?@test → #[Test]) retain identical behavior? (e.g., parallel test execution).@uses, @runInSeparateProcess)?@skip?#[Before])?dev-dependency model.#[Test]).php-cs-fixer for combined test formatting/migration pipelines.laravel/pint or nunomaduro/collision.tests/Unit/ExampleTest.php).--dry-run to preview changes:
vendor/bin/rector process tests/Unit --dry-run
.rectorignore).rector.php configuration:
$config->ruleWithConfiguration(
Rules\PHPUnit\ReplaceTestAttributeWithTestPrefixRector::class,
[
'attribute' => 'test', // Migrate `@test` to `#[Test]`
'prefix' => 'test_', // Optional: Enforce `test_*` method naming
]
);
composer.json scripts:
"scripts": {
"rector:tests": "rector process tests --level=all",
"rector:tests:check": "rector process tests --dry-run --level=all"
}
- name: Migrate Test Attributes
run: composer rector:tests:check
# .rectorignore
tests/Unit/Pest/
@group or @dataProvider:
$config->skip([
__DIR__ . '/tests/Feature/Groups/*',
]);
#[Test]).phpunit/phpunit:^9.0 is in composer.json.nunomaduro/collision or laravel/pint to avoid conflicts.--ignore to skip files with @skip or custom annotations.composer.json to require PHPUnit 9+.tests/Unit/).--testdox-html to verify coverage.#[Test] syntax.ergebnis/rector-rules for PHPUnit attribute syntax changes (e.g., #[Test] deprecation).rector.php in a shared template (e.g., Laravel monorepo or laravel/package).README.md.@test annotations post-migration (e.g., via php-cs-fixer).RECTOR_RULES.md file explaining test attribute migration.
How can I help you explore Laravel packages today?