abenbachir/refactor-bundle
Laravel bundle that groups refactoring utilities and helpers to aid safe code changes. Provides commands and tooling to modernize and reorganize codebases, streamline maintenance, and support incremental refactors across projects.
Installation Add the package via Composer:
composer require abenbachir/refactor-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Abenbachir\RefactorBundle\RefactorBundle::class => ['all' => true],
];
First Use Case
Locate the refactoring command in artisan:
php artisan refactor:analyze
Run a basic analysis on a controller (e.g., app/Http/Controllers/ExampleController.php):
php artisan refactor:analyze app/Http/Controllers/ExampleController.php
Review output for suggested refactorings (e.g., extract method, rename variable).
Where to Look First
php artisan list refactor
config/refactor.php (if provided).Incremental Refactoring
Use the --step flag to apply changes in small, reviewable batches:
php artisan refactor:refactor app/Http/Controllers/ExampleController.php --step
Targeted Refactoring
Focus on specific classes/methods with --class or --method:
php artisan refactor:refactor --class=UserController --method=store
Integration with CI/CD
Add to phpunit.xml or Git hooks to enforce refactoring standards:
<php>
<file>vendor/autoload.php</file>
<file>vendor/abenbachir/refactor-bundle/src/RefactorBundle.php</file>
</php>
Custom Rules
Extend the bundle by creating a custom refactoring rule (see src/RefactorRuleInterface):
namespace App\RefactorRules;
use Abenbachir\RefactorBundle\RefactorRuleInterface;
class CustomRule implements RefactorRuleInterface {
public function check($code) { /* Logic */ }
public function fix($code) { /* Refactor */ }
}
Register in config/refactor.php:
'rules' => [
App\RefactorRules\CustomRule::class,
],
refactor:analyze in a Git pre-commit hook to catch issues early..gitignore or config.php artisan refactor:analyze --ignore="database/migrations/*"
RefactorRuleInterface.False Positives
config/refactor.php or suppress rules via annotations:
// @refactor-ignore
public function sensitiveMethod() { ... }
Performance on Large Codebases
app/ or vendor/ can be slow.--path or --exclude flags.Breaking Changes
--dry-run first:
php artisan refactor:refactor --dry-run
Configuration Overrides
config/refactor.php:
'naming' => [
'max_length' => 20, // Default may be 12
],
php artisan refactor:analyze --verbose
php artisan refactor:debug Rule\Name
git diff HEAD~1
Custom Refactoring Rules
Implement RefactorRuleInterface and register in the container:
// In a ServiceProvider
$this->app->bind(
\Abenbachir\RefactorBundle\RefactorRuleInterface::class,
App\RefactorRules\CustomRule::class
);
Event Listeners
Listen to refactoring events (e.g., RefactoringStarted, RefactoringCompleted):
namespace App\Listeners;
use Abenbachir\RefactorBundle\Events\RefactoringCompleted;
class LogRefactoring {
public function handle(RefactoringCompleted $event) {
Log::info('Refactored ' . $event->getFile());
}
}
Register in EventServiceProvider:
protected $listen = [
RefactoringCompleted::class => [LogRefactoring::class],
];
API Integration Expose refactoring logic via a custom API endpoint:
Route::post('/refactor', function (Request $request) {
$refactorService = app(\Abenbachir\RefactorBundle\RefactorService::class);
return $refactorService->refactor($request->file);
});
refactor:refactor.php artisan refactor:analyze --ignore="tests/*"
How can I help you explore Laravel packages today?