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 |
||
18 | abstract class JsonApiController extends Controller |
||
19 | { |
||
20 | use JsonApiErrors, JsonApiTransforms, AuthorizesRequests, ValidatesRequests; |
||
21 | |||
22 | /** |
||
23 | * Return the Eloquent Model for the resource. |
||
24 | * |
||
25 | * @return Model |
||
26 | */ |
||
27 | abstract protected function getModel(); |
||
28 | |||
29 | /** |
||
30 | * Return the type name of the resource. |
||
31 | * |
||
32 | * @return string |
||
33 | */ |
||
34 | protected function getModelType() |
||
35 | { |
||
36 | return $this->getModel()->getTable(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * The model relationships that can be updated. |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | protected function getModelRelationships() |
||
45 | { |
||
46 | return []; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return a listing of the resource. |
||
51 | * |
||
52 | * @param Request $request |
||
53 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
54 | * |
||
55 | * @return JsonApiResponse |
||
56 | */ |
||
57 | public function indexAction(Request $request, $query = null) |
||
58 | { |
||
59 | $records = $query ?: $this->getModel()->newQuery(); |
||
60 | $params = $this->getRequestParameters($request); |
||
61 | |||
62 | $records = $this->sortQuery($records, $params['sort']); |
||
63 | $records = $this->filterQuery($records, $params['filter']); |
||
64 | |||
65 | try { |
||
66 | $pageSize = min($this->getModel()->getPerPage(), $request->input('page.size')); |
||
67 | $pageNumber = $request->input('page.number') ?: 1; |
||
68 | |||
69 | $records = $records->paginate($pageSize, null, 'page', $pageNumber); |
||
|
|||
70 | } catch (QueryException $e) { |
||
71 | return $this->error(Response::HTTP_BAD_REQUEST, 'Invalid query parameters'); |
||
72 | } |
||
73 | |||
74 | return new JsonApiResponse($this->transformCollection($records, $params['fields'])); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Store a new record. |
||
79 | * |
||
80 | * @param Request $request |
||
81 | * |
||
82 | * @return JsonApiResponse |
||
83 | */ |
||
84 | public function storeAction(Request $request) |
||
85 | { |
||
86 | $record = $this->getModel()->create((array) $request->input('data.attributes')); |
||
87 | |||
88 | if ($relationships = $request->input('data.relationships')) { |
||
89 | $this->updateRecordRelationships($record, (array) $relationships); |
||
90 | } |
||
91 | |||
92 | return new JsonApiResponse($this->transformRecord($record), Response::HTTP_CREATED); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return a specified record. |
||
97 | * |
||
98 | * @param Request $request |
||
99 | * @param Model|int $record |
||
100 | * |
||
101 | * @return JsonApiResponse |
||
102 | */ |
||
103 | public function showAction(Request $request, $record) |
||
104 | { |
||
105 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
106 | $params = $this->getRequestParameters($request); |
||
107 | |||
108 | return new JsonApiResponse($this->transformRecord($record, $params['fields'], $params['include'])); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Update a specified record. |
||
113 | * |
||
114 | * @param Request $request |
||
115 | * @param Model|int $record |
||
116 | * |
||
117 | * @return JsonApiResponse |
||
118 | */ |
||
119 | public function updateAction(Request $request, $record) |
||
120 | { |
||
121 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
122 | $record->update((array) $request->input('data.attributes')); |
||
123 | |||
124 | if ($relationships = $request->input('data.relationships')) { |
||
125 | $this->updateRecordRelationships($record, (array) $relationships); |
||
126 | } |
||
127 | |||
128 | return $this->showAction($request, $record); |
||
129 | } |
||
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) |
||
140 | { |
||
141 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
142 | $record->delete(); |
||
143 | |||
144 | return new JsonApiResponse(null, Response::HTTP_NO_CONTENT); |
||
145 | } |
||
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) |
||
159 | { |
||
160 | abort_if(!array_key_exists($relation, $this->getModelRelationships()), Response::HTTP_NOT_FOUND); |
||
161 | |||
162 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
163 | |||
164 | return new JsonApiResponse($this->transformRelationship($record->{$relation})); |
||
165 | } |
||
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 | * @param string|null $foreignKey |
||
175 | * |
||
176 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
177 | * |
||
178 | * @return JsonApiResponse |
||
179 | */ |
||
180 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
181 | { |
||
182 | abort_if(!array_key_exists($relation, $this->getModelRelationships()), Response::HTTP_NOT_FOUND); |
||
183 | |||
184 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
185 | $data = (array) $request->input('data'); |
||
186 | |||
187 | $record->update([($foreignKey ?: $relation . '_id') => $data['id']]); |
||
188 | |||
189 | return new JsonApiResponse(); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Update named many-to-many relationship entries on a specified record. |
||
194 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
195 | * |
||
196 | * @param Request $request |
||
197 | * @param Model|int $record |
||
198 | * @param string $relation |
||
199 | * |
||
200 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
201 | * |
||
202 | * @return JsonApiResponse |
||
203 | */ |
||
204 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
205 | { |
||
206 | abort_if(!array_key_exists($relation, $this->getModelRelationships()), Response::HTTP_NOT_FOUND); |
||
207 | |||
208 | $record = $record instanceof Model ? $record : $this->findModelInstance($record); |
||
209 | $relationships = (array) $request->input('data'); |
||
210 | $items = []; |
||
211 | |||
212 | foreach ($relationships as $item) { |
||
213 | if (isset($item['attributes'])) { |
||
214 | $items[$item['id']] = $item['attributes']; |
||
215 | } else { |
||
216 | $items[] = $item['id']; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | switch ($request->method()) { |
||
221 | case 'PATCH': |
||
222 | $record->{$relation}()->sync($items); |
||
223 | break; |
||
224 | case 'POST': |
||
225 | $record->{$relation}()->sync($items, false); |
||
226 | break; |
||
227 | case 'DELETE': |
||
228 | $record->{$relation}()->detach(array_keys($items)); |
||
229 | } |
||
230 | |||
231 | return new JsonApiResponse(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Return an instance of the resource by primary key. |
||
236 | * |
||
237 | * @param int $key |
||
238 | * |
||
239 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
240 | * |
||
241 | * @return Model |
||
242 | */ |
||
243 | protected function findModelInstance($key) |
||
247 | |||
248 | /** |
||
249 | * Return any JSON API resource parameters from a request. |
||
250 | * |
||
251 | * @param Request $request |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function getRequestParameters($request) |
||
264 | |||
265 | /** |
||
266 | * Return any comma separated values in a request query field as an array. |
||
267 | * |
||
268 | * @param Request $request |
||
269 | * @param string $key |
||
270 | * |
||
271 | * @return array |
||
272 | */ |
||
273 | protected function getRequestQuerySet($request, $key) |
||
277 | |||
278 | /** |
||
279 | * Sort a resource query by one or more attributes. |
||
280 | * |
||
281 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
282 | * @param array $attributes |
||
283 | * |
||
284 | * @return \Illuminate\Database\Eloquent\Builder |
||
285 | */ |
||
286 | protected function sortQuery($query, $attributes) |
||
296 | |||
297 | /** |
||
298 | * Filter a resource query by one or more attributes. |
||
299 | * |
||
300 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
301 | * @param array $attributes |
||
302 | * |
||
303 | * @return \Illuminate\Database\Eloquent\Builder |
||
304 | */ |
||
305 | protected function filterQuery($query, $attributes) |
||
322 | |||
323 | /** |
||
324 | * Update one or more relationships on a model instance. |
||
325 | * |
||
326 | * @param Model $record |
||
327 | * @param array $relationships |
||
328 | */ |
||
329 | protected function updateRecordRelationships($record, array $relationships) |
||
344 | } |
||
345 |
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: