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 | /** |
||
| 24 | * JsonApiSerializer constructor. |
||
| 25 | * |
||
| 26 | * @param string $baseUrl |
||
| 27 | */ |
||
| 28 | 29 | public function __construct($baseUrl = null) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Serialize a collection. |
||
| 36 | * |
||
| 37 | * @param string $resourceKey |
||
| 38 | * @param array $data |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | 21 | public function collection($resourceKey, array $data) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Serialize an item. |
||
| 55 | * |
||
| 56 | * @param string $resourceKey |
||
| 57 | * @param array $data |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | 29 | public function item($resourceKey, array $data) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Serialize the paginator. |
||
| 86 | * |
||
| 87 | * @param PaginatorInterface $paginator |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | 3 | public function paginator(PaginatorInterface $paginator) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Serialize the meta. |
||
| 124 | * |
||
| 125 | * @param array $meta |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | 28 | public function meta(array $meta) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | 2 | public function null() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Serialize the included data. |
||
| 157 | * |
||
| 158 | * @param ResourceInterface $resource |
||
| 159 | * @param array $data |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 28 | public function includedData(ResourceInterface $resource, array $data) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Indicates if includes should be side-loaded. |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 29 | public function sideloadIncludes() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param array $data |
||
| 202 | * @param array $includedData |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | 28 | public function injectData($data, $includedData) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Hook to manipulate the final sideloaded includes. |
||
| 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 | 28 | public function filterIncludes($includedData, $data) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get the mandatory fields for the serializer |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | 4 | public function getMandatoryFields() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Filter function to delete root objects from array. |
||
| 259 | * |
||
| 260 | * @param array $object |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | 15 | protected function filterRootObject($object) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Set the root objects of the JSON API tree. |
||
| 271 | * |
||
| 272 | * @param array $objects |
||
| 273 | */ |
||
| 274 | protected function setRootObjects(array $objects = []) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Determines whether an object is a root object of the JSON API tree. |
||
| 283 | * |
||
| 284 | * @param array $object |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | 15 | protected function isRootObject($object) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $data |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 19 | protected function isCollection($data) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param array $data |
||
| 307 | * |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | 19 | protected function isNull($data) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $data |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 18 | protected function isEmpty($data) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param array $data |
||
| 327 | * @param array $relationships |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | 19 | protected function fillRelationships($data, $relationships) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $includedData |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | 28 | protected function parseRelationships($includedData) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param array $data |
||
| 366 | * |
||
| 367 | * @return integer |
||
| 368 | */ |
||
| 369 | 29 | protected function getIdFromData(array $data) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Keep all sideloaded inclusion data on the top level. |
||
| 381 | * |
||
| 382 | * @param array $data |
||
| 383 | * |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | 28 | protected function pullOutNestedIncludedData(array $data) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Whether or not the serializer should include `links` for resource objects. |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | 28 | protected function shouldIncludeLinks() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Check if the objects are part of a collection or not |
||
| 423 | * |
||
| 424 | * @param $includeObject |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | 17 | private function createIncludeObjects($includeObject) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Sets the RootObjects, either as collection or not. |
||
| 443 | * |
||
| 444 | * @param $data |
||
| 445 | */ |
||
| 446 | 15 | private function createRootObjects($data) |
|
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Loops over the relationships of the provided data and formats it |
||
| 458 | * |
||
| 459 | * @param $data |
||
| 460 | * @param $relationship |
||
| 461 | * @param $nestedDepth |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | 7 | private function fillRelationshipAsCollection($data, $relationship, $nestedDepth) |
|
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * @param $data |
||
| 477 | * @param $relationship |
||
| 478 | * @param $key |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | 14 | private function FillRelationshipAsSingleResource($data, $relationship, $key) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @param $includeKey |
||
| 501 | * @param $relationships |
||
| 502 | * @param $includeObject |
||
| 503 | * @param $key |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | 19 | private function buildRelationships($includeKey, $relationships, $includeObject, $key) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @param $includeKey |
||
| 537 | * @param $relationships |
||
| 538 | * |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | 19 | private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param $includeObject |
||
| 553 | * @param $relationship |
||
| 554 | * |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | 10 | private function addIncludedDataToRelationship($includeObject, $relationship) |
|
| 568 | } |
||
| 569 |