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() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The model relationships that can be updated. |
||
| 38 | * |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | protected function getModelRelationships() |
||
| 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) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Store a new record. |
||
| 88 | * |
||
| 89 | * @param Request $request |
||
| 90 | * |
||
| 91 | * @return JsonApiResponse |
||
| 92 | */ |
||
| 93 | public function storeAction(Request $request) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return a specified record. |
||
| 106 | * |
||
| 107 | * @param Request $request |
||
| 108 | * @param Model|int $record |
||
| 109 | * |
||
| 110 | * @return JsonApiResponse |
||
| 111 | */ |
||
| 112 | public function showAction(Request $request, $record) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Update a specified record. |
||
| 122 | * |
||
| 123 | * @param Request $request |
||
| 124 | * @param Model|int $record |
||
| 125 | * |
||
| 126 | * @return JsonApiResponse |
||
| 127 | */ |
||
| 128 | public function updateAction(Request $request, $record) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Destroy a specified record. |
||
| 142 | * |
||
| 143 | * @param Request $request |
||
| 144 | * @param Model|int $record |
||
| 145 | * |
||
| 146 | * @return JsonApiResponse |
||
| 147 | */ |
||
| 148 | public function destroyAction(Request $request, $record) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return a specified record relationship. |
||
| 158 | * |
||
| 159 | * @param Request $request |
||
| 160 | * @param Model|int $record |
||
| 161 | * @param string $relation |
||
| 162 | * |
||
| 163 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 164 | * |
||
| 165 | * @return JsonApiResponse |
||
| 166 | */ |
||
| 167 | public function relationshipAction(Request $request, $record, $relation) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Update a named many-to-one relationship association on a specified record. |
||
| 188 | * http://jsonapi.org/format/#crud-updating-to-one-relationships |
||
| 189 | * |
||
| 190 | * @param Request $request |
||
| 191 | * @param Model|int $record |
||
| 192 | * @param string $relation |
||
| 193 | * @param string|null $foreignKey |
||
| 194 | * |
||
| 195 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 196 | * |
||
| 197 | * @return JsonApiResponse |
||
| 198 | */ |
||
| 199 | public function updateToOneRelationshipAction(Request $request, $record, $relation, $foreignKey = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Update named many-to-many relationship entries on a specified record. |
||
| 213 | * http://jsonapi.org/format/#crud-updating-to-many-relationships |
||
| 214 | * |
||
| 215 | * @param Request $request |
||
| 216 | * @param Model|int $record |
||
| 217 | * @param string $relation |
||
| 218 | * |
||
| 219 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
| 220 | * |
||
| 221 | * @return JsonApiResponse |
||
| 222 | */ |
||
| 223 | public function updateToManyRelationshipAction(Request $request, $record, $relation) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return an instance of the resource by primary key. |
||
| 251 | * |
||
| 252 | * @param int $key |
||
| 253 | * |
||
| 254 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
| 255 | * |
||
| 256 | * @return Model |
||
| 257 | */ |
||
| 258 | protected function findModelInstance($key) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return any JSON API resource parameters from a request. |
||
| 265 | * |
||
| 266 | * @param Request $request |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | protected function getRequestParameters($request) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return any comma separated values in a request query field as an array. |
||
| 282 | * |
||
| 283 | * @param Request $request |
||
| 284 | * @param string $key |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | protected function getRequestQuerySet($request, $key) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Transform a model instance into a JSON API object. |
||
| 295 | * |
||
| 296 | * @param Model $record |
||
| 297 | * @param array|null $fields Field names of attributes to limit to |
||
| 298 | * @param array|null $include Relations to include |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function transformRecord($record, array $fields = [], array $include = []) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Transform a set of models into a JSON API collection. |
||
| 346 | * |
||
| 347 | * @param \Illuminate\Support\Collection $records |
||
| 348 | * @param array $fields |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | protected function transformCollection($records, array $fields = []) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Transform a set of models into a collection of JSON API resource |
||
| 363 | * identifier objects. |
||
| 364 | * |
||
| 365 | * @param \Illuminate\Support\Collection $records |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | protected function transformCollectionIds($records) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Update one or more relationships on a model instance. |
||
| 383 | * |
||
| 384 | * @param Model $record |
||
| 385 | * @param array $relationships |
||
| 386 | */ |
||
| 387 | protected function updateRecordRelationships($record, array $relationships) |
||
| 402 | } |
||
| 403 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.