andersundsehr/rector-p
Run Rector in large legacy projects file-by-file. rector-p prompts you per changed file to apply or skip changes, tracks unchanged files to speed repeats, supports running on specific paths/files, and can process only a chunk (e.g., 1/2) at a time.
Installation:
composer require andersundsehr/rector-p
Ensure your rector.php config exists (Laravel’s default Rector setup works here).
First Run:
rector-p
Apply changes to [file.php]? [y/N]
y to apply or n to skip.First Use Case: Refactor a single legacy file (e.g., a controller with deprecated Laravel helpers) without risking a full project-wide refactor.
rector-p src/Http/Controllers/UserController.php
rector.php (no additional setup needed).rector-p --help to explore options like --chunk, --startOver, or --dry-run.~/.rector-p/cache (tracks processed files; delete to reset with --startOver).rector-p --chunk=1/10 src/
Process 10% of files daily, resuming where you left off..git/hooks/pre-commit to auto-refactor staged files:
#!/bin/bash
git diff --cached --name-only | xargs rector-p --no-interaction
app/Services/):
rector-p app/Services/
excludePaths in rector.php:
return RectorConfig::create()->withPaths([__DIR__.'/src'])->withExcludePaths([__DIR__.'/tests']);
rector-p --chunk=1/2 src/rector-p --chunk=2/2 src/- name: Run Rector-P (Chunk 1/3)
run: rector-p --chunk=1/3 --no-interaction
rector-p --dry-run src/Http/Controllers/
rector-p src/ && php artisan test --filter=UserControllerTest
// rector.php
use Rector\Laravel\Set\LaravelLevelSetList;
return RectorConfig::create()
->withPaths([__DIR__.'/app'])
->withRules([
LaravelLevelSetList::LEVEL_90, // Laravel 9+ rules
\Rector\Php80\Rector\Class_\AnnotatedPropertyFetchToMethodCallRector::class,
]);
->withPaths([__DIR__.'/resources/views'])
->withExcludePaths([__DIR__.'/resources/views/*.blade.php'])
rector-p --dry-run to preview changes.rector-p with pest --minimal to ensure refactored code doesn’t introduce regressions.--startOver to reset the cache when switching teams or environments.Rule Conflicts:
--verbose to debug:
rector-p --verbose src/Controller/
rector.php and add rules incrementally.Cache Corruption:
rector-p skips files unexpectedly or crashes.rm -rf ~/.rector-p/cache && rector-p --startOver
Interactive Mode in CI:
--no-interaction still prompts for input.CI=true).Path Resolution Issues:
rector-p can’t find files (e.g., No such file or directory).getcwd()-relative paths:
rector-p $(pwd)/src/
Performance with Large Files:
AppServiceProvider).--verbose or --vvv for debug-level logs:
rector-p --vvv src/
--dry-run before applying:
rector-p --dry-run --verbose src/ | grep "Would change"
vendor/bin/rector process src/File.php --dry-run
Chunk Mode:
2/2 = second half).# PR #1: First 30%
rector-p --chunk=1/3 src/
# PR #2: Next 30%
rector-p --chunk=2/3 src/
Quiet Mode:
--quiet suppresses all output except errors. Useful for CI:
rector-p --quiet --no-interaction --chunk=1/5 src/
Symfony Console Integration:
Command class. Customize behavior by overriding its execute() method in a subclass (advanced use case).Custom Prompt Logic:
Override the interactive prompt by extending the package’s PartialCommand:
// app/Console/Commands/CustomRectorCommand.php
namespace App\Console\Commands;
use AndersUndSehr\RectorP\Command\PartialCommand;
class CustomRectorCommand extends PartialCommand {
protected function askToApply(string $file): bool {
// Custom logic (e.g., auto-approve files in `app/Old/`)
return str_contains($file, 'Old/') || parent::askToApply($file);
}
}
Pre/Post-Refactor Hooks:
Use Rector’s RectorConfig to add hooks:
->withAutoloadFiles([__DIR__.'/rector-plugins.php'])
Then define hooks in rector-plugins.php:
RectorConfig::create()->withHooks([
new \Rector\Hook\BeforeRefactorHook(function (FileRefactor $fileRefactor) {
// Pre-refactor logic (e.g., log file contents)
}),
]);
Custom Cache Location:
Override the default cache path by setting the RECTOR_P_CACHE_DIR environment variable:
export RECTOR_P_CACHE_DIR=/custom/path/.rector-p-cache
rector-p
php-cs-fixer:
Run rector-p followed by php-cs-fixer to auto-format refactored files:
rector-p src/ && php-cs-fixer fix src/
.gitconfig to refactor staged files:
[alias]
rector = "!f() { git diff --cached --name-only | xargs vendor/bin/rector-p --
How can I help you explore Laravel packages today?