Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractRestfulController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractRestfulController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractRestfulController extends BaseController |
||
| 36 | { |
||
| 37 | use AuthorizesRequests; |
||
| 38 | use ValidatesRequests; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The views to render. |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $views = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Return a list of matching models. |
||
| 48 | * @param Request $request |
||
| 49 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
| 50 | */ |
||
| 51 | public function index(Request $request) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Generate a new query builder for the model. |
||
| 71 | * @return Builder |
||
| 72 | */ |
||
| 73 | protected function createModelQueryBuilder(): Builder |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Creates a new model instance. |
||
| 80 | * @return Model |
||
| 81 | */ |
||
| 82 | protected function newModelInstance(): Model |
||
| 88 | |||
| 89 | /** |
||
| 90 | * What Model class to search for entities. |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | abstract protected function getModelClass(): string; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Apply causes to the builder. |
||
| 97 | * @param Builder $builder |
||
| 98 | * @param string $column |
||
| 99 | * @param mixed $value |
||
| 100 | */ |
||
| 101 | protected function filterValue(Builder $builder, string $column, $value): void |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Build and return a response. |
||
| 108 | * @param Request $request |
||
| 109 | * @param mixed $data |
||
| 110 | * @param string $method |
||
| 111 | * @return JsonResponse|ResponseFactory|Response|RedirectResponse|Redirector |
||
| 112 | */ |
||
| 113 | protected function return(Request $request, $data, string $method) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Redirects to the show route for the model if one exists. |
||
| 153 | * @param Request $request |
||
| 154 | * @param mixed $data |
||
| 155 | * @return RedirectResponse|Redirector|null |
||
| 156 | */ |
||
| 157 | protected function redirectToShowRoute(Request $request, $data) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Handles creating a model. The C of CRUD |
||
| 206 | * @param Request $request |
||
| 207 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function store(Request $request) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Save a model, either from a store or an update. |
||
| 224 | * @param Model $model |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | protected function saveModel(Model $model) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Finds the model instance. |
||
| 234 | * @param int|string $id |
||
| 235 | * @param Request|null $request |
||
| 236 | * @return Model |
||
| 237 | */ |
||
| 238 | protected function findModel($id, Request $request = null): Model |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Preload any relationships required. |
||
| 253 | * @param Builder $builder |
||
| 254 | * @param Request $request |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | protected function preloadRelationships(Builder &$builder, Request $request): void |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Shows a model. The R of CRUD. |
||
| 276 | * @param Request $request |
||
| 277 | * @param int $id |
||
| 278 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
| 279 | */ |
||
| 280 | public function show(Request $request, $id) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Looks for an "extra" param in the route, and if it exists, looks for relationships |
||
| 291 | * based on that route. |
||
| 292 | * @param Model $model |
||
| 293 | * @param Request $request |
||
| 294 | * @return LengthAwarePaginator|Collection|Model|mixed |
||
| 295 | * @throws BadMethodCallException |
||
| 296 | */ |
||
| 297 | protected function iterateThroughChildren(Model $model, Request $request) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Update a record. The U of CRUD. |
||
| 341 | * @param Request $request |
||
| 342 | * @param int $id |
||
| 343 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
| 344 | */ |
||
| 345 | View Code Duplication | public function update(Request $request, $id) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Destroy a model. The D of CRUD. |
||
| 360 | * @param Request $request |
||
| 361 | * @param int $id |
||
| 362 | * @return ResponseFactory|Response |
||
| 363 | * @throws Exception |
||
| 364 | */ |
||
| 365 | public function destroy(Request $request, $id) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Return the _links. The O of CRUD..... |
||
| 376 | * @param Request $request |
||
| 377 | * @return ResponseFactory|JsonResponse|RedirectResponse|Response|Redirector |
||
| 378 | */ |
||
| 379 | public function options(Request $request) |
||
| 393 | } |
||
| 394 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: