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); |
||
24 | class Hydrator |
||
25 | { |
||
26 | /** |
||
27 | * @var ContainerInterface |
||
28 | */ |
||
29 | protected $container; |
||
30 | |||
31 | /** |
||
32 | * @var LoopInterface |
||
33 | */ |
||
34 | protected $loop; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $options; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $hydrators = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $annotations = []; |
||
50 | |||
51 | /** |
||
52 | * @var HandlerInterface[] |
||
53 | */ |
||
54 | protected $annotationHandlers = []; |
||
55 | |||
56 | /** |
||
57 | * @var Reader |
||
58 | */ |
||
59 | protected $annotationReader; |
||
60 | |||
61 | /** |
||
62 | * @param ContainerInterface $container |
||
63 | * @param array $options |
||
64 | */ |
||
65 | 7 | public function __construct(ContainerInterface $container, array $options) |
|
84 | |||
85 | protected function setUpAnnotations() |
||
95 | |||
96 | public function preheat(string $scanTarget, string $namespace) |
||
124 | |||
125 | /** |
||
126 | * @param string $class |
||
127 | * @param array $json |
||
128 | * @return CancellablePromiseInterface |
||
129 | */ |
||
130 | View Code Duplication | public function hydrate(string $class, array $json): CancellablePromiseInterface |
|
142 | |||
143 | /** |
||
144 | * @param string $class |
||
145 | * @param array $json |
||
146 | * @return CancellablePromiseInterface |
||
147 | */ |
||
148 | public function hydrateFQCN(string $class, array $json): CancellablePromiseInterface |
||
159 | |||
160 | /** |
||
161 | * @param array $json |
||
162 | * @param ResourceInterface $object |
||
163 | * @return CancellablePromiseInterface |
||
164 | */ |
||
165 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): CancellablePromiseInterface |
|
187 | |||
188 | protected function getEmptyOrResource(string $class, array $json): string |
||
212 | |||
213 | /** |
||
214 | * @param string $class |
||
215 | * @param ResourceInterface $object |
||
216 | * @return CancellablePromiseInterface |
||
217 | */ |
||
218 | View Code Duplication | public function extract(string $class, ResourceInterface $object): CancellablePromiseInterface |
|
230 | |||
231 | /** |
||
232 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
233 | * @param string $class |
||
234 | * @param ResourceInterface $object |
||
235 | * @return CancellablePromiseInterface |
||
236 | */ |
||
237 | public function extractFQCN(string $class, ResourceInterface $object): CancellablePromiseInterface |
||
249 | |||
250 | /** |
||
251 | * @param array $json |
||
252 | * @param ResourceInterface $object |
||
253 | * @return CancellablePromiseInterface |
||
254 | */ |
||
255 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): CancellablePromiseInterface |
|
277 | |||
278 | /** |
||
279 | * @param ResourceInterface $object |
||
280 | * @param string $annotationClass |
||
281 | * @return null|AnnotationInterface |
||
282 | */ |
||
283 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
||
297 | |||
298 | /** |
||
299 | * @param string $class |
||
300 | * @param string $annotationClass |
||
301 | * @return null|AnnotationInterface |
||
302 | */ |
||
303 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
||
330 | |||
331 | /** |
||
332 | * @param string $resource |
||
333 | * @param ResourceInterface $object |
||
334 | * @return ResourceInterface |
||
335 | */ |
||
336 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
||
348 | |||
349 | /** |
||
350 | * @param string $class |
||
351 | * @return HydratorInterface |
||
352 | */ |
||
353 | protected function getHydrator(string $class): HydratorInterface |
||
371 | } |
||
372 |