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 |
||
13 | abstract class JsonApiController extends Controller |
||
14 | { |
||
15 | use JsonApiErrors; |
||
16 | |||
17 | /** |
||
18 | * Return the Eloquent Model for the resource. |
||
19 | * |
||
20 | * @return Model |
||
21 | */ |
||
22 | abstract protected function getModel(); |
||
23 | |||
24 | /** |
||
25 | * Return the type name of the resource. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | protected function getModelType() |
||
33 | |||
34 | /** |
||
35 | * The model relationships that can be updated. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | protected function getModelRelationships() |
||
43 | |||
44 | /** |
||
45 | * Return a listing of the resource. |
||
46 | * |
||
47 | * @param Request $request |
||
48 | * |
||
49 | * @return JsonApiResponse |
||
50 | */ |
||
51 | public function indexAction(Request $request) |
||
83 | |||
84 | /** |
||
85 | * Store a new record. |
||
86 | * |
||
87 | * @param Request $request |
||
88 | * |
||
89 | * @return JsonApiResponse |
||
90 | */ |
||
91 | public function storeAction(Request $request) |
||
101 | |||
102 | /** |
||
103 | * Return a specified record. |
||
104 | * |
||
105 | * @param Request $request |
||
106 | * @param Model|int $record |
||
107 | * |
||
108 | * @return JsonApiResponse |
||
109 | */ |
||
110 | public function showAction(Request $request, $record) |
||
117 | |||
118 | /** |
||
119 | * Update a specified record. |
||
120 | * |
||
121 | * @param Request $request |
||
122 | * @param Model|int $record |
||
123 | * |
||
124 | * @return JsonApiResponse |
||
125 | */ |
||
126 | public function updateAction(Request $request, $record) |
||
137 | |||
138 | /** |
||
139 | * Destroy a specified record. |
||
140 | * |
||
141 | * @param Request $request |
||
142 | * @param Model|int $record |
||
143 | * |
||
144 | * @return JsonApiResponse |
||
145 | */ |
||
146 | public function destroyAction(Request $request, $record) |
||
153 | |||
154 | /** |
||
155 | * Return a specified record relationship. |
||
156 | * |
||
157 | * @param Request $request |
||
158 | * @param Model|int $record |
||
159 | * @param string $relation |
||
160 | * |
||
161 | * @return JsonApiResponse |
||
162 | */ |
||
163 | public function relationshipAction(Request $request, $record, $relation) |
||
183 | |||
184 | /** |
||
185 | * Update a named many-to-one relationship association on a specified record. |
||
186 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
187 | * |
||
188 | * @param Request $request |
||
189 | * @param Model|int $record |
||
190 | * @param string $relation |
||
191 | * @param string|null $foreignKey |
||
192 | * |
||
193 | * @return JsonApiResponse |
||
194 | */ |
||
195 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
208 | |||
209 | /** |
||
210 | * Update named many-to-many relationship entries on a specified record. |
||
211 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
212 | * |
||
213 | * @param Request $request |
||
214 | * @param Model|int $record |
||
215 | * @param string $relation |
||
216 | * |
||
217 | * @return JsonApiResponse |
||
218 | */ |
||
219 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
246 | |||
247 | /** |
||
248 | * Return an instance of the resource by primary key. |
||
249 | * |
||
250 | * @param int $key |
||
251 | * |
||
252 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
253 | * |
||
254 | * @return Model |
||
255 | */ |
||
256 | protected function findModelInstance($key) |
||
260 | |||
261 | /** |
||
262 | * Return any JSON API resource parameters from a request. |
||
263 | * |
||
264 | * @param Request $request |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | protected function getRequestParameters($request) |
||
277 | |||
278 | /** |
||
279 | * Return any comma separated values in a request query field as an array. |
||
280 | * |
||
281 | * @param Request $request |
||
282 | * @param string $key |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function getRequestQuerySet($request, $key) |
||
290 | |||
291 | /** |
||
292 | * Transform a model instance into a JSON API object. |
||
293 | * |
||
294 | * @param Model $record |
||
295 | * @param array|null $fields Field names of attributes to limit to |
||
296 | * @param array|null $include Relations to include |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | protected function transformRecord($record, array $fields = [], array $include = []) |
||
341 | |||
342 | /** |
||
343 | * Transform a set of models into a JSON API collection. |
||
344 | * |
||
345 | * @param \Illuminate\Support\Collection $records |
||
346 | * @param array $fields |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | protected function transformCollection($records, array $fields = []) |
||
358 | |||
359 | /** |
||
360 | * Transform a set of models into a collection of JSON API resource |
||
361 | * identifier objects. |
||
362 | * |
||
363 | * @param \Illuminate\Support\Collection $records |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | protected function transformCollectionIds($records) |
||
378 | |||
379 | /** |
||
380 | * Update one or more relationships on a model instance. |
||
381 | * |
||
382 | * @param Model $record |
||
383 | * @param array $relationships |
||
384 | */ |
||
385 | protected function updateRecordRelationships($record, array $relationships) |
||
400 | } |
||
401 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.