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 | 36 | public function meta(array $meta): array |
|
160 | |||
161 | /** |
||
162 | * Serialize the meta. |
||
163 | * |
||
164 | * @param array $meta |
||
|
|||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 2 | public function null(): array |
|
174 | |||
175 | /** |
||
176 | * Serialize the included data. |
||
177 | * |
||
178 | * @param ResourceInterface $resource |
||
179 | * @param array $data |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | 36 | public function includedData(ResourceInterface $resource, array $data): array |
|
200 | |||
201 | /** |
||
202 | * Indicates if includes should be side-loaded. |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | 37 | public function sideloadIncludes(): bool |
|
210 | |||
211 | /** |
||
212 | * @param array $data |
||
213 | * @param array $includedData |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | 36 | public function injectData($data, $includedData): array |
|
227 | |||
228 | /** |
||
229 | * Hook to manipulate the final sideloaded includes. |
||
230 | * The JSON API specification does not allow the root object to be included |
||
231 | * into the sideloaded `included`-array. We have to make sure it is |
||
232 | * filtered out, in case some object links to the root object in a |
||
233 | * relationship. |
||
234 | * |
||
235 | * @param array $includedData |
||
236 | * @param array $data |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | 36 | public function filterIncludes($includedData, $data): array |
|
257 | |||
258 | /** |
||
259 | * Get the mandatory fields for the serializer |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | 4 | public function getMandatoryFields() : array |
|
267 | |||
268 | /** |
||
269 | * Filter function to delete root objects from array. |
||
270 | * |
||
271 | * @param array $object |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | 18 | protected function filterRootObject($object): bool |
|
279 | |||
280 | /** |
||
281 | * Set the root objects of the JSON API tree. |
||
282 | * |
||
283 | * @param array $objects |
||
284 | */ |
||
285 | 18 | protected function setRootObjects(array $objects = []): void |
|
291 | |||
292 | /** |
||
293 | * Determines whether an object is a root object of the JSON API tree. |
||
294 | * |
||
295 | * @param array $object |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | 18 | protected function isRootObject($object): bool |
|
304 | |||
305 | /** |
||
306 | * @param array|null $data |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | 29 | View Code Duplication | protected function isCollection($data): bool |
319 | |||
320 | /** |
||
321 | * @param array|null $data |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 22 | View Code Duplication | protected function isNull($data): bool |
333 | |||
334 | /** |
||
335 | * @param array|null $data |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | 21 | View Code Duplication | protected function isEmpty($data): bool |
347 | |||
348 | /** |
||
349 | * @param array $data |
||
350 | * @param array $relationships |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | 22 | protected function fillRelationships($data, $relationships): array |
|
368 | |||
369 | /** |
||
370 | * @param array $includedData |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | 36 | protected function parseRelationships($includedData): array |
|
389 | |||
390 | /** |
||
391 | * @param array $data |
||
392 | * |
||
393 | * @return integer |
||
394 | */ |
||
395 | 37 | protected function getIdFromData(array $data): int |
|
404 | |||
405 | /** |
||
406 | * Keep all sideloaded inclusion data on the top level. |
||
407 | * |
||
408 | * @param array $data |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | 36 | protected function pullOutNestedIncludedData(array $data): array |
|
427 | |||
428 | /** |
||
429 | * Whether or not the serializer should include `links` for resource objects. |
||
430 | * |
||
431 | * @return bool |
||
432 | */ |
||
433 | 36 | protected function shouldIncludeLinks(): bool |
|
437 | |||
438 | /** |
||
439 | * Check if the objects are part of a collection or not |
||
440 | * |
||
441 | * @param array $includeObject |
||
442 | * |
||
443 | * @return array |
||
444 | */ |
||
445 | 20 | private function createIncludeObjects($includeObject): array |
|
457 | |||
458 | /** |
||
459 | * Sets the RootObjects, either as collection or not. |
||
460 | * |
||
461 | * @param array $data |
||
462 | */ |
||
463 | 18 | private function createRootObjects(array $data): void |
|
471 | |||
472 | /** |
||
473 | * Loops over the relationships of the provided data and formats it |
||
474 | * |
||
475 | * @param $data |
||
476 | * @param $relationship |
||
477 | * @param $key |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | 9 | private function fillRelationshipAsCollection($data, $relationship, $key): array |
|
489 | |||
490 | |||
491 | /** |
||
492 | * @param $data |
||
493 | * @param $relationship |
||
494 | * @param $key |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | 15 | private function fillRelationshipAsSingleResource($data, $relationship, $key): array |
|
504 | |||
505 | /** |
||
506 | * @param $includeKey |
||
507 | * @param $relationships |
||
508 | * @param $includeObject |
||
509 | * @param $key |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | 22 | private function buildRelationships(string $includeKey, array $relationships, array $includeObject, string $key): array |
|
539 | |||
540 | /** |
||
541 | * @param $includeKey |
||
542 | * @param $relationships |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | 22 | private function addIncludeKeyToRelationsIfNotSet(string $includeKey, array $relationships): array |
|
555 | |||
556 | /** |
||
557 | * @param array $includeObject |
||
558 | * @param array $relationship |
||
559 | * |
||
560 | * @return array |
||
561 | */ |
||
562 | 11 | private function addIncludedDataToRelationship(array $includeObject, array $relationship) : array |
|
573 | |||
574 | /** |
||
575 | * {@inheritdoc} |
||
576 | */ |
||
577 | 35 | public function injectAvailableIncludeData($data, $availableIncludes): array |
|
598 | |||
599 | |||
600 | /** |
||
601 | * Adds links for all available includes to a single resource. |
||
602 | * |
||
603 | * @param array $resource The resource to add relationship links to |
||
604 | * @param string $relationshipKey The resource key of the relationship |
||
605 | */ |
||
606 | 11 | private function addRelationshipLinks(array $resource, string $relationshipKey): array |
|
624 | |||
625 | /** |
||
626 | * @param $includeObjects |
||
627 | * @param $linkedIds |
||
628 | * @param $serializedData |
||
629 | * |
||
630 | * @return array |
||
631 | */ |
||
632 | 20 | private function serializeIncludedObjectsWithCacheKey(array $includeObjects, array $linkedIds, array $serializedData): array |
|
645 | |||
646 | /** |
||
647 | * @param array $resource |
||
648 | * |
||
649 | * @return bool |
||
650 | */ |
||
651 | 36 | private function areResourceLinksSet(array $resource): bool |
|
655 | |||
656 | /** |
||
657 | * @param array $resource |
||
658 | * |
||
659 | * @return bool |
||
660 | */ |
||
661 | 36 | private function isResourceMetaSet(array $resource): bool |
|
665 | |||
666 | /** |
||
667 | * @param array $resource |
||
668 | * |
||
669 | * @return bool |
||
670 | */ |
||
671 | 36 | private function isDataAttributesEmpty(array $resource) : bool |
|
675 | } |
||
676 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.