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 LoopInterface |
||
24 | */ |
||
25 | protected $loop; |
||
26 | |||
27 | /** |
||
28 | * @var CommandBusInterface |
||
29 | */ |
||
30 | protected $commandBus; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $options; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $hydrators = []; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $annotations = []; |
||
46 | |||
47 | /** |
||
48 | * @var HandlerInterface[] |
||
49 | */ |
||
50 | protected $annotationHandlers = []; |
||
51 | |||
52 | /** |
||
53 | * @var Reader |
||
54 | */ |
||
55 | protected $annotationReader; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $classProperties = []; |
||
61 | |||
62 | /** |
||
63 | * @param LoopInterface $loop |
||
64 | * @param CommandBusInterface $commandBus |
||
65 | * @param array $options |
||
66 | */ |
||
67 | 10 | public function __construct(LoopInterface $loop, CommandBusInterface $commandBus, array $options) |
|
68 | { |
||
69 | 10 | $this->loop = $loop; |
|
70 | 10 | $this->commandBus = $commandBus; |
|
71 | 10 | $this->options = $options; |
|
72 | |||
73 | 10 | $reader = new AnnotationReader(); |
|
74 | 10 | if (isset($this->options[Options::ANNOTATION_CACHE]) && |
|
75 | 10 | $this->options[Options::ANNOTATION_CACHE] instanceof Cache |
|
76 | ) { |
||
77 | 1 | $reader = new CachedReader( |
|
78 | 1 | $reader, |
|
79 | 1 | $this->options[Options::ANNOTATION_CACHE] |
|
80 | ); |
||
81 | } |
||
82 | 10 | $this->annotationReader = $reader; |
|
83 | |||
84 | 10 | $this->setUpAnnotations(); |
|
85 | 10 | } |
|
86 | |||
87 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
115 | |||
116 | /** |
||
117 | * @param string $class |
||
118 | * @param array $json |
||
119 | * @return ResourceInterface |
||
120 | */ |
||
121 | 5 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
134 | |||
135 | /** |
||
136 | * @param string $class |
||
137 | * @param array $json |
||
138 | * @return ResourceInterface |
||
139 | */ |
||
140 | 7 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
154 | |||
155 | /** |
||
156 | * @param string $class |
||
157 | * @param ResourceInterface $object |
||
158 | * @return array |
||
159 | */ |
||
160 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
173 | |||
174 | /** |
||
175 | * Takes a fully qualified class name and extracts the data for that class from the given $object. |
||
176 | * @param string $class |
||
177 | * @param ResourceInterface $object |
||
178 | * @return array |
||
179 | */ |
||
180 | 3 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
192 | |||
193 | /** |
||
194 | * @param string $resource |
||
195 | * @param ResourceInterface $object |
||
196 | * @return ResourceInterface |
||
197 | */ |
||
198 | 1 | View Code Duplication | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
208 | |||
209 | /** |
||
210 | * @param string $resource |
||
211 | * @param ResourceInterface $object |
||
212 | * @return ResourceInterface |
||
213 | */ |
||
214 | View Code Duplication | public function buildSyncFromAsync(string $resource, ResourceInterface $object): ResourceInterface |
|
224 | |||
225 | 10 | protected function setUpAnnotations() |
|
235 | |||
236 | /** |
||
237 | * @param array $json |
||
238 | * @param ResourceInterface $object |
||
239 | * @return array |
||
240 | */ |
||
241 | 6 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
254 | |||
255 | /** |
||
256 | * Ensure all properties expected by resource are available. |
||
257 | * |
||
258 | * @param array $json |
||
259 | * @param string $class |
||
260 | * @return array |
||
261 | */ |
||
262 | 6 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
|
274 | |||
275 | /** |
||
276 | * @param string $class |
||
277 | * @return string[] |
||
278 | */ |
||
279 | 6 | protected function getReflectionClassProperties(string $class): array |
|
292 | |||
293 | 7 | protected function getEmptyOrResource(string $class, array $json): string |
|
323 | |||
324 | /** |
||
325 | * @param array $json |
||
326 | * @param ResourceInterface $object |
||
327 | * @return array |
||
328 | */ |
||
329 | 3 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
342 | |||
343 | /** |
||
344 | * @param ResourceInterface $object |
||
345 | * @param string $annotationClass |
||
346 | * @return null|AnnotationInterface |
||
347 | */ |
||
348 | 6 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
363 | |||
364 | /** |
||
365 | * @param string $class |
||
366 | * @param string $annotationClass |
||
367 | * @return null|AnnotationInterface |
||
368 | */ |
||
369 | 6 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
396 | |||
397 | /** |
||
398 | * @param string $class |
||
399 | * @return HydratorInterface |
||
400 | */ |
||
401 | 9 | protected function getHydrator(string $class): HydratorInterface |
|
423 | } |
||
424 |