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 | * @var array |
||
| 55 | */ |
||
| 56 | protected $classProperties = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param ContainerInterface $container |
||
| 60 | * @param array $options |
||
| 61 | */ |
||
| 62 | 9 | public function __construct(ContainerInterface $container, array $options) |
|
| 80 | |||
| 81 | 9 | protected function setUpAnnotations() |
|
| 91 | |||
| 92 | 2 | public function preheat(string $scanTarget, string $namespace) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $class |
||
| 123 | * @param array $json |
||
| 124 | * @return ResourceInterface |
||
| 125 | */ |
||
| 126 | 5 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $class |
||
| 141 | * @param array $json |
||
| 142 | * @return ResourceInterface |
||
| 143 | */ |
||
| 144 | 6 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param array $json |
||
| 157 | * @param ResourceInterface $object |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | 6 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
| 173 | |||
| 174 | /** |
||
| 175 | * Ensure all properties expected by resource are available |
||
| 176 | * |
||
| 177 | * @param array $json |
||
| 178 | * @param string $class |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | 6 | protected function ensureMissingValuesAreNull(array $json, string $class): array |
|
| 182 | { |
||
| 183 | 6 | foreach ($this->getReflectionClassProperties($class) as $key) { |
|
| 184 | 6 | if (isset($json[$key])) { |
|
| 185 | 6 | continue; |
|
| 186 | } |
||
| 187 | |||
| 188 | 2 | $json[$key] = null; |
|
| 189 | } |
||
| 190 | |||
| 191 | 6 | return $json; |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $class |
||
| 196 | * @return string[] |
||
| 197 | */ |
||
| 198 | 6 | protected function getReflectionClassProperties(string $class): array |
|
| 210 | |||
| 211 | 6 | protected function getEmptyOrResource(string $class, array $json): string |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $class |
||
| 241 | * @param ResourceInterface $object |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
| 256 | |||
| 257 | /** |
||
| 258 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
| 259 | * @param string $class |
||
| 260 | * @param ResourceInterface $object |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | 3 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $json |
||
| 276 | * @param ResourceInterface $object |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 3 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
| 292 | |||
| 293 | /** |
||
| 294 | * @param ResourceInterface $object |
||
| 295 | * @param string $annotationClass |
||
| 296 | * @return null|AnnotationInterface |
||
| 297 | */ |
||
| 298 | 6 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $class |
||
| 315 | * @param string $annotationClass |
||
| 316 | * @return null|AnnotationInterface |
||
| 317 | */ |
||
| 318 | 6 | protected function recursivelyGetAnnotation(string $class, string $annotationClass) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $resource |
||
| 348 | * @param ResourceInterface $object |
||
| 349 | * @return ResourceInterface |
||
| 350 | */ |
||
| 351 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $class |
||
| 364 | * @return HydratorInterface |
||
| 365 | */ |
||
| 366 | 8 | protected function getHydrator(string $class): HydratorInterface |
|
| 384 | } |
||
| 385 |