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 | /** @var string|null */ |
||
| 21 | protected $baseUrl; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected $rootObjects; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * JsonApiSerializer constructor. |
||
| 28 | * |
||
| 29 | * @param string $baseUrl |
||
| 30 | */ |
||
| 31 | 37 | public function __construct(string $baseUrl = null) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Serialize a collection. |
||
| 39 | * |
||
| 40 | * @param string $resourceKey |
||
| 41 | * @param array $data |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | 24 | public function collection($resourceKey, array $data): array |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Serialize an item. |
||
| 58 | * |
||
| 59 | * @param string $resourceKey |
||
| 60 | * @param array $data |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 37 | public function item($resourceKey, array $data): array |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Serialize the paginator. |
||
| 107 | * |
||
| 108 | * @param PaginatorInterface $paginator |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 3 | public function paginator(PaginatorInterface $paginator): array |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Serialize the meta. |
||
| 145 | * |
||
| 146 | * @param array $meta |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 36 | public function meta(array $meta): array |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Serialize the meta. |
||
| 170 | * |
||
| 171 | * @param array $meta |
||
|
|
|||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | 2 | public function null(): array |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Serialize the included data. |
||
| 184 | * |
||
| 185 | * @param ResourceInterface $resource |
||
| 186 | * @param array $data |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 36 | public function includedData(ResourceInterface $resource, array $data): array |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Indicates if includes should be side-loaded. |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 37 | public function sideloadIncludes(): bool |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $data |
||
| 220 | * @param array $includedData |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 36 | public function injectData($data, $includedData): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Hook to manipulate the final sideloaded includes. |
||
| 237 | * The JSON API specification does not allow the root object to be included |
||
| 238 | * into the sideloaded `included`-array. We have to make sure it is |
||
| 239 | * filtered out, in case some object links to the root object in a |
||
| 240 | * relationship. |
||
| 241 | * |
||
| 242 | * @param array $includedData |
||
| 243 | * @param array $data |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | 36 | public function filterIncludes($includedData, $data): array |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the mandatory fields for the serializer |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | 4 | public function getMandatoryFields() : array |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Filter function to delete root objects from array. |
||
| 277 | * |
||
| 278 | * @param array $object |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 18 | protected function filterRootObject($object): bool |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Set the root objects of the JSON API tree. |
||
| 289 | * |
||
| 290 | * @param array $objects |
||
| 291 | */ |
||
| 292 | 18 | protected function setRootObjects(array $objects = []): void |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Determines whether an object is a root object of the JSON API tree. |
||
| 301 | * |
||
| 302 | * @param array $object |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 18 | protected function isRootObject($object): bool |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param array|null $data |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 29 | View Code Duplication | protected function isCollection($data): bool |
| 326 | |||
| 327 | /** |
||
| 328 | * @param array|null $data |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | 22 | View Code Duplication | protected function isNull($data): bool |
| 340 | |||
| 341 | /** |
||
| 342 | * @param array|null $data |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | 21 | View Code Duplication | protected function isEmpty($data): bool |
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $data |
||
| 357 | * @param array $relationships |
||
| 358 | * |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | 22 | protected function fillRelationships($data, $relationships): array |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param array $includedData |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | 36 | protected function parseRelationships($includedData): array |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param array $data |
||
| 399 | * |
||
| 400 | * @return integer |
||
| 401 | */ |
||
| 402 | 37 | protected function getIdFromData(array $data): int |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Keep all sideloaded inclusion data on the top level. |
||
| 414 | * |
||
| 415 | * @param array $data |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | 36 | protected function pullOutNestedIncludedData(array $data): array |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Whether or not the serializer should include `links` for resource objects. |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | 36 | protected function shouldIncludeLinks(): bool |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Check if the objects are part of a collection or not |
||
| 447 | * |
||
| 448 | * @param array $includeObject |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 20 | private function createIncludeObjects($includeObject): array |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Sets the RootObjects, either as collection or not. |
||
| 467 | * |
||
| 468 | * @param array $data |
||
| 469 | */ |
||
| 470 | 18 | private function createRootObjects(array $data): void |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Loops over the relationships of the provided data and formats it |
||
| 481 | * |
||
| 482 | * @param array $data |
||
| 483 | * @param array $relationship |
||
| 484 | * @param string $key |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | 9 | private function fillRelationshipAsCollection($data, $relationship, $key): array |
|
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @param array $data |
||
| 500 | * @param array $relationship |
||
| 501 | * @param string $key |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | 15 | private function fillRelationshipAsSingleResource($data, $relationship, $key): array |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $includeKey |
||
| 514 | * @param array $relationships |
||
| 515 | * @param array $includeObject |
||
| 516 | * @param string $key |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | 22 | private function buildRelationships(string $includeKey, array $relationships, array $includeObject, string $key): array |
|
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $includeKey |
||
| 549 | * @param array $relationships |
||
| 550 | * |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | 22 | private function addIncludeKeyToRelationsIfNotSet(string $includeKey, array $relationships): array |
|
| 562 | |||
| 563 | /** |
||
| 564 | * @param array $includeObject |
||
| 565 | * @param array $relationship |
||
| 566 | * |
||
| 567 | * @return array |
||
| 568 | */ |
||
| 569 | 11 | private function addIncludedDataToRelationship(array $includeObject, array $relationship) : array |
|
| 580 | |||
| 581 | /** |
||
| 582 | * {@inheritdoc} |
||
| 583 | */ |
||
| 584 | 35 | public function injectAvailableIncludeData($data, $availableIncludes): array |
|
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * Adds links for all available includes to a single resource. |
||
| 609 | * |
||
| 610 | * @param array $resource The resource to add relationship links to |
||
| 611 | * @param string $relationshipKey The resource key of the relationship |
||
| 612 | */ |
||
| 613 | 11 | private function addRelationshipLinks(array $resource, string $relationshipKey): array |
|
| 631 | |||
| 632 | /** |
||
| 633 | * @param $includeObjects |
||
| 634 | * @param $linkedIds |
||
| 635 | * @param $serializedData |
||
| 636 | * |
||
| 637 | * @return array |
||
| 638 | */ |
||
| 639 | 20 | private function serializeIncludedObjectsWithCacheKey(array $includeObjects, array $linkedIds, array $serializedData): array |
|
| 652 | |||
| 653 | /** |
||
| 654 | * @param array $resource |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | 36 | private function areResourceLinksSet(array $resource): bool |
|
| 662 | |||
| 663 | /** |
||
| 664 | * @param array $resource |
||
| 665 | * |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | 36 | private function isResourceMetaSet(array $resource): bool |
|
| 672 | |||
| 673 | /** |
||
| 674 | * @param array $resource |
||
| 675 | * |
||
| 676 | * @return bool |
||
| 677 | */ |
||
| 678 | 36 | private function isDataAttributesEmpty(array $resource) : bool |
|
| 682 | } |
||
| 683 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.