Complex classes like ApiResource 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 ApiResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\LaravelApiHelper\Http; |
||
| 26 | class ApiResource implements ArrayAccess, JsonSerializable, Responsable, UrlRoutable |
||
| 27 | { |
||
| 28 | /* ----------------------------------------------------------------- |
||
| 29 | | Traits |
||
| 30 | | ----------------------------------------------------------------- |
||
| 31 | */ |
||
| 32 | |||
| 33 | use DelegatesToResource; |
||
| 34 | |||
| 35 | /* ----------------------------------------------------------------- |
||
| 36 | | Properties |
||
| 37 | | ----------------------------------------------------------------- |
||
| 38 | */ |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The resource instance. |
||
| 42 | * |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | public $resource; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The additional data that should be added to the top-level resource array. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $with = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The additional meta data that should be added to the resource response. |
||
| 56 | * |
||
| 57 | * Added during response construction by the developer. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $additional = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The "data" wrapper that should be applied. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | public static $wrap = 'data'; |
||
| 69 | |||
| 70 | /* ----------------------------------------------------------------- |
||
| 71 | | Constructor |
||
| 72 | | ----------------------------------------------------------------- |
||
| 73 | */ |
||
| 74 | |||
| 75 | /** |
||
| 76 | * PostTransformer constructor. |
||
| 77 | * |
||
| 78 | * @param mixed $resource |
||
| 79 | */ |
||
| 80 | 4 | public function __construct($resource) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Create a new resource instance. |
||
| 87 | * |
||
| 88 | * @param mixed $resource |
||
| 89 | * |
||
| 90 | * @return static |
||
| 91 | */ |
||
| 92 | 4 | public static function make($resource) |
|
| 96 | |||
| 97 | /* ----------------------------------------------------------------- |
||
| 98 | | Main Methods |
||
| 99 | | ----------------------------------------------------------------- |
||
| 100 | */ |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Transform the resource into an array. |
||
| 104 | * |
||
| 105 | * @param \Illuminate\Http\Request $request |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | public function toArray($request) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get any additional data that should be returned with the resource array. |
||
| 116 | * |
||
| 117 | * @param \Illuminate\Http\Request $request |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 4 | public function with($request) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Add additional meta data to the resource response. |
||
| 128 | * |
||
| 129 | * @param array $data |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function additional(array $data) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the string that should wrap the outer-most resource array. |
||
| 142 | * |
||
| 143 | * @param string $value |
||
| 144 | */ |
||
| 145 | public static function wrap($value) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Disable wrapping of the outer-most resource array. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public static function withoutWrapping() |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Customize the response for a request. |
||
| 163 | * |
||
| 164 | * @param \Illuminate\Http\Request $request |
||
| 165 | * @param \Illuminate\Http\JsonResponse $response |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | 4 | public function withResponse($request, $response) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Merge the given attributes. |
||
| 176 | * |
||
| 177 | * @param array $attributes |
||
| 178 | * |
||
| 179 | * @return \Illuminate\Http\Resources\MergeValue |
||
| 180 | */ |
||
| 181 | protected function attributes($attributes) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Retrieve a relationship if it has been loaded. |
||
| 190 | * |
||
| 191 | * @param string $relationship |
||
| 192 | * |
||
| 193 | * @return \Illuminate\Http\Resources\MissingValue|mixed |
||
| 194 | */ |
||
| 195 | protected function whenLoaded($relationship) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Execute a callback if the given pivot table has been loaded. |
||
| 204 | * |
||
| 205 | * @param string $table |
||
| 206 | * @param mixed $value |
||
| 207 | * @param mixed $default |
||
| 208 | * |
||
| 209 | * @return \Illuminate\Http\Resources\MissingValue|mixed |
||
| 210 | */ |
||
| 211 | protected function whenPivotLoaded($table, $value, $default = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Resolve the resource to an array. |
||
| 224 | * |
||
| 225 | * @param \Illuminate\Http\Request|null $request |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 4 | public function resolve($request = null) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Resolve the nested resources to an array. |
||
| 250 | * |
||
| 251 | * @param array $data |
||
| 252 | * @param \Illuminate\Http\Request $request |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 4 | protected function resolveNestedRelations($data, $request) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Resolve the resource to an array. |
||
| 272 | * |
||
| 273 | * @param \Illuminate\Support\Collection $collection |
||
| 274 | * @param \Illuminate\Http\Request|null $request |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function resolveCollection($collection, $request = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Transform the resource into an HTTP response. |
||
| 287 | * |
||
| 288 | * @param \Illuminate\Http\Request|null $request |
||
| 289 | * |
||
| 290 | * @return \Illuminate\Http\Response |
||
| 291 | */ |
||
| 292 | 4 | public function response($request = null) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Create an HTTP response that represents the object. |
||
| 301 | * |
||
| 302 | * @param \Illuminate\Http\Request $request |
||
| 303 | * |
||
| 304 | * @return \Illuminate\Http\Response|mixed |
||
| 305 | */ |
||
| 306 | 4 | public function toResponse($request) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Retrieve a value based on a given condition. |
||
| 317 | * |
||
| 318 | * @param bool $condition |
||
| 319 | * @param mixed $value |
||
| 320 | * @param mixed $default |
||
| 321 | * |
||
| 322 | * @return \Illuminate\Http\Resources\MissingValue|mixed |
||
| 323 | */ |
||
| 324 | protected function when($condition, $value, $default = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Filter the given data, removing any optional values. |
||
| 333 | * |
||
| 334 | * @param array $data |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | 4 | protected function filter($data) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Merge the given data in at the given index. |
||
| 368 | * |
||
| 369 | * @param array $data |
||
| 370 | * @param int $index |
||
| 371 | * @param array $merge |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | protected function merge($data, $index, $merge) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Merge a value based on a given condition. |
||
| 391 | * |
||
| 392 | * @param bool $condition |
||
| 393 | * @param mixed $value |
||
| 394 | * |
||
| 395 | * @return \Illuminate\Http\Resources\MissingValue|mixed |
||
| 396 | */ |
||
| 397 | protected function mergeWhen($condition, $value) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Transform the given value if it is present. |
||
| 404 | * |
||
| 405 | * @param mixed $value |
||
| 406 | * @param callable $callback |
||
| 407 | * @param mixed $default |
||
| 408 | * |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | protected function transform($value, callable $callback, $default = null) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Prepare the resource for JSON serialization. |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function jsonSerialize() |
||
| 427 | } |
||
| 428 |