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 |
||
25 | abstract class JsonApiController extends Controller |
||
26 | { |
||
27 | use JsonApiErrors; |
||
28 | use AuthorizesRequests; |
||
29 | use ValidatesRequests; |
||
30 | |||
31 | /** |
||
32 | * Return the Eloquent Model for the resource. |
||
33 | * |
||
34 | * @return Model |
||
35 | */ |
||
36 | abstract protected function getModel(); |
||
37 | |||
38 | /** |
||
39 | * The model relationships that can be updated. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | protected function getModelRelationships() |
||
47 | |||
48 | /** |
||
49 | * Return a listing of the resource. |
||
50 | * |
||
51 | * @param Request $request |
||
52 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
53 | * |
||
54 | * @return JsonApiResponse |
||
55 | */ |
||
56 | public function indexAction(Request $request, $query = null) |
||
75 | |||
76 | /** |
||
77 | * Store a new record. |
||
78 | * |
||
79 | * @param Request $request |
||
80 | * |
||
81 | * @return JsonApiResponse |
||
82 | */ |
||
83 | public function storeAction(Request $request) |
||
93 | |||
94 | /** |
||
95 | * Return a specified record. |
||
96 | * |
||
97 | * @param Request $request |
||
98 | * @param Model|int $record |
||
99 | * |
||
100 | * @return JsonApiResponse |
||
101 | */ |
||
102 | public function showAction(Request $request, $record) |
||
109 | |||
110 | /** |
||
111 | * Update a specified record. |
||
112 | * |
||
113 | * @param Request $request |
||
114 | * @param Model|int $record |
||
115 | * |
||
116 | * @return JsonApiResponse |
||
117 | */ |
||
118 | public function updateAction(Request $request, $record) |
||
130 | |||
131 | /** |
||
132 | * Destroy a specified record. |
||
133 | * |
||
134 | * @param Request $request |
||
135 | * @param Model|int $record |
||
136 | * |
||
137 | * @return JsonApiResponse |
||
138 | */ |
||
139 | public function destroyAction(Request $request, $record) |
||
146 | |||
147 | /** |
||
148 | * Return a specified record relationship. |
||
149 | * |
||
150 | * @param Request $request |
||
151 | * @param Model|int $record |
||
152 | * @param string $relation |
||
153 | * |
||
154 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
155 | * |
||
156 | * @return JsonApiResponse |
||
157 | */ |
||
158 | public function relationshipAction(Request $request, $record, $relation) |
||
166 | |||
167 | /** |
||
168 | * Update a named many-to-one relationship association on a specified record. |
||
169 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
170 | * |
||
171 | * @param Request $request |
||
172 | * @param Model|int $record |
||
173 | * @param string $relation |
||
174 | * |
||
175 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
176 | * |
||
177 | * @return JsonApiResponse |
||
178 | */ |
||
179 | public function updateToOneRelationshipAction(Request $request, $record, $relation) |
||
192 | |||
193 | /** |
||
194 | * Update named many-to-many relationship entries on a specified record. |
||
195 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
196 | * |
||
197 | * @param Request $request |
||
198 | * @param Model|int $record |
||
199 | * @param string $relation |
||
200 | * |
||
201 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
202 | * |
||
203 | * @return JsonApiResponse |
||
204 | */ |
||
205 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
234 | |||
235 | /** |
||
236 | * Return existing instance of the resource or find by primary key. |
||
237 | * |
||
238 | * @param Model|int $record |
||
239 | * |
||
240 | * @throws ModelNotFoundException |
||
241 | * |
||
242 | * @return Model |
||
243 | */ |
||
244 | protected function findModelInstance($record) |
||
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) |
||
279 | |||
280 | /** |
||
281 | * Return any comma separated values in a request query field as an array. |
||
282 | * |
||
283 | * @param Request $request |
||
284 | * @param string $key |
||
285 | * @param string|null $validate Regular expression to test for each item |
||
286 | * |
||
287 | * @throws \Illuminate\Validation\ValidationException |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
308 | |||
309 | /** |
||
310 | * Sort a resource query by one or more attributes. |
||
311 | * |
||
312 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
313 | * @param array $attributes |
||
314 | * |
||
315 | * @return \Illuminate\Database\Eloquent\Builder |
||
316 | */ |
||
317 | protected function sortQuery($query, $attributes) |
||
327 | |||
328 | /** |
||
329 | * Filter a resource query by one or more attributes. |
||
330 | * |
||
331 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
332 | * @param array $attributes |
||
333 | * |
||
334 | * @return \Illuminate\Database\Eloquent\Builder |
||
335 | */ |
||
336 | protected function filterQuery($query, $attributes) |
||
358 | |||
359 | /** |
||
360 | * Update one or more relationships on a model instance. |
||
361 | * |
||
362 | * @param Model $record |
||
363 | * @param array $relationships |
||
364 | */ |
||
365 | protected function updateRecordRelationships($record, array $relationships) |
||
381 | } |
||
382 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: