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 | 25 | public function __construct($baseUrl = null) |
|
| 24 | { |
||
| 25 | 25 | $this->baseUrl = $baseUrl; |
|
| 26 | 25 | $this->rootObjects = []; |
|
| 27 | 25 | } |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Serialize a collection. |
||
| 31 | * |
||
| 32 | * @param string $resourceKey |
||
| 33 | * @param array $data |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | 17 | public function collection($resourceKey, array $data) |
|
| 38 | { |
||
| 39 | 17 | $resources = []; |
|
| 40 | |||
| 41 | 17 | foreach ($data as $resource) { |
|
| 42 | 16 | $resources[] = $this->item($resourceKey, $resource)['data']; |
|
| 43 | 17 | } |
|
| 44 | |||
| 45 | 17 | 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 | 25 | 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) |
|
| 87 | { |
||
| 88 | 3 | $currentPage = (int)$paginator->getCurrentPage(); |
|
| 89 | 3 | $lastPage = (int)$paginator->getLastPage(); |
|
| 90 | |||
| 91 | $pagination = [ |
||
| 92 | 3 | 'total' => (int)$paginator->getTotal(), |
|
| 93 | 3 | 'count' => (int)$paginator->getCount(), |
|
| 94 | 3 | 'per_page' => (int)$paginator->getPerPage(), |
|
| 95 | 3 | 'current_page' => $currentPage, |
|
| 96 | 3 | 'total_pages' => $lastPage, |
|
| 97 | 3 | ]; |
|
| 98 | |||
| 99 | 3 | $pagination['links'] = []; |
|
| 100 | |||
| 101 | 3 | $pagination['links']['self'] = $paginator->getUrl($currentPage); |
|
| 102 | 3 | $pagination['links']['first'] = $paginator->getUrl(1); |
|
| 103 | |||
| 104 | 3 | if ($currentPage > 1) { |
|
| 105 | 2 | $pagination['links']['prev'] = $paginator->getUrl($currentPage - 1); |
|
| 106 | 2 | } |
|
| 107 | |||
| 108 | 3 | if ($currentPage < $lastPage) { |
|
| 109 | 2 | $pagination['links']['next'] = $paginator->getUrl($currentPage + 1); |
|
| 110 | 2 | } |
|
| 111 | |||
| 112 | 3 | $pagination['links']['last'] = $paginator->getUrl($lastPage); |
|
| 113 | |||
| 114 | 3 | return ['pagination' => $pagination]; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Serialize the meta. |
||
| 119 | * |
||
| 120 | * @param array $meta |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 24 | 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 | 24 | public function includedData(ResourceInterface $resource, array $data) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Indicates if includes should be side-loaded. |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 25 | public function sideloadIncludes() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param array $data |
||
| 197 | * @param array $includedData |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 24 | public function injectData($data, $includedData) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Hook to manipulate the final sideloaded includes. |
||
| 214 | * The JSON API specification does not allow the root object to be included |
||
| 215 | * into the sideloaded `included`-array. We have to make sure it is |
||
| 216 | * filtered out, in case some object links to the root object in a |
||
| 217 | * relationship. |
||
| 218 | * |
||
| 219 | * @param array $includedData |
||
| 220 | * @param array $data |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 24 | public function filterIncludes($includedData, $data) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Filter function to delete root objects from array. |
||
| 244 | * |
||
| 245 | * @param array $object |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 13 | protected function filterRootObject($object) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Set the root objects of the JSON API tree. |
||
| 256 | * |
||
| 257 | * @param array $objects |
||
| 258 | */ |
||
| 259 | protected function setRootObjects(array $objects = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Determines whether an object is a root object of the JSON API tree. |
||
| 268 | * |
||
| 269 | * @param array $object |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 13 | protected function isRootObject($object) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $data |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 15 | protected function isCollection($data) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $data |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | 15 | protected function isNull($data) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $data |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | 14 | protected function isEmpty($data) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param array $data |
||
| 312 | * @param array $relationships |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 15 | protected function fillRelationships($data, $relationships) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $includedData |
||
| 333 | * |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | 24 | protected function parseRelationships($includedData) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param array $data |
||
| 351 | * |
||
| 352 | * @return integer |
||
| 353 | */ |
||
| 354 | 25 | protected function getIdFromData(array $data) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Keep all sideloaded inclusion data on the top level. |
||
| 366 | * |
||
| 367 | * @param array $data |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | 24 | protected function pullOutNestedIncludedData(array $data) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Whether or not the serializer should include `links` for resource objects. |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | 24 | protected function shouldIncludeLinks() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Check if the objects are part of a collection or not |
||
| 408 | * |
||
| 409 | * @param $includeObject |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | 13 | private function CreateIncludeObjects($includeObject) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Sets the RootObjects, either as collection or not. |
||
| 428 | * |
||
| 429 | * @param $data |
||
| 430 | */ |
||
| 431 | 13 | private function CreateRootObjects($data) |
|
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * Loops over the relationships of the provided data and formats it |
||
| 443 | * |
||
| 444 | * @param $data |
||
| 445 | * @param $relationship |
||
| 446 | * @param $nestedDepth |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | 7 | private function FillRelationshipAsCollection($data, $relationship, $nestedDepth) |
|
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * @param $data |
||
| 462 | * @param $relationship |
||
| 463 | * @param $key |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | 10 | protected function FillRelationshipAsSingleResource($data, $relationship, $key) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @param $includeKey |
||
| 486 | * @param $relationships |
||
| 487 | * @param $includeObject |
||
| 488 | * @param $key |
||
| 489 | * |
||
| 490 | * @return array |
||
| 491 | */ |
||
| 492 | 15 | protected function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * @param $includeKey |
||
| 522 | * @param $relationships |
||
| 523 | * |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | 15 | protected function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @param $includeObject |
||
| 538 | * @param $relationship |
||
| 539 | * |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | 7 | protected function addIncludedDataToRelationship($includeObject, $relationship) |
|
| 553 | } |
||
| 554 |