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 DataParser 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 DataParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class DataParser implements DataParserInterface |
||
34 | { |
||
35 | const ERROR_CODE = 'ee2c1d49-ba40-4077-a6bb-b06baceb3e97'; |
||
36 | |||
37 | /** |
||
38 | * Current path |
||
39 | * |
||
40 | * @var \SplStack |
||
41 | */ |
||
42 | protected $path; |
||
43 | |||
44 | /** |
||
45 | * @var Context |
||
46 | */ |
||
47 | protected $context; |
||
48 | |||
49 | /** |
||
50 | * Resource decoders factory |
||
51 | * |
||
52 | * @var MetadataFactoryInterface |
||
53 | */ |
||
54 | protected $factory; |
||
55 | |||
56 | /** |
||
57 | * @var PropertyAccessor |
||
58 | */ |
||
59 | protected $accessor; |
||
60 | |||
61 | /** |
||
62 | * @var CallbackResolverInterface |
||
63 | */ |
||
64 | protected $callbackResolver; |
||
65 | |||
66 | /** |
||
67 | * Serialization groups |
||
68 | * |
||
69 | * @var string[] |
||
70 | */ |
||
71 | protected $serializationGroups = ['Default']; |
||
72 | |||
73 | /** |
||
74 | * Constructor |
||
75 | * |
||
76 | * @param MetadataFactoryInterface $factory |
||
77 | * @param CallbackResolverInterface $callbackResolver |
||
78 | */ |
||
79 | 21 | public function __construct(MetadataFactoryInterface $factory, CallbackResolverInterface $callbackResolver) |
|
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 16 | public function setPath($path) |
|
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | */ |
||
104 | 16 | public function restorePath() |
|
110 | |||
111 | /** |
||
112 | * @return string[] |
||
113 | */ |
||
114 | 3 | public function getSerializationGroups() |
|
118 | |||
119 | /** |
||
120 | * @param string[] $serializationGroups |
||
121 | * @return $this |
||
122 | */ |
||
123 | 3 | public function setSerializationGroups(array $serializationGroups) |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 4 | public function getPath() |
|
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | 17 | public function hasValue($data, $path) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 16 | public function getValue($data, $path) |
|
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | 10 | public function parseString($data, $path) |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 3 | public function parseInt($data, $path) |
|
190 | |||
191 | /** |
||
192 | * @inheritdoc |
||
193 | */ |
||
194 | 2 | public function parseFloat($data, $path) |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 2 | public function parseRaw($data, $path) |
|
215 | |||
216 | /** |
||
217 | * @inheritdoc |
||
218 | */ |
||
219 | 4 | public function parseCallback($data, $path, $callback) |
|
232 | |||
233 | /** |
||
234 | * @inheritdoc |
||
235 | */ |
||
236 | 2 | public function parseBool($data, $path) |
|
261 | |||
262 | /** |
||
263 | * @inheritdoc |
||
264 | */ |
||
265 | 2 | public function parseDateTime($data, $path, $format = 'Y-m-d') |
|
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 8 | public function parseArray($data, $path, \Closure $itemsParser) |
|
321 | |||
322 | /** |
||
323 | * Parse data object value at specified path as object of specified class |
||
324 | * |
||
325 | * @param array|object $data |
||
326 | * @param string $path |
||
327 | * @param string $objType |
||
328 | * @return null |
||
329 | */ |
||
330 | 1 | public function parseObject($data, $path, $objType) |
|
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | 6 | public function parseResource($data, $path, $resType, $loader = null) |
|
422 | |||
423 | /** |
||
424 | * @inheritdoc |
||
425 | */ |
||
426 | 5 | public function parseDocument($data, $docType) |
|
427 | { |
||
428 | try { |
||
429 | 5 | $this->initPathStack(); |
|
430 | 5 | $this->initContext(); |
|
431 | |||
432 | 5 | $this->parseErrors($data); |
|
433 | |||
434 | 4 | $metadata = $this->factory->getMetadataFor($docType); |
|
435 | 4 | if (!$metadata instanceof DocumentMetadataInterface) { |
|
436 | 1 | throw new \InvalidArgumentException(sprintf("Failed to parse %s as JSON API document", $docType)); |
|
437 | } |
||
438 | |||
439 | /* @var $metadata \Reva2\JsonApi\Contracts\Decoders\Mapping\DocumentMetadataInterface */ |
||
440 | |||
441 | 3 | $docClass = $metadata->getClassName(); |
|
442 | 3 | $doc = new $docClass(); |
|
443 | |||
444 | 3 | $this->parseLinkedResources($data); |
|
445 | |||
446 | 3 | $this->parseProperty($data, $doc, $metadata->getContentMetadata()); |
|
447 | |||
448 | $docMetadata = $metadata->getMetadata(); |
||
449 | if ($docMetadata !== null) { |
||
450 | $this->parseProperty($data, $doc, $metadata->getMetadata()); |
||
451 | } |
||
452 | |||
453 | return $doc; |
||
454 | 5 | } catch (JsonApiException $e) { |
|
455 | 1 | throw $e; |
|
456 | 4 | } catch (\Exception $e) { |
|
457 | 4 | throw $this->convertToApiException($e, 'document'); |
|
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @inheritdoc |
||
463 | */ |
||
464 | 3 | public function parseQueryParams($data, $paramsType) |
|
485 | |||
486 | /** |
||
487 | * Prepare path segment |
||
488 | * |
||
489 | * @param string $path |
||
490 | * @return string |
||
491 | */ |
||
492 | 16 | protected function preparePathSegment($path) |
|
496 | |||
497 | /** |
||
498 | * Initialize stack that store current path |
||
499 | */ |
||
500 | 21 | protected function initPathStack() |
|
504 | |||
505 | /** |
||
506 | * Initialize decoder context |
||
507 | */ |
||
508 | 21 | protected function initContext() |
|
512 | |||
513 | /** |
||
514 | * Parse numeric value |
||
515 | * |
||
516 | * @param mixed $data |
||
517 | * @param string $path |
||
518 | * @param string $type |
||
519 | * @return float|int|null |
||
520 | */ |
||
521 | 4 | protected function parseNumeric($data, $path, $type) |
|
545 | |||
546 | /** |
||
547 | * Parse property of specified object |
||
548 | * |
||
549 | * @param object|array $data |
||
550 | * @param object $obj |
||
551 | * @param PropertyMetadataInterface $metadata |
||
552 | * @param string|null $path |
||
553 | */ |
||
554 | 8 | private function parseProperty($data, $obj, PropertyMetadataInterface $metadata, $path = null) |
|
580 | |||
581 | /** |
||
582 | * Parse value of specified property |
||
583 | * |
||
584 | * @param object|array $data |
||
585 | * @param string $path |
||
586 | * @param PropertyMetadataInterface $metadata |
||
587 | * @return mixed|null |
||
588 | */ |
||
589 | 7 | private function parsePropertyValue($data, $path, PropertyMetadataInterface $metadata) |
|
619 | |||
620 | /** |
||
621 | * Parse value as JSON API resource or object |
||
622 | * |
||
623 | * @param object|array $data |
||
624 | * @param string $path |
||
625 | * @param string $objClass |
||
626 | * @param PropertyMetadataInterface $propMetadata |
||
627 | * @return mixed|null |
||
628 | */ |
||
629 | 5 | public function parseResourceOrObject($data, $path, $objClass, PropertyMetadataInterface $propMetadata) |
|
645 | |||
646 | /** |
||
647 | * Parse value that contains JSON API object |
||
648 | * |
||
649 | * @param object|array $data |
||
650 | * @param string $objType |
||
651 | * @return mixed |
||
652 | */ |
||
653 | 4 | public function parseObjectValue($data, $objType) |
|
675 | |||
676 | /** |
||
677 | * Parse value that contains array |
||
678 | * |
||
679 | * @param object|array $data |
||
680 | * @param string $path |
||
681 | * @param array $params |
||
682 | * @param PropertyMetadataInterface $propMetadata |
||
683 | * @return array|null |
||
684 | */ |
||
685 | 3 | public function parseArrayValue($data, $path, array $params, PropertyMetadataInterface $propMetadata) |
|
686 | { |
||
687 | 3 | $type = $params[0]; |
|
688 | 3 | $typeParams = $params[1]; |
|
689 | |||
690 | switch ($type) { |
||
691 | 3 | case 'scalar': |
|
692 | 1 | return $this->parseArray( |
|
693 | 1 | $data, |
|
694 | 1 | $path, |
|
695 | 1 | function ($data, $path, DataParser $parser) use ($typeParams) { |
|
696 | 1 | return $parser->parseScalarValue($data, $path, $typeParams); |
|
697 | 1 | } |
|
698 | ); |
||
699 | |||
700 | 3 | case 'datetime': |
|
701 | 1 | $format = (!empty($typeParams)) ? $typeParams : 'Y-m-d'; |
|
702 | 1 | return $this->parseArray( |
|
703 | 1 | $data, |
|
704 | 1 | $path, |
|
705 | 1 | function ($data, $path, DataParser $parser) use ($format) { |
|
706 | 1 | return $parser->parseDateTime($data, $path, $format); |
|
707 | 1 | } |
|
708 | ); |
||
709 | |||
710 | 3 | case 'object': |
|
711 | 3 | return $this->parseArray( |
|
712 | 3 | $data, |
|
713 | 3 | $path, |
|
714 | 3 | function ($data, $path, DataParser $parser) use ($typeParams, $propMetadata) { |
|
715 | 3 | return $parser->parseResourceOrObject($data, $path, $typeParams, $propMetadata); |
|
716 | 3 | } |
|
717 | ); |
||
718 | |||
719 | 1 | case 'array': |
|
720 | 1 | return $this->parseArray( |
|
721 | 1 | $data, |
|
722 | 1 | $path, |
|
723 | 1 | function ($data, $path, DataParser $parser) use ($typeParams, $propMetadata) { |
|
724 | 1 | return $parser->parseArrayValue($data, $path, $typeParams, $propMetadata); |
|
725 | 1 | } |
|
726 | ); |
||
727 | |||
728 | 1 | case 'raw': |
|
729 | 1 | return $this->parseArray( |
|
730 | 1 | $data, |
|
731 | 1 | $path, |
|
732 | 1 | function ($data, $path, DataParser $parser) { |
|
733 | 1 | return $parser->parseRaw($data, $path); |
|
734 | 1 | } |
|
735 | ); |
||
736 | |||
737 | default: |
||
738 | throw new \InvalidArgumentException(sprintf( |
||
739 | "Unsupported array item type '%s' specified", |
||
740 | $type |
||
741 | )); |
||
742 | } |
||
743 | } |
||
744 | |||
745 | /** |
||
746 | * Parse scalar value |
||
747 | * |
||
748 | * @param object|array $data |
||
749 | * @param string $path |
||
750 | * @param string $type |
||
751 | * @return bool|float|int|null|string |
||
752 | */ |
||
753 | 7 | public function parseScalarValue($data, $path, $type) |
|
754 | { |
||
755 | switch ($type) { |
||
756 | 7 | case 'string': |
|
757 | 6 | return $this->parseString($data, $path); |
|
758 | |||
759 | 2 | case 'bool': |
|
760 | 2 | case 'boolean': |
|
761 | 1 | return $this->parseBool($data, $path); |
|
762 | |||
763 | 2 | case 'int': |
|
764 | 2 | case 'integer': |
|
765 | 2 | return $this->parseInt($data, $path); |
|
766 | |||
767 | 1 | case 'float': |
|
768 | 1 | case 'double': |
|
769 | 1 | return $this->parseFloat($data, $path); |
|
770 | |||
771 | default: |
||
772 | throw new \InvalidArgumentException(sprintf("Unsupported scalar type '%s' specified", $type)); |
||
773 | } |
||
774 | } |
||
775 | |||
776 | /** |
||
777 | * Convert any exception to JSON API exception |
||
778 | * |
||
779 | * @param \Exception $e |
||
780 | * @param string $objType |
||
781 | * @return JsonApiException |
||
782 | */ |
||
783 | 4 | private function convertToApiException(\Exception $e, $objType) |
|
784 | { |
||
785 | 4 | $status = $e->getCode(); |
|
786 | 4 | $message = 'Failed to parse request'; |
|
787 | 4 | if (empty($status)) { |
|
788 | 1 | $message = 'Internal server error'; |
|
789 | 1 | $status = 500; |
|
790 | } |
||
791 | |||
792 | 4 | $source = null; |
|
793 | switch ($objType) { |
||
794 | 4 | case 'document': |
|
795 | 4 | $source = ['pointer' => $this->getPath()]; |
|
796 | 4 | break; |
|
797 | |||
798 | case 'query': |
||
799 | $source = ['parameter' => $this->getPath()]; |
||
800 | break; |
||
801 | } |
||
802 | |||
803 | 4 | $error = new Error(rand(), null, $status, self::ERROR_CODE, $message, $e->getMessage(), $source); |
|
804 | |||
805 | 4 | return new JsonApiException($error, $status, $e); |
|
806 | } |
||
807 | |||
808 | /** |
||
809 | * Returns appropriate discriminator class for specified data |
||
810 | * |
||
811 | * @param ClassMetadataInterface $metadata |
||
812 | * @param array|object $data |
||
813 | * @return string|null |
||
814 | */ |
||
815 | 9 | private function getClassByDiscriminator(ClassMetadataInterface $metadata, $data) |
|
830 | |||
831 | /** |
||
832 | * Check if specified property should be excluded |
||
833 | * |
||
834 | * @param PropertyMetadataInterface $metadata |
||
835 | * @return bool |
||
836 | */ |
||
837 | 8 | private function isExcludedProperty(PropertyMetadataInterface $metadata) |
|
848 | |||
849 | /** |
||
850 | * |
||
851 | * @param mixed $data |
||
852 | */ |
||
853 | 3 | private function parseLinkedResources($data) |
|
882 | |||
883 | /** |
||
884 | * Parse specified relationship data |
||
885 | * |
||
886 | * @param mixed $data |
||
887 | * @param mixed $pathValue |
||
888 | * @param PropertyMetadataInterface $relationship |
||
889 | * @return void |
||
890 | */ |
||
891 | 5 | private function parseRelationship($data, $pathValue, PropertyMetadataInterface $relationship) |
|
930 | |||
931 | /** |
||
932 | * Parse data for relationship that contains array of resources |
||
933 | * |
||
934 | * @param mixed $data |
||
935 | * @param mixed $pathValue |
||
936 | * @param PropertyMetadataInterface $relationship |
||
937 | */ |
||
938 | private function parseArrayRelationship($data, $pathValue, PropertyMetadataInterface $relationship) |
||
986 | |||
987 | /** |
||
988 | * Sets property value using metadata |
||
989 | * |
||
990 | * @param mixed $obj |
||
991 | * @param mixed $value |
||
992 | * @param PropertyMetadataInterface $metadata |
||
993 | */ |
||
994 | 8 | private function setProperty($obj, $value, PropertyMetadataInterface $metadata) |
|
1004 | |||
1005 | /** |
||
1006 | * Parse errors from JSON API document |
||
1007 | * |
||
1008 | * @param object $data |
||
1009 | */ |
||
1010 | 5 | private function parseErrors($data) |
|
1038 | } |
||
1039 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.