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 | 1 | $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 | 10 | protected function setUpAnnotations() |
|
97 | |||
98 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
126 | |||
127 | /** |
||
128 | * @param string $class |
||
129 | * @param array $json |
||
130 | * @return ResourceInterface |
||
131 | */ |
||
132 | 5 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
144 | |||
145 | /** |
||
146 | * @param string $class |
||
147 | * @param array $json |
||
148 | * @return ResourceInterface |
||
149 | */ |
||
150 | 7 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
163 | |||
164 | /** |
||
165 | * @param array $json |
||
166 | * @param ResourceInterface $object |
||
167 | * @return array |
||
168 | */ |
||
169 | 6 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
182 | |||
183 | /** |
||
184 | * Ensure all properties expected by resource are available |
||
185 | * |
||
186 | * @param array $json |
||
187 | * @param string $class |
||
188 | * @return array |
||
189 | */ |
||
190 | 6 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
|
202 | |||
203 | /** |
||
204 | * @param string $class |
||
205 | * @return string[] |
||
206 | */ |
||
207 | 6 | protected function getReflectionClassProperties(string $class): array |
|
219 | |||
220 | 7 | protected function getEmptyOrResource(string $class, array $json): string |
|
250 | |||
251 | /** |
||
252 | * @param string $class |
||
253 | * @param ResourceInterface $object |
||
254 | * @return array |
||
255 | */ |
||
256 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
268 | |||
269 | /** |
||
270 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
271 | * @param string $class |
||
272 | * @param ResourceInterface $object |
||
273 | * @return array |
||
274 | */ |
||
275 | 3 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
285 | |||
286 | /** |
||
287 | * @param array $json |
||
288 | * @param ResourceInterface $object |
||
289 | * @return array |
||
290 | */ |
||
291 | 3 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
304 | |||
305 | /** |
||
306 | * @param ResourceInterface $object |
||
307 | * @param string $annotationClass |
||
308 | * @return null|AnnotationInterface |
||
309 | */ |
||
310 | 6 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
324 | |||
325 | /** |
||
326 | * @param string $class |
||
327 | * @param string $annotationClass |
||
328 | * @return null|AnnotationInterface |
||
329 | */ |
||
330 | 6 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
357 | |||
358 | /** |
||
359 | * @param string $resource |
||
360 | * @param ResourceInterface $object |
||
361 | * @return ResourceInterface |
||
362 | */ |
||
363 | 1 | View Code Duplication | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
373 | |||
374 | /** |
||
375 | * @param string $resource |
||
376 | * @param ResourceInterface $object |
||
377 | * @return ResourceInterface |
||
378 | */ |
||
379 | View Code Duplication | public function buildSyncFromAsync(string $resource, ResourceInterface $object): ResourceInterface |
|
389 | |||
390 | /** |
||
391 | * @param string $class |
||
392 | * @return HydratorInterface |
||
393 | */ |
||
394 | 9 | protected function getHydrator(string $class): HydratorInterface |
|
416 | } |
||
417 |