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 AbstractClassMetadataLoader 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 AbstractClassMetadataLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class AbstractClassMetadataLoader implements ClassMetadataLoaderInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var TypeParserInterface |
||
| 28 | */ |
||
| 29 | private $typeParser; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var mixed[][] |
||
| 33 | */ |
||
| 34 | private $data = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param TypeParserInterface|null $typeParser |
||
| 38 | */ |
||
| 39 | 1092 | public function __construct(TypeParserInterface $typeParser = null) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 870 | public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $class |
||
| 66 | * |
||
| 67 | * @return mixed[]|null |
||
| 68 | */ |
||
| 69 | abstract protected function loadData($class); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param ClassMetadataInterface $classMetadata |
||
| 73 | * @param mixed[] $data |
||
| 74 | */ |
||
| 75 | 828 | private function doLoadClassMetadata(ClassMetadataInterface $classMetadata, array $data) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 105 | * @param mixed $data |
||
| 106 | */ |
||
| 107 | 810 | private function loadPropertyMetadata(PropertyMetadataInterface $propertyMetadata, $data) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 156 | * @param string $alias |
||
| 157 | */ |
||
| 158 | 93 | View Code Duplication | private function loadPropertyMetadataAlias(PropertyMetadataInterface $propertyMetadata, $alias) |
| 175 | |||
| 176 | /** |
||
| 177 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 178 | * @param string $type |
||
| 179 | */ |
||
| 180 | 267 | private function loadPropertyMetadataType(PropertyMetadataInterface $propertyMetadata, $type) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 194 | * @param string $accessor |
||
| 195 | */ |
||
| 196 | 39 | View Code Duplication | private function loadPropertyMetadataAccessor(PropertyMetadataInterface $propertyMetadata, $accessor) |
| 197 | { |
||
| 198 | 39 | if (!is_string($accessor)) { |
|
| 199 | 6 | throw new \InvalidArgumentException(sprintf( |
|
| 200 | 6 | 'The mapping property accessor must be a non empty string, got "%s".', |
|
| 201 | 6 | is_object($accessor) ? get_class($accessor) : gettype($accessor) |
|
| 202 | 4 | )); |
|
| 203 | } |
||
| 204 | |||
| 205 | 33 | $accessor = trim($accessor); |
|
| 206 | |||
| 207 | 33 | if (empty($accessor)) { |
|
| 208 | 3 | throw new \InvalidArgumentException('The mapping property accessor must be a non empty string.'); |
|
| 209 | } |
||
| 210 | |||
| 211 | 30 | $propertyMetadata->setAccessor($accessor); |
|
| 212 | 30 | } |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 216 | * @param string $mutator |
||
| 217 | */ |
||
| 218 | 39 | View Code Duplication | private function loadPropertyMetadataMutator(PropertyMetadataInterface $propertyMetadata, $mutator) |
| 219 | { |
||
| 220 | 39 | if (!is_string($mutator)) { |
|
| 221 | 6 | throw new \InvalidArgumentException(sprintf( |
|
| 222 | 6 | 'The mapping property mutator must be a non empty string, got "%s".', |
|
| 223 | 6 | is_object($mutator) ? get_class($mutator) : gettype($mutator) |
|
| 224 | 4 | )); |
|
| 225 | } |
||
| 226 | |||
| 227 | 33 | $mutator = trim($mutator); |
|
| 228 | |||
| 229 | 33 | if (empty($mutator)) { |
|
| 230 | 3 | throw new \InvalidArgumentException('The mapping property mutator must be a non empty string.'); |
|
| 231 | } |
||
| 232 | |||
| 233 | 30 | $propertyMetadata->setMutator($mutator); |
|
| 234 | 30 | } |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 238 | * @param string $version |
||
| 239 | */ |
||
| 240 | 75 | View Code Duplication | private function loadPropertyMetadataSinceVersion(PropertyMetadataInterface $propertyMetadata, $version) |
| 257 | |||
| 258 | /** |
||
| 259 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 260 | * @param string $version |
||
| 261 | */ |
||
| 262 | 75 | View Code Duplication | private function loadPropertyMetadataUntilVersion(PropertyMetadataInterface $propertyMetadata, $version) |
| 279 | |||
| 280 | /** |
||
| 281 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 282 | * @param string|int $maxDepth |
||
| 283 | */ |
||
| 284 | 54 | private function loadPropertyMetadataMaxDepth(PropertyMetadataInterface $propertyMetadata, $maxDepth) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 307 | * @param string[] $groups |
||
| 308 | */ |
||
| 309 | 84 | private function loadPropertyMetadataGroups(PropertyMetadataInterface $propertyMetadata, $groups) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param mixed[] $data |
||
| 340 | * |
||
| 341 | * @return string|null |
||
| 342 | */ |
||
| 343 | 819 | private function getExclusionPolicy(array $data) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param mixed[] $data |
||
| 376 | * @param PropertyMetadataInterface[] $properties |
||
| 377 | * |
||
| 378 | * @return string|string[]|null |
||
| 379 | */ |
||
| 380 | 729 | private function getOrder(array $data, array $properties) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param PropertyMetadataInterface[] $properties |
||
| 443 | * @param string|string[] $order |
||
| 444 | * |
||
| 445 | * @return PropertyMetadataInterface[] |
||
| 446 | */ |
||
| 447 | 90 | private function sortProperties(array $properties, $order) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param bool $exclude |
||
| 464 | */ |
||
| 465 | 36 | View Code Duplication | private function validatePropertyMetadataExclude($exclude) |
| 474 | |||
| 475 | /** |
||
| 476 | * @param bool $expose |
||
| 477 | */ |
||
| 478 | 36 | View Code Duplication | private function validatePropertyMetadataExpose($expose) |
| 487 | |||
| 488 | /** |
||
| 489 | * @param mixed[] $property |
||
| 490 | * @param string $policy |
||
| 491 | * |
||
| 492 | * @return bool |
||
| 493 | */ |
||
| 494 | 729 | private function isPropertyMetadataExposed($property, $policy) |
|
| 501 | } |
||
| 502 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.