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 |
||
27 | abstract class JsonApiController extends Controller |
||
28 | { |
||
29 | use JsonApiErrors; |
||
30 | use QueriesResources; |
||
31 | use AuthorizesRequests; |
||
32 | use ValidatesRequests; |
||
33 | |||
34 | /** |
||
35 | * The Eloquent Model for the resource. |
||
36 | * |
||
37 | * @var Model|string |
||
38 | */ |
||
39 | protected $model; |
||
40 | |||
41 | /** |
||
42 | * Create a new JsonApiController instance. |
||
43 | */ |
||
44 | public function __construct() |
||
55 | |||
56 | protected function getModelRelationships() |
||
60 | |||
61 | /** |
||
62 | * Return a listing of the resource. |
||
63 | * |
||
64 | * @param Request $request |
||
65 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
66 | * |
||
67 | * @return JsonApiResponse |
||
68 | */ |
||
69 | public function indexAction(Request $request, $query = null) |
||
89 | |||
90 | /** |
||
91 | * Store a new record. |
||
92 | * |
||
93 | * @param Request $request |
||
94 | * |
||
95 | * @return JsonApiResponse |
||
96 | */ |
||
97 | public function storeAction(Request $request) |
||
107 | |||
108 | /** |
||
109 | * Return a specified record. |
||
110 | * |
||
111 | * @param Request $request |
||
112 | * @param Model|mixed $record |
||
113 | * |
||
114 | * @return JsonApiResponse |
||
115 | */ |
||
116 | public function showAction(Request $request, $record) |
||
124 | |||
125 | /** |
||
126 | * Update a specified record. |
||
127 | * |
||
128 | * @param Request $request |
||
129 | * @param Model|mixed $record |
||
130 | * |
||
131 | * @return JsonApiResponse |
||
132 | */ |
||
133 | public function updateAction(Request $request, $record) |
||
145 | |||
146 | /** |
||
147 | * Destroy a specified record. |
||
148 | * |
||
149 | * @param Request $request |
||
150 | * @param Model|mixed $record |
||
151 | * |
||
152 | * @return JsonApiResponse |
||
153 | */ |
||
154 | public function destroyAction(Request $request, $record) |
||
161 | |||
162 | /** |
||
163 | * Return a specified record relationship. |
||
164 | * |
||
165 | * @param Request $request |
||
166 | * @param Model|mixed $record |
||
167 | * @param string $relation |
||
168 | * |
||
169 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
170 | * |
||
171 | * @return JsonApiResponse |
||
172 | */ |
||
173 | public function relationshipAction(Request $request, $record, $relation) |
||
181 | |||
182 | /** |
||
183 | * Update a named many-to-one relationship association on a specified record. |
||
184 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
185 | * |
||
186 | * @param Request $request |
||
187 | * @param Model|mixed $record |
||
188 | * @param string $relation |
||
189 | * |
||
190 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
191 | * |
||
192 | * @return JsonApiResponse |
||
193 | */ |
||
194 | public function updateToOneRelationshipAction(Request $request, $record, $relation) |
||
207 | |||
208 | /** |
||
209 | * Update named many-to-many relationship entries on a specified record. |
||
210 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
211 | * |
||
212 | * @param Request $request |
||
213 | * @param Model|mixed $record |
||
214 | * @param string $relation |
||
215 | * |
||
216 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
217 | * |
||
218 | * @return JsonApiResponse |
||
219 | */ |
||
220 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
249 | |||
250 | /** |
||
251 | * Return existing instance of the resource or find by primary key. |
||
252 | * |
||
253 | * @param Model|mixed $record |
||
254 | * |
||
255 | * @throws ModelNotFoundException |
||
256 | * |
||
257 | * @return Model |
||
258 | */ |
||
259 | protected function findModelInstance($record) |
||
271 | |||
272 | /** |
||
273 | * Return any JSON API resource parameters from a request. |
||
274 | * |
||
275 | * @param Request $request |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | protected function getRequestParameters($request) |
||
294 | |||
295 | /** |
||
296 | * Return any comma separated values in a request query field as an array. |
||
297 | * |
||
298 | * @param Request $request |
||
299 | * @param string $key |
||
300 | * @param string|null $validate Regular expression to test for each item |
||
301 | * |
||
302 | * @throws \Illuminate\Validation\ValidationException |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
323 | |||
324 | /** |
||
325 | * Validate the requested included relationships against those that are |
||
326 | * allowed on the requested resource type. |
||
327 | * |
||
328 | * @param array|null $relations |
||
329 | * |
||
330 | * @throws InvalidRelationPathException |
||
331 | */ |
||
332 | protected function validateIncludableRelations($relations) |
||
344 | |||
345 | /** |
||
346 | * Update one or more relationships on a model instance. |
||
347 | * |
||
348 | * @param Model $record |
||
349 | * @param array $relationships |
||
350 | */ |
||
351 | protected function updateRecordRelationships($record, array $relationships) |
||
367 | } |
||
368 |
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: