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 | * @param callable|null $callback |
||
56 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
57 | */ |
||
58 | public function index(Request $request, ?callable $callback = null) |
||
81 | |||
82 | /** |
||
83 | * Handles creating a model. The C of CRUD |
||
84 | * @param Request $request |
||
85 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
86 | */ |
||
87 | public function store(Request $request) |
||
99 | |||
100 | /** |
||
101 | * Shows a model. The R of CRUD. |
||
102 | * @param Request $request |
||
103 | * @param int $id |
||
104 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
105 | */ |
||
106 | public function show(Request $request, $id) |
||
114 | |||
115 | /** |
||
116 | * Update a record. The U of CRUD. |
||
117 | * @param Request $request |
||
118 | * @param int $id |
||
119 | * @return JsonResponse|RedirectResponse|ResponseFactory|Response|Redirector |
||
120 | */ |
||
121 | public function update(Request $request, $id) |
||
133 | |||
134 | /** |
||
135 | * Destroy a model. The D of CRUD. |
||
136 | * @param Request $request |
||
137 | * @param int $id |
||
138 | * @return ResponseFactory|Response |
||
139 | * @throws Exception |
||
140 | */ |
||
141 | public function destroy(Request $request, $id) |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Return the _links. The O of CRUD..... |
||
153 | * @param Request $request |
||
154 | * @return ResponseFactory|JsonResponse|RedirectResponse|Response|Redirector |
||
155 | */ |
||
156 | public function options(Request $request) |
||
170 | |||
171 | /** |
||
172 | * Generate a new query builder for the model. |
||
173 | * @return Builder |
||
174 | */ |
||
175 | protected function createModelQueryBuilder(): Builder |
||
181 | |||
182 | /** |
||
183 | * Creates a new model instance. |
||
184 | * @return Model |
||
185 | */ |
||
186 | protected function newModelInstance(): Model |
||
192 | |||
193 | /** |
||
194 | * Looks for an "extra" param in the route, and if it exists, looks for relationships |
||
195 | * based on that route. |
||
196 | * @param Model $model |
||
197 | * @param Request $request |
||
198 | * @return LengthAwarePaginator|Collection|Model|mixed |
||
199 | * @throws BadMethodCallException |
||
200 | */ |
||
201 | protected function iterateThroughChildren(Model $model, Request $request) |
||
242 | |||
243 | /** |
||
244 | * Finds the model instance. |
||
245 | * @param int|string $id |
||
246 | * @param Request|null $request |
||
247 | * @return Model |
||
248 | */ |
||
249 | protected function findModel($id, Request $request = null): Model |
||
261 | |||
262 | /** |
||
263 | * Apply causes to the builder. |
||
264 | * @param Builder $builder |
||
265 | * @param string $column |
||
266 | * @param mixed $value |
||
267 | */ |
||
268 | protected function filterValue(Builder $builder, string $column, $value): void |
||
272 | |||
273 | /** |
||
274 | * Build and return a response. |
||
275 | * @param Request $request |
||
276 | * @param mixed $data |
||
277 | * @param string $method |
||
278 | * @return JsonResponse|ResponseFactory|Response|RedirectResponse|Redirector |
||
279 | */ |
||
280 | protected function return(Request $request, $data, string $method) |
||
317 | |||
318 | /** |
||
319 | * Redirects to the show route for the model if one exists. |
||
320 | * @param Request $request |
||
321 | * @param mixed $data |
||
322 | * @return RedirectResponse|Redirector|null |
||
323 | */ |
||
324 | protected function redirectToShowRoute(Request $request, $data) |
||
356 | |||
357 | /** |
||
358 | * Preload any relationships required. |
||
359 | * @param Model $class |
||
360 | * @param Request $request |
||
361 | * @return void |
||
362 | */ |
||
363 | protected function preloadRelationships(Model &$class, Request $request): void |
||
379 | |||
380 | /** |
||
381 | * Save a model, either from a store or an update. |
||
382 | * @param Model $model |
||
383 | * @return bool |
||
384 | */ |
||
385 | private function saveModel(Model $model) |
||
389 | } |
||
390 |
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: