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:
1 | <?php |
||
20 | class RelationshipExtension implements ExtensionInterface |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | 6 | View Code Duplication | public function hydrate($object, $source, DocumentHydrator $hydrator) |
|
|||
26 | { |
||
27 | 6 | if (! $object instanceof RelationshipsAwareInterface) { |
|
28 | throw new InvalidDocumentException(sprintf( |
||
29 | 'Given instance of "%s" does not implements "%s"', |
||
30 | get_class($object), |
||
31 | RelationshipsAwareInterface::class |
||
32 | )); |
||
33 | } |
||
34 | |||
35 | 6 | foreach ($source as $name => $content) |
|
36 | { |
||
37 | 6 | $relationship = $this->createRelationship($content, $hydrator); |
|
38 | |||
39 | 4 | $object->setRelationship($name, $relationship); |
|
40 | } |
||
41 | 4 | } |
|
42 | |||
43 | /** |
||
44 | * Create relationship |
||
45 | * |
||
46 | * @param mixed $source |
||
47 | * @param DocumentHydrator $hydrator |
||
48 | * @return AbstractRelationship |
||
49 | */ |
||
50 | 6 | public function createRelationship($source, DocumentHydrator $hydrator): AbstractRelationship |
|
66 | |||
67 | /** |
||
68 | * Process relationship contains no data |
||
69 | * |
||
70 | * @param mixed $source |
||
71 | * @param DocumentHydrator $hydrator |
||
72 | * @return NoDataRelationship |
||
73 | */ |
||
74 | 1 | protected function processNoDataRelationship($source, DocumentHydrator $hydrator): NoDataRelationship |
|
82 | |||
83 | /** |
||
84 | * Process relationship with single resource identifier |
||
85 | * |
||
86 | * @param mixed $source |
||
87 | * @param DocumentHydrator $hydrator |
||
88 | * @return SingleIdentifierRelationship |
||
89 | */ |
||
90 | 3 | protected function processSingleIdentifierRelationship($source, DocumentHydrator $hydrator): SingleIdentifierRelationship |
|
99 | |||
100 | /** |
||
101 | * Process relationship with collection of resource identifiers |
||
102 | * |
||
103 | * @param mixed $source |
||
104 | * @param DocumentHydrator $hydrator |
||
105 | * @return IdentifierCollectionRelationship |
||
106 | */ |
||
107 | 2 | protected function processIdentifierCollectionRelationship($source, DocumentHydrator $hydrator): IdentifierCollectionRelationship |
|
120 | |||
121 | /** |
||
122 | * Create resource |
||
123 | * |
||
124 | * @param mixed $source |
||
125 | * @param DocumentHydrator $hydrator |
||
126 | * @return ResourceIdentifierObject |
||
127 | */ |
||
128 | 4 | protected function createResourceIdentifier($source, DocumentHydrator $hydrator): ResourceIdentifierObject |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 1 | public function supports(): array |
|
152 | } |
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.