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 |
||
| 26 | abstract class JsonApiController extends Controller |
||
| 27 | { |
||
| 28 | use JsonApiErrors; |
||
| 29 | use AuthorizesRequests; |
||
| 30 | use ValidatesRequests; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Return the Eloquent Model for the resource. |
||
| 34 | * |
||
| 35 | * @return Model |
||
| 36 | */ |
||
| 37 | abstract protected function getModel(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The model relationships that can be updated. |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | protected function getModelRelationships() |
||
| 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) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Store a new record. |
||
| 80 | * |
||
| 81 | * @param Request $request |
||
| 82 | * |
||
| 83 | * @return JsonApiResponse |
||
| 84 | */ |
||
| 85 | public function storeAction(Request $request) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Return a specified record. |
||
| 98 | * |
||
| 99 | * @param Request $request |
||
| 100 | * @param Model|int $record |
||
| 101 | * |
||
| 102 | * @return JsonApiResponse |
||
| 103 | */ |
||
| 104 | public function showAction(Request $request, $record) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Update a specified record. |
||
| 115 | * |
||
| 116 | * @param Request $request |
||
| 117 | * @param Model|int $record |
||
| 118 | * |
||
| 119 | * @return JsonApiResponse |
||
| 120 | */ |
||
| 121 | public function updateAction(Request $request, $record) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Destroy a specified record. |
||
| 136 | * |
||
| 137 | * @param Request $request |
||
| 138 | * @param Model|int $record |
||
| 139 | * |
||
| 140 | * @return JsonApiResponse |
||
| 141 | */ |
||
| 142 | public function destroyAction(Request $request, $record) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return a specified record relationship. |
||
| 152 | * |
||
| 153 | * @param Request $request |
||
| 154 | * @param Model|int $record |
||
| 155 | * @param string $relation |
||
| 156 | * |
||
| 157 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 158 | * |
||
| 159 | * @return JsonApiResponse |
||
| 160 | */ |
||
| 161 | 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 | * |
||
| 178 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 179 | * |
||
| 180 | * @return JsonApiResponse |
||
| 181 | */ |
||
| 182 | public function updateToOneRelationshipAction(Request $request, $record, $relation) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Update named many-to-many relationship entries on a specified record. |
||
| 198 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
| 199 | * |
||
| 200 | * @param Request $request |
||
| 201 | * @param Model|int $record |
||
| 202 | * @param string $relation |
||
| 203 | * |
||
| 204 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 205 | * |
||
| 206 | * @return JsonApiResponse |
||
| 207 | */ |
||
| 208 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return existing instance of the resource or find by primary key. |
||
| 240 | * |
||
| 241 | * @param Model|int $record |
||
| 242 | * |
||
| 243 | * @throws ModelNotFoundException |
||
| 244 | * |
||
| 245 | * @return Model |
||
| 246 | */ |
||
| 247 | protected function findModelInstance($record) |
||
| 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 | * @param string|null $validate Regular expression to test for each item |
||
| 283 | * |
||
| 284 | * @throws \Illuminate\Validation\ValidationException |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Validate the requested included relationships against those that are |
||
| 308 | * allowed on the requested resource type. |
||
| 309 | * |
||
| 310 | * @param array|null $relations |
||
| 311 | * |
||
| 312 | * @throws InvalidRelationPathException |
||
| 313 | */ |
||
| 314 | protected function validateIncludableRelations($relations) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Sort a resource query by one or more attributes. |
||
| 331 | * |
||
| 332 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 333 | * @param array $attributes |
||
| 334 | * |
||
| 335 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 336 | */ |
||
| 337 | protected function sortQuery($query, $attributes) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Filter a resource query by one or more attributes. |
||
| 350 | * |
||
| 351 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 352 | * @param array $attributes |
||
| 353 | * |
||
| 354 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 355 | */ |
||
| 356 | protected function filterQuery($query, $attributes) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Update one or more relationships on a model instance. |
||
| 381 | * |
||
| 382 | * @param Model $record |
||
| 383 | * @param array $relationships |
||
| 384 | */ |
||
| 385 | protected function updateRecordRelationships($record, array $relationships) |
||
| 401 | } |
||
| 402 |
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: