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); |
||
| 21 | class Hydrator |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var ContainerInterface |
||
| 25 | */ |
||
| 26 | protected $container; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $options; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $hydrators = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $annotations = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var HandlerInterface[] |
||
| 45 | */ |
||
| 46 | protected $annotationHandlers = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Reader |
||
| 50 | */ |
||
| 51 | protected $annotationReader; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param ContainerInterface $container |
||
| 55 | * @param array $options |
||
| 56 | */ |
||
| 57 | 7 | public function __construct(ContainerInterface $container, array $options) |
|
| 58 | { |
||
| 59 | 7 | $this->container = $container; |
|
| 60 | 7 | $this->options = $options; |
|
| 61 | |||
| 62 | 7 | $reader = new AnnotationReader(); |
|
| 63 | 7 | if (isset($this->options[Options::ANNOTATION_CACHE]) && |
|
| 64 | 7 | $this->options[Options::ANNOTATION_CACHE] instanceof Cache |
|
| 65 | ) { |
||
| 66 | 1 | $reader = new CachedReader( |
|
| 67 | $reader, |
||
| 68 | 1 | $this->options[Options::ANNOTATION_CACHE] |
|
| 69 | ); |
||
| 70 | } |
||
| 71 | 7 | $this->annotationReader = $reader; |
|
| 72 | |||
| 73 | 7 | $this->setUpAnnotations(); |
|
| 74 | 7 | } |
|
| 75 | |||
| 76 | 7 | protected function setUpAnnotations() |
|
| 77 | { |
||
| 78 | 7 | if (!isset($this->options[Options::ANNOTATIONS])) { |
|
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | 7 | foreach ($this->options[Options::ANNOTATIONS] as $annotationClass => $handler) { |
|
| 83 | 7 | $this->annotationHandlers[$annotationClass] = new $handler($this); |
|
| 84 | } |
||
| 85 | 7 | } |
|
| 86 | |||
| 87 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
| 88 | { |
||
| 89 | 2 | $directory = new RecursiveDirectoryIterator($scanTarget); |
|
| 90 | 2 | $directory = new RecursiveIteratorIterator($directory); |
|
| 91 | |||
| 92 | 2 | foreach ($directory as $node) { |
|
| 93 | 2 | if (!is_file($node->getPathname())) { |
|
| 94 | 2 | continue; |
|
| 95 | } |
||
| 96 | |||
| 97 | 2 | $file = substr($node->getPathname(), strlen($scanTarget)); |
|
| 98 | 2 | $file = ltrim($file, DIRECTORY_SEPARATOR); |
|
| 99 | 2 | $file = rtrim($file, '.php'); |
|
| 100 | |||
| 101 | 2 | $class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file); |
|
| 102 | |||
| 103 | 2 | if (!class_exists($class)) { |
|
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | 2 | if (!is_subclass_of($class, ResourceInterface::class)) { |
|
|
|
|||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | 2 | $this->getHydrator($class); |
|
| 112 | 2 | $this->annotationReader->getClassAnnotations(new ReflectionClass($class)); |
|
| 113 | } |
||
| 114 | 2 | } |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $class |
||
| 118 | * @param array $json |
||
| 119 | * @return ResourceInterface |
||
| 120 | */ |
||
| 121 | 4 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
| 122 | { |
||
| 123 | 4 | $fullClassName = implode( |
|
| 124 | 4 | '\\', |
|
| 125 | [ |
||
| 126 | $this->options[Options::NAMESPACE], |
||
| 127 | 4 | $this->options[Options::NAMESPACE_SUFFIX], |
|
| 128 | 4 | $class, |
|
| 129 | ] |
||
| 130 | ); |
||
| 131 | 4 | return $this->hydrateFQCN($fullClassName, $json); |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $class |
||
| 136 | * @param array $json |
||
| 137 | * @return ResourceInterface |
||
| 138 | */ |
||
| 139 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
| 140 | { |
||
| 141 | 4 | $class = $this->getEmptyOrResource($class, $json); |
|
| 142 | 4 | $hydrator = $this->getHydrator($class); |
|
| 143 | 4 | $object = new $class($this->container->get(LoopInterface::class), $this->container->get(CommandBus::class)); |
|
| 144 | 4 | $json = $this->hydrateApplyAnnotations($json, $object); |
|
| 145 | 4 | $resource = $hydrator->hydrate($json, $object); |
|
| 146 | 4 | return $resource; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $json |
||
| 151 | * @param ResourceInterface $object |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
| 167 | |||
| 168 | 4 | protected function getEmptyOrResource(string $class, array $json): string |
|
| 169 | { |
||
| 170 | 4 | if (count($json) > 0) { |
|
| 171 | 4 | return $class; |
|
| 172 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $class |
||
| 198 | * @param ResourceInterface $object |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
| 213 | |||
| 214 | /** |
||
| 215 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
| 216 | * @param string $class |
||
| 217 | * @param ResourceInterface $object |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param array $json |
||
| 233 | * @param ResourceInterface $object |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 2 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
| 249 | |||
| 250 | /** |
||
| 251 | * @param ResourceInterface $object |
||
| 252 | * @param string $annotationClass |
||
| 253 | * @return null|AnnotationInterface |
||
| 254 | */ |
||
| 255 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $class |
||
| 272 | * @param string $annotationClass |
||
| 273 | * @return null|AnnotationInterface |
||
| 274 | */ |
||
| 275 | 4 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $resource |
||
| 305 | * @param ResourceInterface $object |
||
| 306 | * @return ResourceInterface |
||
| 307 | */ |
||
| 308 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $class |
||
| 321 | * @return HydratorInterface |
||
| 322 | */ |
||
| 323 | 6 | protected function getHydrator(string $class): HydratorInterface |
|
| 341 | } |
||
| 342 |