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 ContainerInterface |
||
24 | */ |
||
25 | protected $container; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $options; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $hydrators = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $annotations = []; |
||
41 | |||
42 | /** |
||
43 | * @var HandlerInterface[] |
||
44 | */ |
||
45 | protected $annotationHandlers = []; |
||
46 | |||
47 | /** |
||
48 | * @var Reader |
||
49 | */ |
||
50 | protected $annotationReader; |
||
51 | |||
52 | /** |
||
53 | * @param ContainerInterface $container |
||
54 | * @param array $options |
||
55 | */ |
||
56 | 8 | public function __construct(ContainerInterface $container, array $options) |
|
74 | |||
75 | 8 | protected function setUpAnnotations() |
|
85 | |||
86 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
114 | |||
115 | /** |
||
116 | * @param string $class |
||
117 | * @param array $json |
||
118 | * @return ResourceInterface |
||
119 | */ |
||
120 | 4 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
132 | |||
133 | /** |
||
134 | * @param string $class |
||
135 | * @param array $json |
||
136 | * @return ResourceInterface |
||
137 | */ |
||
138 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
147 | |||
148 | /** |
||
149 | * @param array $json |
||
150 | * @param ResourceInterface $object |
||
151 | * @return array |
||
152 | */ |
||
153 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
166 | |||
167 | 4 | protected function getEmptyOrResource(string $class, array $json): string |
|
191 | |||
192 | /** |
||
193 | * @param string $class |
||
194 | * @param ResourceInterface $object |
||
195 | * @return array |
||
196 | */ |
||
197 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
209 | |||
210 | /** |
||
211 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
212 | * @param string $class |
||
213 | * @param ResourceInterface $object |
||
214 | * @return array |
||
215 | */ |
||
216 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
226 | |||
227 | /** |
||
228 | * @param array $json |
||
229 | * @param ResourceInterface $object |
||
230 | * @return array |
||
231 | */ |
||
232 | 2 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
245 | |||
246 | /** |
||
247 | * @param ResourceInterface $object |
||
248 | * @param string $annotationClass |
||
249 | * @return null|AnnotationInterface |
||
250 | */ |
||
251 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
265 | |||
266 | /** |
||
267 | * @param string $class |
||
268 | * @param string $annotationClass |
||
269 | * @return null|AnnotationInterface |
||
270 | */ |
||
271 | 4 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
298 | |||
299 | /** |
||
300 | * @param string $resource |
||
301 | * @param ResourceInterface $object |
||
302 | * @return ResourceInterface |
||
303 | */ |
||
304 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
314 | |||
315 | /** |
||
316 | * @param string $class |
||
317 | * @return HydratorInterface |
||
318 | */ |
||
319 | 6 | protected function getHydrator(string $class): HydratorInterface |
|
337 | } |
||
338 |