Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Refactor Bundle Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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],
    ];
    
  2. 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).

  3. Where to Look First

    • Documentation: Check the GitHub README for CLI flags and configuration.
    • Commands: List available commands:
      php artisan list refactor
      
    • Config: Override default settings in config/refactor.php (if provided).

Implementation Patterns

Usage Patterns

  1. Incremental Refactoring Use the --step flag to apply changes in small, reviewable batches:

    php artisan refactor:refactor app/Http/Controllers/ExampleController.php --step
    
  2. Targeted Refactoring Focus on specific classes/methods with --class or --method:

    php artisan refactor:refactor --class=UserController --method=store
    
  3. 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>
    
  4. 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,
    ],
    

Workflows

  • Pair with IDE Tools: Use the bundle to identify issues, then apply fixes in your IDE (e.g., PHPStorm’s refactoring tools).
  • Pre-Pull Request Checks: Run refactor:analyze in a Git pre-commit hook to catch issues early.
  • Legacy Code Modernization: Process large files in chunks, committing changes incrementally.

Integration Tips

  • Laravel Mix/Webpack: Exclude generated JS/TS files from analysis by adding to .gitignore or config.
  • Database Migrations: Skip migration files in analysis:
    php artisan refactor:analyze --ignore="database/migrations/*"
    
  • Testing: Mock refactoring logic in unit tests by extending RefactorRuleInterface.

Gotchas and Tips

Pitfalls

  1. False Positives

    • The bundle may suggest refactorings that don’t align with your project’s style (e.g., naming conventions).
    • Fix: Adjust thresholds in config/refactor.php or suppress rules via annotations:
      // @refactor-ignore
      public function sensitiveMethod() { ... }
      
  2. Performance on Large Codebases

    • Analyzing app/ or vendor/ can be slow.
    • Fix: Limit scope with --path or --exclude flags.
  3. Breaking Changes

    • Refactoring methods/variables may break dependent code.
    • Fix: Run tests after each refactor batch and use --dry-run first:
      php artisan refactor:refactor --dry-run
      
  4. Configuration Overrides

    • Default rules may not suit your team’s PSR standards.
    • Fix: Override in config/refactor.php:
      'naming' => [
          'max_length' => 20, // Default may be 12
      ],
      

Debugging

  • Verbose Output: Enable debug mode for detailed logs:
    php artisan refactor:analyze --verbose
    
  • Rule-Specific Debugging: Isolate a rule by running:
    php artisan refactor:debug Rule\Name
    
  • Git Diffs: Compare changes before/after refactoring:
    git diff HEAD~1
    

Extension Points

  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
    );
    
  2. 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],
    ];
    
  3. 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);
    });
    

Tips

  • Backup First: Always commit or backup before running refactor:refactor.
  • Team Alignment: Discuss refactoring rules as a team to avoid conflicting changes.
  • Exclude Tests: Skip test files to avoid unnecessary noise:
    php artisan refactor:analyze --ignore="tests/*"
    
  • Leverage IDE: Use the bundle to identify issues, then apply fixes manually for finer control.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity