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) |
||
171 | |||
172 | /** |
||
173 | * Update a named many-to-one relationship association on a specified record. |
||
174 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
175 | * |
||
176 | * @param Request $request |
||
177 | * @param Model|int $record |
||
178 | * @param string $relation |
||
179 | * @param string|null $foreignKey |
||
180 | * |
||
181 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
182 | * |
||
183 | * @return JsonApiResponse |
||
184 | */ |
||
185 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
196 | |||
197 | /** |
||
198 | * Update named many-to-many relationship entries on a specified record. |
||
199 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
200 | * |
||
201 | * @param Request $request |
||
202 | * @param Model|int $record |
||
203 | * @param string $relation |
||
204 | * |
||
205 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
206 | * |
||
207 | * @return JsonApiResponse |
||
208 | */ |
||
209 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
238 | |||
239 | /** |
||
240 | * Return an instance of the resource by primary key. |
||
241 | * |
||
242 | * @param int $key |
||
243 | * |
||
244 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
245 | * |
||
246 | * @return Model |
||
247 | */ |
||
248 | protected function findModelInstance($key) |
||
252 | |||
253 | /** |
||
254 | * Return any JSON API resource parameters from a request. |
||
255 | * |
||
256 | * @param Request $request |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function getRequestParameters($request) |
||
269 | |||
270 | /** |
||
271 | * Return any comma separated values in a request query field as an array. |
||
272 | * |
||
273 | * @param Request $request |
||
274 | * @param string $key |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function getRequestQuerySet($request, $key) |
||
282 | |||
283 | /** |
||
284 | * Sort a resource query by one or more attributes. |
||
285 | * |
||
286 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
287 | * @param array $attributes |
||
288 | * |
||
289 | * @return \Illuminate\Database\Eloquent\Builder |
||
290 | */ |
||
291 | protected function sortQuery($query, $attributes) |
||
301 | |||
302 | /** |
||
303 | * Filter a resource query by one or more attributes. |
||
304 | * |
||
305 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
306 | * @param array $attributes |
||
307 | * |
||
308 | * @return \Illuminate\Database\Eloquent\Builder |
||
309 | */ |
||
310 | protected function filterQuery($query, $attributes) |
||
327 | |||
328 | /** |
||
329 | * Update one or more relationships on a model instance. |
||
330 | * |
||
331 | * @param Model $record |
||
332 | * @param array $relationships |
||
333 | */ |
||
334 | protected function updateRecordRelationships($record, array $relationships) |
||
349 | } |
||
350 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.