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) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Store a new record. |
||
| 79 | * |
||
| 80 | * @param Request $request |
||
| 81 | * |
||
| 82 | * @return JsonApiResponse |
||
| 83 | */ |
||
| 84 | public function storeAction(Request $request) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return a specified record. |
||
| 97 | * |
||
| 98 | * @param Request $request |
||
| 99 | * @param Model|mixed $record |
||
| 100 | * |
||
| 101 | * @return JsonApiResponse |
||
| 102 | */ |
||
| 103 | public function showAction(Request $request, $record) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Update a specified record. |
||
| 114 | * |
||
| 115 | * @param Request $request |
||
| 116 | * @param Model|mixed $record |
||
| 117 | * |
||
| 118 | * @return JsonApiResponse |
||
| 119 | */ |
||
| 120 | public function updateAction(Request $request, $record) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Destroy a specified record. |
||
| 135 | * |
||
| 136 | * @param Request $request |
||
| 137 | * @param Model|mixed $record |
||
| 138 | * |
||
| 139 | * @return JsonApiResponse |
||
| 140 | */ |
||
| 141 | public function destroyAction(Request $request, $record) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return a specified record relationship. |
||
| 151 | * |
||
| 152 | * @param Request $request |
||
| 153 | * @param Model|mixed $record |
||
| 154 | * @param string $relation |
||
| 155 | * |
||
| 156 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 157 | * |
||
| 158 | * @return JsonApiResponse |
||
| 159 | */ |
||
| 160 | public function relationshipAction(Request $request, $record, $relation) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Update a named many-to-one relationship association on a specified record. |
||
| 171 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
| 172 | * |
||
| 173 | * @param Request $request |
||
| 174 | * @param Model|mixed $record |
||
| 175 | * @param string $relation |
||
| 176 | * |
||
| 177 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 178 | * |
||
| 179 | * @return JsonApiResponse |
||
| 180 | */ |
||
| 181 | public function updateToOneRelationshipAction(Request $request, $record, $relation) |
||
| 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|mixed $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) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return existing instance of the resource or find by primary key. |
||
| 239 | * |
||
| 240 | * @param Model|mixed $record |
||
| 241 | * |
||
| 242 | * @throws ModelNotFoundException |
||
| 243 | * |
||
| 244 | * @return Model |
||
| 245 | */ |
||
| 246 | protected function findModelInstance($record) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return any JSON API resource parameters from a request. |
||
| 261 | * |
||
| 262 | * @param Request $request |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | protected function getRequestParameters($request) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Return any comma separated values in a request query field as an array. |
||
| 278 | * |
||
| 279 | * @param Request $request |
||
| 280 | * @param string $key |
||
| 281 | * @param string|null $validate Regular expression to test for each item |
||
| 282 | * |
||
| 283 | * @throws \Illuminate\Validation\ValidationException |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Validate the requested included relationships against those that are |
||
| 307 | * allowed on the requested resource type. |
||
| 308 | * |
||
| 309 | * @param array|null $relations |
||
| 310 | * |
||
| 311 | * @throws InvalidRelationPathException |
||
| 312 | */ |
||
| 313 | protected function validateIncludableRelations($relations) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Sort 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 sortQuery($query, $attributes) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Filter a resource query by one or more attributes. |
||
| 349 | * |
||
| 350 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 351 | * @param array $attributes |
||
| 352 | * |
||
| 353 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 354 | */ |
||
| 355 | protected function filterQuery($query, $attributes) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Update one or more relationships on a model instance. |
||
| 380 | * |
||
| 381 | * @param Model $record |
||
| 382 | * @param array $relationships |
||
| 383 | */ |
||
| 384 | protected function updateRecordRelationships($record, array $relationships) |
||
| 400 | } |
||
| 401 |
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: