Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like JsonApiSerializer 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 JsonApiSerializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class JsonApiSerializer extends ArraySerializer |
||
| 19 | { |
||
| 20 | protected $baseUrl; |
||
| 21 | protected $rootObjects; |
||
| 22 | |||
| 23 | 27 | public function __construct($baseUrl = null) |
|
| 24 | { |
||
| 25 | 27 | $this->baseUrl = $baseUrl; |
|
| 26 | 27 | $this->rootObjects = []; |
|
| 27 | 27 | } |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Serialize a collection. |
||
| 31 | * |
||
| 32 | * @param string $resourceKey |
||
| 33 | * @param array $data |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | 19 | public function collection($resourceKey, array $data) |
|
| 38 | { |
||
| 39 | 19 | $resources = []; |
|
| 40 | |||
| 41 | 19 | foreach ($data as $resource) { |
|
| 42 | 18 | $resources[] = $this->item($resourceKey, $resource)['data']; |
|
| 43 | 19 | } |
|
| 44 | |||
| 45 | 19 | return ['data' => $resources]; |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Serialize an item. |
||
| 50 | * |
||
| 51 | * @param string $resourceKey |
||
| 52 | * @param array $data |
||
| 53 | * |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 27 | public function item($resourceKey, array $data) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Serialize the paginator. |
||
| 81 | * |
||
| 82 | * @param PaginatorInterface $paginator |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 3 | public function paginator(PaginatorInterface $paginator) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Serialize the meta. |
||
| 119 | * |
||
| 120 | * @param array $meta |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 26 | public function meta(array $meta) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 2 | public function null() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Serialize the included data. |
||
| 152 | * |
||
| 153 | * @param ResourceInterface $resource |
||
| 154 | * @param array $data |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 26 | public function includedData(ResourceInterface $resource, array $data) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Indicates if includes should be side-loaded. |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | 27 | public function sideloadIncludes() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $data |
||
| 200 | * @param array $includedData |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 26 | public function injectData($data, $includedData) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Hook to manipulate the final sideloaded includes. |
||
| 217 | * |
||
| 218 | * The JSON API specification does not allow the root object to be included |
||
| 219 | * into the sideloaded `included`-array. We have to make sure it is |
||
| 220 | * filtered out, in case some object links to the root object in a |
||
| 221 | * relationship. |
||
| 222 | * |
||
| 223 | * @param array $includedData |
||
| 224 | * @param array $data |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | 26 | public function filterIncludes($includedData, $data) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Filter function to delete root objects from array. |
||
| 251 | * |
||
| 252 | * @param array $object |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 15 | protected function filterRootObject($object) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Set the root objects of the JSON API tree. |
||
| 263 | * |
||
| 264 | * @param array $objects |
||
| 265 | */ |
||
| 266 | protected function setRootObjects(array $objects = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Determines whether an object is a root object of the JSON API tree. |
||
| 275 | * |
||
| 276 | * @param array $object |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 15 | protected function isRootObject($object) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param array $data |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 17 | protected function isCollection($data) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param array $data |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | 17 | protected function isNull($data) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $data |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | 16 | protected function isEmpty($data) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param array $data |
||
| 319 | * @param array $relationships |
||
| 320 | * |
||
| 321 | * @return mixed |
||
| 322 | */ |
||
| 323 | 17 | protected function fillRelationships($data, $relationships) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param array $includedData |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 26 | protected function parseRelationships($includedData) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @param array $data |
||
| 406 | * |
||
| 407 | * @return mixed |
||
| 408 | */ |
||
| 409 | 27 | protected function getIdFromData(array $data) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Keep all sideloaded inclusion data on the top level. |
||
| 421 | * |
||
| 422 | * @param array $data |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | 26 | protected function pullOutNestedIncludedData(array $data) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Whether or not the serializer should include `links` for resource objects. |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | 26 | protected function shouldIncludeLinks() |
|
| 460 | } |
||
| 461 |