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 | * @var array |
||
54 | */ |
||
55 | protected $classProperties = []; |
||
56 | |||
57 | /** |
||
58 | * @param ContainerInterface $container |
||
59 | * @param array $options |
||
60 | */ |
||
61 | 9 | public function __construct(ContainerInterface $container, array $options) |
|
79 | |||
80 | 9 | protected function setUpAnnotations() |
|
90 | |||
91 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
92 | { |
||
93 | 2 | $directory = new RecursiveDirectoryIterator($scanTarget); |
|
94 | 2 | $directory = new RecursiveIteratorIterator($directory); |
|
95 | |||
96 | 2 | foreach ($directory as $node) { |
|
97 | 2 | if (!is_file($node->getPathname())) { |
|
98 | 2 | continue; |
|
99 | } |
||
100 | |||
101 | 2 | $file = substr($node->getPathname(), strlen($scanTarget)); |
|
102 | 2 | $file = ltrim($file, DIRECTORY_SEPARATOR); |
|
103 | 2 | $file = rtrim($file, '.php'); |
|
104 | |||
105 | 2 | $class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file); |
|
106 | |||
107 | 2 | if (!class_exists($class)) { |
|
108 | continue; |
||
109 | } |
||
110 | |||
111 | 2 | if (!is_subclass_of($class, ResourceInterface::class)) { |
|
|
|||
112 | continue; |
||
113 | } |
||
114 | |||
115 | 2 | $this->getHydrator($class); |
|
116 | 2 | $this->annotationReader->getClassAnnotations(new ReflectionClass($class)); |
|
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param string $class |
||
122 | * @param array $json |
||
123 | * @return ResourceInterface |
||
124 | */ |
||
125 | 3 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
137 | |||
138 | /** |
||
139 | * @param string $class |
||
140 | * @param array $json |
||
141 | * @return ResourceInterface |
||
142 | */ |
||
143 | 6 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
153 | |||
154 | /** |
||
155 | * @param array $json |
||
156 | * @param ResourceInterface $object |
||
157 | * @return array |
||
158 | */ |
||
159 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
172 | |||
173 | /** |
||
174 | * Ensure all properties expected by resource are available |
||
175 | * |
||
176 | * @param array $json |
||
177 | * @param string $class |
||
178 | * @return array |
||
179 | */ |
||
180 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
||
192 | |||
193 | /** |
||
194 | * @param string $class |
||
195 | * @return string[] |
||
196 | */ |
||
197 | protected function getReflectionClassProperties(string $class): array |
||
209 | |||
210 | 6 | protected function getEmptyOrResource(string $class, array $json): string |
|
237 | |||
238 | /** |
||
239 | * @param string $class |
||
240 | * @param ResourceInterface $object |
||
241 | * @return array |
||
242 | */ |
||
243 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
|
255 | |||
256 | /** |
||
257 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
258 | * @param string $class |
||
259 | * @param ResourceInterface $object |
||
260 | * @return array |
||
261 | */ |
||
262 | public function extractFQCN(string $class, ResourceInterface $object): array |
||
272 | |||
273 | /** |
||
274 | * @param array $json |
||
275 | * @param ResourceInterface $object |
||
276 | * @return array |
||
277 | */ |
||
278 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
|
291 | |||
292 | /** |
||
293 | * @param ResourceInterface $object |
||
294 | * @param string $annotationClass |
||
295 | * @return null|AnnotationInterface |
||
296 | */ |
||
297 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
311 | |||
312 | /** |
||
313 | * @param string $class |
||
314 | * @param string $annotationClass |
||
315 | * @return null|AnnotationInterface |
||
316 | */ |
||
317 | 4 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
344 | |||
345 | /** |
||
346 | * @param string $resource |
||
347 | * @param ResourceInterface $object |
||
348 | * @return ResourceInterface |
||
349 | */ |
||
350 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
||
360 | |||
361 | /** |
||
362 | * @param string $class |
||
363 | * @return HydratorInterface |
||
364 | */ |
||
365 | 8 | protected function getHydrator(string $class): HydratorInterface |
|
383 | } |
||
384 |