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); |
||
21 | class Hydrator |
||
22 | { |
||
23 | /** |
||
24 | * @var ContainerInterface |
||
25 | */ |
||
26 | protected $container; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $options; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $hydrators = []; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $annotations = []; |
||
42 | |||
43 | /** |
||
44 | * @var HandlerInterface[] |
||
45 | */ |
||
46 | protected $annotationHandlers = []; |
||
47 | |||
48 | /** |
||
49 | * @var Reader |
||
50 | */ |
||
51 | protected $annotationReader; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $classProperties = []; |
||
57 | |||
58 | /** |
||
59 | * @param ContainerInterface $container |
||
60 | * @param array $options |
||
61 | */ |
||
62 | 9 | public function __construct(ContainerInterface $container, array $options) |
|
80 | |||
81 | 9 | protected function setUpAnnotations() |
|
91 | |||
92 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
120 | |||
121 | /** |
||
122 | * @param string $class |
||
123 | * @param array $json |
||
124 | * @return ResourceInterface |
||
125 | */ |
||
126 | 5 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
138 | |||
139 | /** |
||
140 | * @param string $class |
||
141 | * @param array $json |
||
142 | * @return ResourceInterface |
||
143 | */ |
||
144 | 6 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
157 | |||
158 | /** |
||
159 | * @param array $json |
||
160 | * @param ResourceInterface $object |
||
161 | * @return array |
||
162 | */ |
||
163 | 6 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
176 | |||
177 | /** |
||
178 | * Ensure all properties expected by resource are available |
||
179 | * |
||
180 | * @param array $json |
||
181 | * @param string $class |
||
182 | * @return array |
||
183 | */ |
||
184 | 6 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
|
196 | |||
197 | /** |
||
198 | * @param string $class |
||
199 | * @return string[] |
||
200 | */ |
||
201 | 6 | protected function getReflectionClassProperties(string $class): array |
|
213 | |||
214 | 6 | protected function getEmptyOrResource(string $class, array $json): string |
|
244 | |||
245 | /** |
||
246 | * @param string $class |
||
247 | * @param ResourceInterface $object |
||
248 | * @return array |
||
249 | */ |
||
250 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
262 | |||
263 | /** |
||
264 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
265 | * @param string $class |
||
266 | * @param ResourceInterface $object |
||
267 | * @return array |
||
268 | */ |
||
269 | 3 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
279 | |||
280 | /** |
||
281 | * @param array $json |
||
282 | * @param ResourceInterface $object |
||
283 | * @return array |
||
284 | */ |
||
285 | 3 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
298 | |||
299 | /** |
||
300 | * @param ResourceInterface $object |
||
301 | * @param string $annotationClass |
||
302 | * @return null|AnnotationInterface |
||
303 | */ |
||
304 | 6 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
318 | |||
319 | /** |
||
320 | * @param string $class |
||
321 | * @param string $annotationClass |
||
322 | * @return null|AnnotationInterface |
||
323 | */ |
||
324 | 6 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
351 | |||
352 | /** |
||
353 | * @param string $resource |
||
354 | * @param ResourceInterface $object |
||
355 | * @return ResourceInterface |
||
356 | */ |
||
357 | 1 | View Code Duplication | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
367 | |||
368 | /** |
||
369 | * @param string $resource |
||
370 | * @param ResourceInterface $object |
||
371 | * @return ResourceInterface |
||
372 | */ |
||
373 | View Code Duplication | public function buildSyncFromAsync(string $resource, ResourceInterface $object): ResourceInterface |
|
383 | |||
384 | /** |
||
385 | * @param string $class |
||
386 | * @return HydratorInterface |
||
387 | */ |
||
388 | 8 | protected function getHydrator(string $class): HydratorInterface |
|
406 | } |
||
407 |