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 Hydrator 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 Hydrator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Hydrator |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $options; |
||
21 | |||
22 | /** |
||
23 | * @var Client |
||
24 | */ |
||
25 | protected $transport; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $hydrators = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $annotations = []; |
||
36 | |||
37 | /** |
||
38 | * @var AnnotationReader |
||
39 | */ |
||
40 | protected $annotationReader; |
||
41 | |||
42 | /** |
||
43 | * @param Client $transport |
||
44 | * @param array $options |
||
45 | */ |
||
46 | 17 | public function __construct(Client $transport, array $options) |
|
52 | |||
53 | /** |
||
54 | * @param string $class |
||
55 | * @param array $json |
||
56 | * @return ResourceInterface |
||
57 | */ |
||
58 | 5 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
67 | |||
68 | /** |
||
69 | * @param ResourceInterface $object |
||
70 | * @param array $json |
||
71 | * @return array |
||
72 | */ |
||
73 | 5 | View Code Duplication | protected function hydrateNestedResources(ResourceInterface $object, array $json) |
91 | |||
92 | /** |
||
93 | * @param ResourceInterface $object |
||
94 | * @param array $json |
||
95 | * @return array |
||
96 | */ |
||
97 | 5 | View Code Duplication | protected function hydrateCollectionResources(ResourceInterface $object, array $json) |
119 | |||
120 | /** |
||
121 | * @param ResourceInterface $object |
||
122 | * @param array $json |
||
123 | * @return array |
||
124 | */ |
||
125 | 5 | View Code Duplication | protected function hydrateRenameResourceProperties(ResourceInterface $object, array $json) |
140 | |||
141 | /** |
||
142 | * @param string $class |
||
143 | * @param array $json |
||
144 | * @return ResourceInterface |
||
145 | */ |
||
146 | 5 | public function hydrate(string $class, array $json): ResourceInterface |
|
151 | |||
152 | /** |
||
153 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
154 | * @param string $class |
||
155 | * @param ResourceInterface $object |
||
156 | * @return array |
||
157 | */ |
||
158 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
166 | |||
167 | /** |
||
168 | * @param array $json |
||
169 | * @param ResourceInterface $object |
||
170 | * @return array |
||
171 | */ |
||
172 | 2 | View Code Duplication | protected function extractNestedResources(array $json, ResourceInterface $object) |
186 | |||
187 | /** |
||
188 | * @param array $json |
||
189 | * @param ResourceInterface $object |
||
190 | * @return array |
||
191 | */ |
||
192 | 2 | View Code Duplication | protected function extractCollectionResources(array $json, ResourceInterface $object) |
210 | |||
211 | /** |
||
212 | * @param array $json |
||
213 | * @param ResourceInterface $object |
||
214 | * @return array |
||
215 | */ |
||
216 | 2 | View Code Duplication | protected function extractRenameResourceProperties(array $json, ResourceInterface $object) |
231 | |||
232 | /** |
||
233 | * @param ResourceInterface $object |
||
234 | * @param string $annotationClass |
||
235 | * @return null|object |
||
236 | */ |
||
237 | 5 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
268 | |||
269 | /** |
||
270 | * @param string $class |
||
271 | * @param ResourceInterface $object |
||
272 | * @return array |
||
273 | */ |
||
274 | 2 | public function extract(string $class, ResourceInterface $object): array |
|
279 | |||
280 | /** |
||
281 | * @param string $resource |
||
282 | * @param ResourceInterface $object |
||
283 | * @return ResourceInterface |
||
284 | */ |
||
285 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
295 | |||
296 | /** |
||
297 | * @param string $class |
||
298 | * @return HydratorInterface |
||
299 | */ |
||
300 | 5 | protected function getHydrator(string $class): HydratorInterface |
|
318 | |||
319 | /** |
||
320 | * @param string $class |
||
321 | * @return ResourceInterface |
||
322 | */ |
||
323 | 5 | protected function createObject(string $class): ResourceInterface |
|
337 | } |
||
338 |
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.