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 | * Update a named many-to-one relationship association on a specified record. |
||
156 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
157 | * |
||
158 | * @param Request $request |
||
159 | * @param Model|int $record |
||
160 | * @param string $relation |
||
161 | * @param string|null $foreignKey |
||
162 | * |
||
163 | * @return JsonApiResponse |
||
164 | */ |
||
165 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
178 | |||
179 | /** |
||
180 | * Update named many-to-many relationship entries on a specified record. |
||
181 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
182 | * |
||
183 | * @param Request $request |
||
184 | * @param Model|int $record |
||
185 | * @param string $relation |
||
186 | * |
||
187 | * @return JsonApiResponse |
||
188 | */ |
||
189 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
216 | |||
217 | /** |
||
218 | * Return an instance of the resource by primary key. |
||
219 | * |
||
220 | * @param int $key |
||
221 | * |
||
222 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
223 | * |
||
224 | * @return Model |
||
225 | */ |
||
226 | protected function findModelInstance($key) |
||
230 | |||
231 | /** |
||
232 | * Return any JSON API resource parameters from a request. |
||
233 | * |
||
234 | * @param Request $request |
||
235 | * |
||
236 | * @return array |
||
237 | */ |
||
238 | protected function getRequestParameters($request) |
||
247 | |||
248 | /** |
||
249 | * Return any comma separated values in a request query field as an array. |
||
250 | * |
||
251 | * @param Request $request |
||
252 | * @param string $key |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | protected function getRequestQuerySet($request, $key) |
||
260 | |||
261 | /** |
||
262 | * Transform a model instance into a JSON API object. |
||
263 | * |
||
264 | * @param Model $record |
||
265 | * @param array|null $fields Field names of attributes to limit to |
||
266 | * @param array|null $include Relations to include |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | protected function transformRecord($record, array $fields = [], array $include = []) |
||
311 | |||
312 | /** |
||
313 | * Transform a set of models into a JSON API collection. |
||
314 | * |
||
315 | * @param \Illuminate\Support\Collection $records |
||
316 | * @param array $fields |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | protected function transformCollection($records, array $fields = []) |
||
328 | |||
329 | /** |
||
330 | * Transform a set of models into a collection of JSON API resource |
||
331 | * identifier objects. |
||
332 | * |
||
333 | * @param \Illuminate\Support\Collection $records |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | protected function transformCollectionIds($records) |
||
348 | |||
349 | /** |
||
350 | * Update one or more relationships on a model instance. |
||
351 | * |
||
352 | * @param Model $record |
||
353 | * @param array $relationships |
||
354 | */ |
||
355 | protected function updateRecordRelationships($record, array $relationships) |
||
370 | } |
||
371 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.