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) |
|
| 125 | { |
||
| 126 | 24 | if (empty($meta)) { |
|
| 127 | 19 | return []; |
|
| 128 | } |
||
| 129 | |||
| 130 | 5 | $result['meta'] = $meta; |
|
| 131 | |||
| 132 | 5 | if (array_key_exists('pagination', $result['meta'])) { |
|
| 133 | 3 | $result['links'] = $result['meta']['pagination']['links']; |
|
| 134 | 3 | unset($result['meta']['pagination']['links']); |
|
| 135 | 3 | } |
|
| 136 | |||
| 137 | 5 | return $result; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | 2 | public function null() |
|
| 144 | { |
||
| 145 | return [ |
||
| 146 | 2 | 'data' => null, |
|
| 147 | 2 | ]; |
|
| 148 | } |
||
| 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) |
|
| 159 | { |
||
| 160 | 24 | list($serializedData, $linkedIds) = $this->pullOutNestedIncludedData($data); |
|
| 161 | |||
| 162 | 24 | foreach ($data as $value) { |
|
| 163 | 24 | foreach ($value as $includeObject) { |
|
| 164 | 15 | if ($this->isNull($includeObject) || $this->isEmpty($includeObject)) { |
|
| 165 | 4 | continue; |
|
| 166 | } |
||
| 167 | 13 | if ($this->isCollection($includeObject)) { |
|
| 168 | 7 | $includeObjects = $includeObject['data']; |
|
| 169 | 7 | } |
|
| 170 | else { |
||
| 171 | 9 | $includeObjects = [$includeObject['data']]; |
|
| 172 | } |
||
| 173 | |||
| 174 | 13 | View Code Duplication | foreach ($includeObjects as $object) { |
| 175 | 13 | $includeType = $object['type']; |
|
| 176 | 13 | $includeId = $object['id']; |
|
| 177 | 13 | $cacheKey = "$includeType:$includeId"; |
|
| 178 | 13 | if (!array_key_exists($cacheKey, $linkedIds)) { |
|
| 179 | 13 | $serializedData[] = $object; |
|
| 180 | 13 | $linkedIds[$cacheKey] = $object; |
|
| 181 | 13 | } |
|
| 182 | 13 | } |
|
| 183 | 24 | } |
|
| 184 | 24 | } |
|
| 185 | |||
| 186 | 24 | return empty($serializedData) ? [] : ['included' => $serializedData]; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Indicates if includes should be side-loaded. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 25 | public function sideloadIncludes() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $data |
||
| 201 | * @param array $includedData |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | 24 | public function injectData($data, $includedData) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Hook to manipulate the final sideloaded includes. |
||
| 218 | * |
||
| 219 | * The JSON API specification does not allow the root object to be included |
||
| 220 | * into the sideloaded `included`-array. We have to make sure it is |
||
| 221 | * filtered out, in case some object links to the root object in a |
||
| 222 | * relationship. |
||
| 223 | * |
||
| 224 | * @param array $includedData |
||
| 225 | * @param array $data |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 24 | public function filterIncludes($includedData, $data) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get the mandatory fields for the serializer |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function getMandatoryFields() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Filter function to delete root objects from array. |
||
| 263 | * |
||
| 264 | * @param array $object |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 13 | protected function filterRootObject($object) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Set the root objects of the JSON API tree. |
||
| 275 | * |
||
| 276 | * @param array $objects |
||
| 277 | */ |
||
| 278 | protected function setRootObjects(array $objects = []) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Determines whether an object is a root object of the JSON API tree. |
||
| 287 | * |
||
| 288 | * @param array $object |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | 13 | protected function isRootObject($object) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $data |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | 15 | protected function isCollection($data) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param array $data |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | 15 | protected function isNull($data) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param array $data |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | 14 | protected function isEmpty($data) { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param array $data |
||
| 330 | * @param array $relationships |
||
| 331 | * |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | 15 | protected function fillRelationships($data, $relationships) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $includedData |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | 24 | protected function parseRelationships($includedData) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param array $data |
||
| 413 | * |
||
| 414 | * @return mixed |
||
| 415 | */ |
||
| 416 | 25 | protected function getIdFromData(array $data) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Keep all sideloaded inclusion data on the top level. |
||
| 428 | * |
||
| 429 | * @param array $data |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | 24 | protected function pullOutNestedIncludedData(array $data) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Whether or not the serializer should include `links` for resource objects. |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | 24 | protected function shouldIncludeLinks() |
|
| 467 | } |
||
| 468 |