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 AuthorizesRequests; |
||
| 31 | use ValidatesRequests; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return the Eloquent Model for the resource. |
||
| 35 | * |
||
| 36 | * @return Model |
||
| 37 | */ |
||
| 38 | abstract protected function getModel(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The model relationships that can be updated. |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | protected function getModelRelationships() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Return a listing of the resource. |
||
| 52 | * |
||
| 53 | * @param Request $request |
||
| 54 | * @param \Illuminate\Database\Eloquent\Builder|null $query Custom resource query |
||
| 55 | * |
||
| 56 | * @return JsonApiResponse |
||
| 57 | */ |
||
| 58 | public function indexAction(Request $request, $query = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Store a new record. |
||
| 81 | * |
||
| 82 | * @param Request $request |
||
| 83 | * |
||
| 84 | * @return JsonApiResponse |
||
| 85 | */ |
||
| 86 | public function storeAction(Request $request) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return a specified record. |
||
| 99 | * |
||
| 100 | * @param Request $request |
||
| 101 | * @param Model|int $record |
||
| 102 | * |
||
| 103 | * @return JsonApiResponse |
||
| 104 | */ |
||
| 105 | public function showAction(Request $request, $record) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Update a specified record. |
||
| 116 | * |
||
| 117 | * @param Request $request |
||
| 118 | * @param Model|int $record |
||
| 119 | * |
||
| 120 | * @return JsonApiResponse |
||
| 121 | */ |
||
| 122 | public function updateAction(Request $request, $record) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Destroy a specified record. |
||
| 137 | * |
||
| 138 | * @param Request $request |
||
| 139 | * @param Model|int $record |
||
| 140 | * |
||
| 141 | * @return JsonApiResponse |
||
| 142 | */ |
||
| 143 | public function destroyAction(Request $request, $record) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Return a specified record relationship. |
||
| 153 | * |
||
| 154 | * @param Request $request |
||
| 155 | * @param Model|int $record |
||
| 156 | * @param string $relation |
||
| 157 | * |
||
| 158 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 159 | * |
||
| 160 | * @return JsonApiResponse |
||
| 161 | */ |
||
| 162 | public function relationshipAction(Request $request, $record, $relation) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Update a named many-to-one relationship association on a specified record. |
||
| 173 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
| 174 | * |
||
| 175 | * @param Request $request |
||
| 176 | * @param Model|int $record |
||
| 177 | * @param string $relation |
||
| 178 | * |
||
| 179 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 180 | * |
||
| 181 | * @return JsonApiResponse |
||
| 182 | */ |
||
| 183 | public function updateToOneRelationshipAction(Request $request, $record, $relation) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Update named many-to-many relationship entries on a specified record. |
||
| 199 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
| 200 | * |
||
| 201 | * @param Request $request |
||
| 202 | * @param Model|int $record |
||
| 203 | * @param string $relation |
||
| 204 | * |
||
| 205 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 206 | * |
||
| 207 | * @return JsonApiResponse |
||
| 208 | */ |
||
| 209 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return existing instance of the resource or find by primary key. |
||
| 241 | * |
||
| 242 | * @param Model|int $record |
||
| 243 | * |
||
| 244 | * @throws ModelNotFoundException |
||
| 245 | * |
||
| 246 | * @return Model |
||
| 247 | */ |
||
| 248 | protected function findModelInstance($record) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Return any JSON API resource parameters from a request. |
||
| 263 | * |
||
| 264 | * @param Request $request |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | protected function getRequestParameters($request) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return any comma separated values in a request query field as an array. |
||
| 286 | * |
||
| 287 | * @param Request $request |
||
| 288 | * @param string $key |
||
| 289 | * @param string|null $validate Regular expression to test for each item |
||
| 290 | * |
||
| 291 | * @throws \Illuminate\Validation\ValidationException |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | protected function getRequestQuerySet($request, $key, $validate = null) |
||
| 296 | { |
||
| 297 | $values = preg_split('/,/', $request->input($key), null, PREG_SPLIT_NO_EMPTY); |
||
| 298 | |||
| 299 | $validator = Validator::make(['param' => $values], [ |
||
| 300 | 'param.*' => 'required' . ($validate ? '|regex:' . $validate : ''), |
||
| 301 | ]); |
||
| 302 | |||
| 303 | if ($validator->fails()) { |
||
| 304 | throw new ValidationException($validator, $this->error( |
||
| 305 | Response::HTTP_BAD_REQUEST, |
||
| 306 | sprintf('Invalid values for "%s" parameter', $key)) |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | |||
| 310 | return $values; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Validate the requested included relationships against those that are |
||
| 315 | * allowed on the requested resource type. |
||
| 316 | * |
||
| 317 | * @param array|null $relations |
||
| 318 | * |
||
| 319 | * @throws InvalidRelationPathException |
||
| 320 | */ |
||
| 321 | protected function validateIncludableRelations($relations) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sort a resource query by one or more attributes. |
||
| 338 | * |
||
| 339 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 340 | * @param array $attributes |
||
| 341 | * |
||
| 342 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 343 | */ |
||
| 344 | protected function sortQuery($query, $attributes) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Filter a resource query by one or more attributes. |
||
| 357 | * |
||
| 358 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 359 | * @param array $attributes |
||
| 360 | * |
||
| 361 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 362 | */ |
||
| 363 | protected function filterQuery($query, $attributes) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Update one or more relationships on a model instance. |
||
| 388 | * |
||
| 389 | * @param Model $record |
||
| 390 | * @param array $relationships |
||
| 391 | */ |
||
| 392 | protected function updateRecordRelationships($record, array $relationships) |
||
| 408 | } |
||
| 409 |
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: