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:
| 1 | <?php declare(strict_types=1); |
||
| 17 | class Hydrator |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $options; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $hydrators = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $annotations = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var HandlerInterface[] |
||
| 36 | */ |
||
| 37 | protected $annotationHandlers = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Reader |
||
| 41 | */ |
||
| 42 | protected $annotationReader; |
||
| 43 | |||
| 44 | /** |
||
| 45 | 4 | * @param array $options |
|
| 46 | */ |
||
| 47 | 4 | public function __construct(array $options) |
|
| 65 | |||
| 66 | 4 | protected function setUpAnnotations() |
|
| 76 | |||
| 77 | 4 | protected function addSelfToExtraProperties() |
|
| 78 | 4 | { |
|
| 79 | $this->options[Options::EXTRA_PROPERTIES]['hydrator'] = $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function preheat(string $scanTarget, string $namespace) |
||
| 83 | { |
||
| 84 | $directory = new RecursiveDirectoryIterator($scanTarget); |
||
| 85 | 4 | $directory = new RecursiveIteratorIterator($directory); |
|
| 86 | |||
| 87 | 4 | foreach ($directory as $node) { |
|
| 88 | 4 | if (!is_file($node->getPathname())) { |
|
| 89 | continue; |
||
| 90 | } |
||
| 91 | 4 | ||
| 92 | 4 | $file = substr($node->getPathname(), strlen($scanTarget)); |
|
| 93 | $file = ltrim($file, DIRECTORY_SEPARATOR); |
||
| 94 | $file = rtrim($file, '.php'); |
||
| 95 | 4 | ||
| 96 | $class = $namespace . '\\' . str_replace(DIRECTORY_SEPARATOR, '\\', $file); |
||
| 97 | |||
| 98 | if (!class_exists($class)) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!is_subclass_of($class, ResourceInterface::class)) { |
||
|
|
|||
| 103 | 4 | continue; |
|
| 104 | } |
||
| 105 | 4 | ||
| 106 | 4 | $this->getHydrator($class); |
|
| 107 | 4 | $this->annotationReader->getClassAnnotations(new ReflectionClass($class)); |
|
| 108 | 4 | } |
|
| 109 | 4 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | 4 | * @param string $class |
|
| 113 | * @param array $json |
||
| 114 | * @return ResourceInterface |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
|
| 117 | { |
||
| 118 | $fullClassName = implode( |
||
| 119 | '\\', |
||
| 120 | 4 | [ |
|
| 121 | $this->options[Options::NAMESPACE], |
||
| 122 | 4 | $this->options[Options::NAMESPACE_SUFFIX], |
|
| 123 | 4 | $class, |
|
| 124 | 4 | ] |
|
| 125 | 4 | ); |
|
| 126 | return $this->hydrateFQCN($fullClassName, $json); |
||
| 127 | } |
||
| 128 | 4 | ||
| 129 | /** |
||
| 130 | * @param string $class |
||
| 131 | 4 | * @param array $json |
|
| 132 | * @return ResourceInterface |
||
| 133 | */ |
||
| 134 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
||
| 135 | { |
||
| 136 | $hydrator = $this->getHydrator($class); |
||
| 137 | $object = new $class(); |
||
| 138 | $json = $this->hydrateApplyAnnotations($json, $object); |
||
| 139 | 2 | $resource = $hydrator->hydrate($json, $object); |
|
| 140 | if ($resource instanceof AbstractResource) { |
||
| 141 | 2 | $resource->setExtraProperties($this->options[Options::EXTRA_PROPERTIES]); |
|
| 142 | 2 | } |
|
| 143 | return $resource; |
||
| 144 | } |
||
| 145 | 2 | ||
| 146 | 2 | /** |
|
| 147 | * @param array $json |
||
| 148 | * @param ResourceInterface $object |
||
| 149 | 2 | * @return array |
|
| 150 | */ |
||
| 151 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
|
| 152 | { |
||
| 153 | foreach ($this->annotationHandlers as $annotationClass => $handler) { |
||
| 154 | $annotation = $this->getAnnotation($object, $annotationClass); |
||
| 155 | if ($annotation === null) { |
||
| 156 | continue; |
||
| 157 | } |
||
| 158 | 2 | ||
| 159 | $json = $handler->hydrate($annotation, $json, $object); |
||
| 160 | 2 | } |
|
| 161 | 2 | ||
| 162 | 2 | return $json; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $class |
||
| 167 | * @param ResourceInterface $object |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
| 171 | { |
||
| 172 | 2 | $fullClassName = implode( |
|
| 173 | 2 | '\\', |
|
| 174 | 2 | [ |
|
| 175 | 2 | $this->options[Options::NAMESPACE], |
|
| 176 | $this->options[Options::NAMESPACE_SUFFIX], |
||
| 177 | $class, |
||
| 178 | 2 | ] |
|
| 179 | ); |
||
| 180 | return $this->extractFQCN($fullClassName, $object); |
||
| 181 | 2 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
| 185 | * @param string $class |
||
| 186 | * @param ResourceInterface $object |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 4 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
| 195 | |||
| 196 | 4 | /** |
|
| 197 | 4 | * @param array $json |
|
| 198 | * @param ResourceInterface $object |
||
| 199 | * @return array |
||
| 200 | 4 | */ |
|
| 201 | 4 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
| 214 | |||
| 215 | /** |
||
| 216 | * @param ResourceInterface $object |
||
| 217 | * @param string $annotationClass |
||
| 218 | 4 | * @return null|AnnotationInterface |
|
| 219 | */ |
||
| 220 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
||
| 251 | 4 | ||
| 252 | 4 | /** |
|
| 253 | * @param string $resource |
||
| 254 | 4 | * @param ResourceInterface $object |
|
| 255 | 4 | * @return ResourceInterface |
|
| 256 | */ |
||
| 257 | 4 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $class |
||
| 270 | * @return HydratorInterface |
||
| 271 | */ |
||
| 272 | protected function getHydrator(string $class): HydratorInterface |
||
| 290 | } |
||
| 291 |