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 |
||
| 15 | abstract class JsonApiController extends Controller |
||
| 16 | { |
||
| 17 | use JsonApiErrors; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Return the Eloquent Model for the resource. |
||
| 21 | * |
||
| 22 | * @return Model |
||
| 23 | */ |
||
| 24 | abstract protected function getModel(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Return the type name of the resource. |
||
| 28 | * |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | protected function getModelType() |
||
| 32 | { |
||
| 33 | return $this->getModel()->getTable(); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The model relationships that can be updated. |
||
| 38 | * |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | protected function getModelRelationships() |
||
| 42 | { |
||
| 43 | return []; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Return a listing of the resource. |
||
| 48 | * |
||
| 49 | * @param Request $request |
||
| 50 | * |
||
| 51 | * @return JsonApiResponse |
||
| 52 | */ |
||
| 53 | public function indexAction(Request $request) |
||
| 54 | { |
||
| 55 | $records = $this->getModel()->newQuery(); |
||
| 56 | $params = $this->getRequestParameters($request); |
||
| 57 | |||
| 58 | $records = $this->sortQuery($records, $params['sort']); |
||
|
|
|||
| 59 | $records = $this->filterQuery($records, $params['filter']); |
||
| 60 | |||
| 61 | try { |
||
| 62 | $records = $records->get(); |
||
| 63 | } catch (QueryException $e) { |
||
| 64 | return $this->error(Response::HTTP_BAD_REQUEST, 'Invalid query parameters'); |
||
| 65 | } |
||
| 66 | |||
| 67 | return new JsonApiResponse($this->transformCollection($records, $params['fields'])); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Store a new record. |
||
| 72 | * |
||
| 73 | * @param Request $request |
||
| 74 | * |
||
| 75 | * @return JsonApiResponse |
||
| 76 | */ |
||
| 77 | public function storeAction(Request $request) |
||
| 78 | { |
||
| 79 | $record = $this->getModel()->create((array) $request->input('data.attributes')); |
||
| 80 | |||
| 81 | if ($relationships = $request->input('data.relationships')) { |
||
| 82 | $this->updateRecordRelationships($record, (array) $relationships); |
||
| 83 | } |
||
| 84 | |||
| 85 | return new JsonApiResponse($this->transformRecord($record), Response::HTTP_CREATED); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Return a specified record. |
||
| 90 | * |
||
| 91 | * @param Request $request |
||
| 92 | * @param Model|int $record |
||
| 93 | * |
||
| 94 | * @return JsonApiResponse |
||
| 95 | */ |
||
| 96 | public function showAction(Request $request, $record) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Update a specified record. |
||
| 106 | * |
||
| 107 | * @param Request $request |
||
| 108 | * @param Model|int $record |
||
| 109 | * |
||
| 110 | * @return JsonApiResponse |
||
| 111 | */ |
||
| 112 | public function updateAction(Request $request, $record) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Destroy a specified record. |
||
| 126 | * |
||
| 127 | * @param Request $request |
||
| 128 | * @param Model|int $record |
||
| 129 | * |
||
| 130 | * @return JsonApiResponse |
||
| 131 | */ |
||
| 132 | public function destroyAction(Request $request, $record) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Return a specified record relationship. |
||
| 142 | * |
||
| 143 | * @param Request $request |
||
| 144 | * @param Model|int $record |
||
| 145 | * @param string $relation |
||
| 146 | * |
||
| 147 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 148 | * |
||
| 149 | * @return JsonApiResponse |
||
| 150 | */ |
||
| 151 | public function relationshipAction(Request $request, $record, $relation) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Update a named many-to-one relationship association on a specified record. |
||
| 172 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
| 173 | * |
||
| 174 | * @param Request $request |
||
| 175 | * @param Model|int $record |
||
| 176 | * @param string $relation |
||
| 177 | * @param string|null $foreignKey |
||
| 178 | * |
||
| 179 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 180 | * |
||
| 181 | * @return JsonApiResponse |
||
| 182 | */ |
||
| 183 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Update named many-to-many relationship entries on a specified record. |
||
| 197 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
| 198 | * |
||
| 199 | * @param Request $request |
||
| 200 | * @param Model|int $record |
||
| 201 | * @param string $relation |
||
| 202 | * |
||
| 203 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 204 | * |
||
| 205 | * @return JsonApiResponse |
||
| 206 | */ |
||
| 207 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return an instance of the resource by primary key. |
||
| 235 | * |
||
| 236 | * @param int $key |
||
| 237 | * |
||
| 238 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 239 | * |
||
| 240 | * @return Model |
||
| 241 | */ |
||
| 242 | protected function findModelInstance($key) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Return any JSON API resource parameters from a request. |
||
| 249 | * |
||
| 250 | * @param Request $request |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | protected function getRequestParameters($request) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return any comma separated values in a request query field as an array. |
||
| 266 | * |
||
| 267 | * @param Request $request |
||
| 268 | * @param string $key |
||
| 269 | * |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | protected function getRequestQuerySet($request, $key) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Sort a resource query by one or more attributes. |
||
| 279 | * |
||
| 280 | * @param Illuminate\Database\Eloquent\Builder $query |
||
| 281 | * @param array $attributes |
||
| 282 | * |
||
| 283 | * @return Illuminate\Database\Eloquent\Builder |
||
| 284 | */ |
||
| 285 | public function sortQuery($query, $attributes) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Filter a resource query by one or more attributes. |
||
| 298 | * |
||
| 299 | * @param Illuminate\Database\Eloquent\Builder $query |
||
| 300 | * @param array $attributes |
||
| 301 | * |
||
| 302 | * @return Illuminate\Database\Eloquent\Builder |
||
| 303 | */ |
||
| 304 | public function filterQuery($query, $attributes) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Transform a model instance into a JSON API object. |
||
| 324 | * |
||
| 325 | * @param Model $record |
||
| 326 | * @param array|null $fields Field names of attributes to limit to |
||
| 327 | * @param array|null $include Relations to include |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | protected function transformRecord($record, array $fields = [], array $include = []) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Transform a set of models into a JSON API collection. |
||
| 375 | * |
||
| 376 | * @param \Illuminate\Support\Collection $records |
||
| 377 | * @param array $fields |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function transformCollection($records, array $fields = []) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Transform a set of models into a collection of JSON API resource |
||
| 392 | * identifier objects. |
||
| 393 | * |
||
| 394 | * @param \Illuminate\Support\Collection $records |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | protected function transformCollectionIds($records) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Update one or more relationships on a model instance. |
||
| 412 | * |
||
| 413 | * @param Model $record |
||
| 414 | * @param array $relationships |
||
| 415 | */ |
||
| 416 | protected function updateRecordRelationships($record, array $relationships) |
||
| 431 | } |
||
| 432 |
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: