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) |
||
74 | |||
75 | /** |
||
76 | * Store a new record. |
||
77 | * |
||
78 | * @param Request $request |
||
79 | * |
||
80 | * @return JsonApiResponse |
||
81 | */ |
||
82 | public function storeAction(Request $request) |
||
92 | |||
93 | /** |
||
94 | * Return a specified record. |
||
95 | * |
||
96 | * @param Request $request |
||
97 | * @param Model|int $record |
||
98 | * |
||
99 | * @return JsonApiResponse |
||
100 | */ |
||
101 | public function showAction(Request $request, $record) |
||
108 | |||
109 | /** |
||
110 | * Update a specified record. |
||
111 | * |
||
112 | * @param Request $request |
||
113 | * @param Model|int $record |
||
114 | * |
||
115 | * @return JsonApiResponse |
||
116 | */ |
||
117 | public function updateAction(Request $request, $record) |
||
128 | |||
129 | /** |
||
130 | * Destroy a specified record. |
||
131 | * |
||
132 | * @param Request $request |
||
133 | * @param Model|int $record |
||
134 | * |
||
135 | * @return JsonApiResponse |
||
136 | */ |
||
137 | public function destroyAction(Request $request, $record) |
||
144 | |||
145 | /** |
||
146 | * Return a specified record relationship. |
||
147 | * |
||
148 | * @param Request $request |
||
149 | * @param Model|int $record |
||
150 | * @param string $relation |
||
151 | * |
||
152 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
153 | * |
||
154 | * @return JsonApiResponse |
||
155 | */ |
||
156 | public function relationshipAction(Request $request, $record, $relation) |
||
178 | |||
179 | /** |
||
180 | * Update a named many-to-one relationship association on a specified record. |
||
181 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
182 | * |
||
183 | * @param Request $request |
||
184 | * @param Model|int $record |
||
185 | * @param string $relation |
||
186 | * @param string|null $foreignKey |
||
187 | * |
||
188 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
189 | * |
||
190 | * @return JsonApiResponse |
||
191 | */ |
||
192 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
203 | |||
204 | /** |
||
205 | * Update named many-to-many relationship entries on a specified record. |
||
206 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
207 | * |
||
208 | * @param Request $request |
||
209 | * @param Model|int $record |
||
210 | * @param string $relation |
||
211 | * |
||
212 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
213 | * |
||
214 | * @return JsonApiResponse |
||
215 | */ |
||
216 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
245 | |||
246 | /** |
||
247 | * Return an instance of the resource by primary key. |
||
248 | * |
||
249 | * @param int $key |
||
250 | * |
||
251 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
252 | * |
||
253 | * @return Model |
||
254 | */ |
||
255 | protected function findModelInstance($key) |
||
259 | |||
260 | /** |
||
261 | * Return any JSON API resource parameters from a request. |
||
262 | * |
||
263 | * @param Request $request |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | protected function getRequestParameters($request) |
||
276 | |||
277 | /** |
||
278 | * Return any comma separated values in a request query field as an array. |
||
279 | * |
||
280 | * @param Request $request |
||
281 | * @param string $key |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | protected function getRequestQuerySet($request, $key) |
||
289 | |||
290 | /** |
||
291 | * Sort a resource query by one or more attributes. |
||
292 | * |
||
293 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
294 | * @param array $attributes |
||
295 | * |
||
296 | * @return \Illuminate\Database\Eloquent\Builder |
||
297 | */ |
||
298 | protected function sortQuery($query, $attributes) |
||
308 | |||
309 | /** |
||
310 | * Filter 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 filterQuery($query, $attributes) |
||
334 | |||
335 | /** |
||
336 | * Update one or more relationships on a model instance. |
||
337 | * |
||
338 | * @param Model $record |
||
339 | * @param array $relationships |
||
340 | */ |
||
341 | protected function updateRecordRelationships($record, array $relationships) |
||
356 | } |
||
357 |
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: