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 | 9 | public function __construct(LoopInterface $loop, CommandBusInterface $commandBus, array $options) |
|
86 | |||
87 | 1 | public function preheat(string $scanTarget, string $namespace): void |
|
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 |
199 | { |
||
200 | 1 | return $this->hydrateFQCN( |
|
201 | $this->options[Options::NAMESPACE] . '\\Async\\' . $resource, |
||
202 | 1 | $this->extractFQCN( |
|
203 | $this->options[Options::NAMESPACE] . '\\Sync\\' . $resource, |
||
204 | $object |
||
205 | ) |
||
206 | ); |
||
207 | } |
||
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 |
|
215 | { |
||
216 | return $this->hydrateFQCN( |
||
217 | $this->options[Options::NAMESPACE] . '\\Sync\\' . $resource, |
||
218 | $this->extractFQCN( |
||
219 | $this->options[Options::NAMESPACE] . '\\Async\\' . $resource, |
||
220 | $object |
||
221 | ) |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | 9 | protected function setUpAnnotations(): void |
|
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) |
|
370 | { |
||
371 | 6 | if (!\class_exists($class)) { |
|
372 | return; |
||
373 | } |
||
374 | |||
375 | 6 | $annotation = $this->annotationReader |
|
376 | 6 | ->getClassAnnotation( |
|
377 | 6 | new ReflectionClass($class), |
|
378 | $annotationClass |
||
379 | ) |
||
380 | ; |
||
381 | |||
382 | 6 | if ($annotation !== null && |
|
383 | 6 | \get_class($annotation) === $annotationClass |
|
384 | ) { |
||
385 | 6 | return $annotation; |
|
386 | } |
||
387 | |||
388 | 6 | $parentClass = \get_parent_class($class); |
|
389 | |||
390 | 6 | if ($parentClass === false || !\class_exists($parentClass)) { |
|
391 | 5 | return; |
|
392 | } |
||
393 | |||
394 | 6 | return $this->recursivelyGetAnnotation($parentClass, $annotationClass); |
|
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param string $class |
||
399 | * @return HydratorInterface |
||
400 | */ |
||
401 | 8 | protected function getHydrator(string $class): HydratorInterface |
|
423 | } |
||
424 |