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 | * |
||
52 | * @return JsonApiResponse |
||
53 | */ |
||
54 | public function indexAction(Request $request) |
||
70 | |||
71 | /** |
||
72 | * Store a new record. |
||
73 | * |
||
74 | * @param Request $request |
||
75 | * |
||
76 | * @return JsonApiResponse |
||
77 | */ |
||
78 | public function storeAction(Request $request) |
||
88 | |||
89 | /** |
||
90 | * Return a specified record. |
||
91 | * |
||
92 | * @param Request $request |
||
93 | * @param Model|int $record |
||
94 | * |
||
95 | * @return JsonApiResponse |
||
96 | */ |
||
97 | public function showAction(Request $request, $record) |
||
104 | |||
105 | /** |
||
106 | * Update a specified record. |
||
107 | * |
||
108 | * @param Request $request |
||
109 | * @param Model|int $record |
||
110 | * |
||
111 | * @return JsonApiResponse |
||
112 | */ |
||
113 | public function updateAction(Request $request, $record) |
||
124 | |||
125 | /** |
||
126 | * Destroy a specified record. |
||
127 | * |
||
128 | * @param Request $request |
||
129 | * @param Model|int $record |
||
130 | * |
||
131 | * @return JsonApiResponse |
||
132 | */ |
||
133 | public function destroyAction(Request $request, $record) |
||
140 | |||
141 | /** |
||
142 | * Return a specified record relationship. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * @param Model|int $record |
||
146 | * @param string $relation |
||
147 | * |
||
148 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
149 | * |
||
150 | * @return JsonApiResponse |
||
151 | */ |
||
152 | public function relationshipAction(Request $request, $record, $relation) |
||
170 | |||
171 | /** |
||
172 | * Update a named many-to-one relationship association on a specified record. |
||
173 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
174 | * |
||
175 | * @param Request $request |
||
176 | * @param Model|int $record |
||
177 | * @param string $relation |
||
178 | * @param string|null $foreignKey |
||
179 | * |
||
180 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
181 | * |
||
182 | * @return JsonApiResponse |
||
183 | */ |
||
184 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
195 | |||
196 | /** |
||
197 | * Update named many-to-many relationship entries on a specified record. |
||
198 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
199 | * |
||
200 | * @param Request $request |
||
201 | * @param Model|int $record |
||
202 | * @param string $relation |
||
203 | * |
||
204 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
205 | * |
||
206 | * @return JsonApiResponse |
||
207 | */ |
||
208 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
233 | |||
234 | /** |
||
235 | * Return an instance of the resource by primary key. |
||
236 | * |
||
237 | * @param int $key |
||
238 | * |
||
239 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
240 | * |
||
241 | * @return Model |
||
242 | */ |
||
243 | protected function findModelInstance($key) |
||
247 | |||
248 | /** |
||
249 | * Return any JSON API resource parameters from a request. |
||
250 | * |
||
251 | * @param Request $request |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getRequestParameters($request) |
||
264 | |||
265 | /** |
||
266 | * Return any comma separated values in a request query field as an array. |
||
267 | * |
||
268 | * @param Request $request |
||
269 | * @param string $key |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | protected function getRequestQuerySet($request, $key) |
||
277 | |||
278 | /** |
||
279 | * Sort a resource query by one or more attributes. |
||
280 | * |
||
281 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
282 | * @param array $attributes |
||
283 | * |
||
284 | * @return \Illuminate\Database\Eloquent\Builder |
||
285 | */ |
||
286 | protected function sortQuery($query, $attributes) |
||
296 | |||
297 | /** |
||
298 | * Filter a resource query by one or more attributes. |
||
299 | * |
||
300 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
301 | * @param array $attributes |
||
302 | * |
||
303 | * @return \Illuminate\Database\Eloquent\Builder |
||
304 | */ |
||
305 | protected function filterQuery($query, $attributes) |
||
322 | |||
323 | /** |
||
324 | * Update one or more relationships on a model instance. |
||
325 | * |
||
326 | * @param Model $record |
||
327 | * @param array $relationships |
||
328 | */ |
||
329 | protected function updateRecordRelationships($record, array $relationships) |
||
344 | } |
||
345 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.