Complex classes like JsonApiController 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 JsonApiController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class JsonApiController extends Controller |
||
17 | { |
||
18 | use JsonApiErrors, JsonApiTransforms; |
||
19 | |||
20 | /** |
||
21 | * Return the Eloquent Model for the resource. |
||
22 | * |
||
23 | * @return Model |
||
24 | */ |
||
25 | abstract protected function getModel(); |
||
26 | |||
27 | /** |
||
28 | * Return the type name of the resource. |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | protected function getModelType() |
||
36 | |||
37 | /** |
||
38 | * The model relationships that can be updated. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | protected function getModelRelationships() |
||
46 | |||
47 | /** |
||
48 | * Return a listing of the resource. |
||
49 | * |
||
50 | * @param Request $request |
||
51 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
52 | * |
||
53 | * @return JsonApiResponse |
||
54 | */ |
||
55 | public function indexAction(Request $request, $query = null) |
||
71 | |||
72 | /** |
||
73 | * Store a new record. |
||
74 | * |
||
75 | * @param Request $request |
||
76 | * |
||
77 | * @return JsonApiResponse |
||
78 | */ |
||
79 | public function storeAction(Request $request) |
||
89 | |||
90 | /** |
||
91 | * Return a specified record. |
||
92 | * |
||
93 | * @param Request $request |
||
94 | * @param Model|int $record |
||
95 | * |
||
96 | * @return JsonApiResponse |
||
97 | */ |
||
98 | public function showAction(Request $request, $record) |
||
105 | |||
106 | /** |
||
107 | * Update a specified record. |
||
108 | * |
||
109 | * @param Request $request |
||
110 | * @param Model|int $record |
||
111 | * |
||
112 | * @return JsonApiResponse |
||
113 | */ |
||
114 | public function updateAction(Request $request, $record) |
||
125 | |||
126 | /** |
||
127 | * Destroy a specified record. |
||
128 | * |
||
129 | * @param Request $request |
||
130 | * @param Model|int $record |
||
131 | * |
||
132 | * @return JsonApiResponse |
||
133 | */ |
||
134 | public function destroyAction(Request $request, $record) |
||
141 | |||
142 | /** |
||
143 | * Return a specified record relationship. |
||
144 | * |
||
145 | * @param Request $request |
||
146 | * @param Model|int $record |
||
147 | * @param string $relation |
||
148 | * |
||
149 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
150 | * |
||
151 | * @return JsonApiResponse |
||
152 | */ |
||
153 | public function relationshipAction(Request $request, $record, $relation) |
||
175 | |||
176 | /** |
||
177 | * Update a named many-to-one relationship association on a specified record. |
||
178 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
179 | * |
||
180 | * @param Request $request |
||
181 | * @param Model|int $record |
||
182 | * @param string $relation |
||
183 | * @param string|null $foreignKey |
||
184 | * |
||
185 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
186 | * |
||
187 | * @return JsonApiResponse |
||
188 | */ |
||
189 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
200 | |||
201 | /** |
||
202 | * Update named many-to-many relationship entries on a specified record. |
||
203 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
204 | * |
||
205 | * @param Request $request |
||
206 | * @param Model|int $record |
||
207 | * @param string $relation |
||
208 | * |
||
209 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
210 | * |
||
211 | * @return JsonApiResponse |
||
212 | */ |
||
213 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
242 | |||
243 | /** |
||
244 | * Return an instance of the resource by primary key. |
||
245 | * |
||
246 | * @param int $key |
||
247 | * |
||
248 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
249 | * |
||
250 | * @return Model |
||
251 | */ |
||
252 | protected function findModelInstance($key) |
||
256 | |||
257 | /** |
||
258 | * Return any JSON API resource parameters from a request. |
||
259 | * |
||
260 | * @param Request $request |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | protected function getRequestParameters($request) |
||
273 | |||
274 | /** |
||
275 | * Return any comma separated values in a request query field as an array. |
||
276 | * |
||
277 | * @param Request $request |
||
278 | * @param string $key |
||
279 | * |
||
280 | * @return array |
||
281 | */ |
||
282 | protected function getRequestQuerySet($request, $key) |
||
286 | |||
287 | /** |
||
288 | * Sort a resource query by one or more attributes. |
||
289 | * |
||
290 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
291 | * @param array $attributes |
||
292 | * |
||
293 | * @return \Illuminate\Database\Eloquent\Builder |
||
294 | */ |
||
295 | protected function sortQuery($query, $attributes) |
||
305 | |||
306 | /** |
||
307 | * Filter a resource query by one or more attributes. |
||
308 | * |
||
309 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
310 | * @param array $attributes |
||
311 | * |
||
312 | * @return \Illuminate\Database\Eloquent\Builder |
||
313 | */ |
||
314 | protected function filterQuery($query, $attributes) |
||
331 | |||
332 | /** |
||
333 | * Update one or more relationships on a model instance. |
||
334 | * |
||
335 | * @param Model $record |
||
336 | * @param array $relationships |
||
337 | */ |
||
338 | protected function updateRecordRelationships($record, array $relationships) |
||
353 | } |
||
354 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.