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 | * What Model class to search for entities. |
||
48 | * @return string |
||
49 | */ |
||
50 | abstract protected function getModelClass(): string; |
||
51 | |||
52 | /** |
||
53 | * Return a list of matching models. |
||
54 | * @param Request $request |
||
55 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
56 | */ |
||
57 | public function index(Request $request) |
||
74 | |||
75 | /** |
||
76 | * Handles creating a model. The C of CRUD |
||
77 | * @param Request $request |
||
78 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
79 | */ |
||
80 | View Code Duplication | public function store(Request $request) |
|
92 | |||
93 | /** |
||
94 | * Shows a model. The R of CRUD. |
||
95 | * @param Request $request |
||
96 | * @param int $id |
||
97 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
98 | */ |
||
99 | public function show(Request $request, $id) |
||
107 | |||
108 | /** |
||
109 | * Update a record. The U of CRUD. |
||
110 | * @param Request $request |
||
111 | * @param int $id |
||
112 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
113 | */ |
||
114 | View Code Duplication | public function update(Request $request, $id) |
|
126 | |||
127 | /** |
||
128 | * Destroy a model. The D of CRUD. |
||
129 | * @param Request $request |
||
130 | * @param int $id |
||
131 | * @return ResponseFactory|Response |
||
132 | * @throws Exception |
||
133 | */ |
||
134 | public function destroy(Request $request, $id) |
||
142 | |||
143 | |||
144 | /** |
||
145 | * Return the _links. The O of CRUD..... |
||
146 | * @param Request $request |
||
147 | * @return ResponseFactory|JsonResponse|RedirectResponse|Response|Redirector |
||
148 | */ |
||
149 | public function options(Request $request) |
||
163 | |||
164 | /** |
||
165 | * Generate a new query builder for the model. |
||
166 | * @return Builder |
||
167 | */ |
||
168 | protected function createModelQueryBuilder(): Builder |
||
174 | |||
175 | /** |
||
176 | * Creates a new model instance. |
||
177 | * @return Model |
||
178 | */ |
||
179 | protected function newModelInstance(): Model |
||
185 | |||
186 | /** |
||
187 | * Looks for an "extra" param in the route, and if it exists, looks for relationships |
||
188 | * based on that route. |
||
189 | * @param Model $model |
||
190 | * @param Request $request |
||
191 | * @return LengthAwarePaginator|Collection|Model|mixed |
||
192 | * @throws BadMethodCallException |
||
193 | */ |
||
194 | protected function iterateThroughChildren(Model $model, Request $request) |
||
235 | |||
236 | /** |
||
237 | * Finds the model instance. |
||
238 | * @param int|string $id |
||
239 | * @param Request|null $request |
||
240 | * @return Model |
||
241 | */ |
||
242 | protected function findModel($id, Request $request = null): Model |
||
254 | |||
255 | /** |
||
256 | * Apply causes to the builder. |
||
257 | * @param Builder $builder |
||
258 | * @param string $column |
||
259 | * @param mixed $value |
||
260 | */ |
||
261 | protected function filterValue(Builder $builder, string $column, $value): void |
||
265 | |||
266 | /** |
||
267 | * Build and return a response. |
||
268 | * @param Request $request |
||
269 | * @param mixed $data |
||
270 | * @param string $method |
||
271 | * @return JsonResponse|ResponseFactory|Response|RedirectResponse|Redirector |
||
272 | */ |
||
273 | protected function return(Request $request, $data, string $method) |
||
310 | |||
311 | /** |
||
312 | * Redirects to the show route for the model if one exists. |
||
313 | * @param Request $request |
||
314 | * @param mixed $data |
||
315 | * @return RedirectResponse|Redirector|null |
||
316 | */ |
||
317 | protected function redirectToShowRoute(Request $request, $data) |
||
363 | |||
364 | /** |
||
365 | * Preload any relationships required. |
||
366 | * @param Model $class |
||
367 | * @param Request $request |
||
368 | * @return void |
||
369 | */ |
||
370 | protected function preloadRelationships(Model &$class, Request $request): void |
||
386 | |||
387 | /** |
||
388 | * Save a model, either from a store or an update. |
||
389 | * @param Model $model |
||
390 | * @return bool |
||
391 | */ |
||
392 | private function saveModel(Model $model) |
||
396 | } |
||
397 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: