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 declare(strict_types=1); |
||
20 | class Hydrator |
||
21 | { |
||
22 | /** |
||
23 | * @var ContainerInterface |
||
24 | */ |
||
25 | protected $container; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $hydrators = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $annotations = []; |
||
41 | |||
42 | /** |
||
43 | * @var HandlerInterface[] |
||
44 | */ |
||
45 | protected $annotationHandlers = []; |
||
46 | |||
47 | /** |
||
48 | * @var Reader |
||
49 | */ |
||
50 | protected $annotationReader; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $classProperties = []; |
||
56 | |||
57 | /** |
||
58 | * @param ContainerInterface $container |
||
59 | * @param array $options |
||
60 | */ |
||
61 | 9 | public function __construct(ContainerInterface $container, array $options) |
|
79 | |||
80 | 9 | protected function setUpAnnotations() |
|
90 | |||
91 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
119 | |||
120 | /** |
||
121 | * @param string $class |
||
122 | * @param array $json |
||
123 | * @return ResourceInterface |
||
124 | */ |
||
125 | 6 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
137 | |||
138 | /** |
||
139 | * @param string $class |
||
140 | * @param array $json |
||
141 | * @return ResourceInterface |
||
142 | */ |
||
143 | 6 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
153 | |||
154 | /** |
||
155 | * @param array $json |
||
156 | * @param ResourceInterface $object |
||
157 | * @return array |
||
158 | */ |
||
159 | 6 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
172 | |||
173 | /** |
||
174 | * Ensure all properties expected by resource are available |
||
175 | * |
||
176 | * @param array $json |
||
177 | * @param string $class |
||
178 | * @return array |
||
179 | */ |
||
180 | 6 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
|
192 | |||
193 | /** |
||
194 | * @param string $class |
||
195 | * @return string[] |
||
196 | */ |
||
197 | 6 | protected function getReflectionClassProperties(string $class): array |
|
209 | |||
210 | 6 | protected function getEmptyOrResource(string $class, array $json): string |
|
234 | |||
235 | /** |
||
236 | * @param string $class |
||
237 | * @param ResourceInterface $object |
||
238 | * @return array |
||
239 | */ |
||
240 | 3 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
252 | |||
253 | /** |
||
254 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
255 | * @param string $class |
||
256 | * @param ResourceInterface $object |
||
257 | * @return array |
||
258 | */ |
||
259 | 3 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
269 | |||
270 | /** |
||
271 | * @param array $json |
||
272 | * @param ResourceInterface $object |
||
273 | * @return array |
||
274 | */ |
||
275 | 3 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
288 | |||
289 | /** |
||
290 | * @param ResourceInterface $object |
||
291 | * @param string $annotationClass |
||
292 | * @return null|AnnotationInterface |
||
293 | */ |
||
294 | 6 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
308 | |||
309 | /** |
||
310 | * @param string $class |
||
311 | * @param string $annotationClass |
||
312 | * @return null|AnnotationInterface |
||
313 | */ |
||
314 | 6 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
341 | |||
342 | /** |
||
343 | * @param string $resource |
||
344 | * @param ResourceInterface $object |
||
345 | * @return ResourceInterface |
||
346 | */ |
||
347 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
357 | |||
358 | /** |
||
359 | * @param string $class |
||
360 | * @return HydratorInterface |
||
361 | */ |
||
362 | 8 | protected function getHydrator(string $class): HydratorInterface |
|
380 | } |
||
381 |