driftingly/rector-laravel

ADD: Refactor Controllers to Single Responsibility principle

Closed this issue · 3 comments

it used to be a custom to have massive Controller files
most Laravel devs need to refactor their controllers to single respons

Feature Request

add a refactor that refactors all old style Controller classes according to single responsibility principle (Separation of Concerns #SOLID) + all of their usages

i.e:
SpaghettiController.php:
class SpaghettiController ..
{

	public function list() {...}

	public function show()  {...}

    public function delete()  {...}
}

into separate files in the same directory:

SpaghettiListController.php:

class SpaghettiListController .. {
    public function __invoke () {...}
}

SpaghettiShowController.php:

class SpaghettiShowController .. {
    public function __invoke () {...}
}

SpaghettiDeleteController.php:

class SpaghettiDeleteController .. {
    public function __invoke () {...}
}

upstream: rectorphp/rector#7847

a more Laravel specific feature would be to rewrite api routes to use these new controller invoke methods:

Route::get('spaghetti/{spaghetti_id}', (new SpaghettiController())->show(...));
or
Route::get('spaghetti/{spaghetti_id}', [SpaghettiController::class, 'show']);

into

Route::get('spaghetti/{spaghetti_id}', SpaghettiShowController::class);

This would be amazing. Unfortunately, I don't think it's possible here.
This would have to be part of a more comprehensive tool capable of creating new files and moving code between them.

@driftingly is this a base rector issue? I wanna write this refactor, since GH Copilot can't do it atm

I believe this falls outside the scope of Rector PHP.