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 | 2256 | public function __construct(TypeParserInterface $typeParser = null) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 1836 | 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 | 1752 | private function doLoadClassMetadata(ClassMetadataInterface $classMetadata, array $data) |
|
| 76 | { |
||
| 77 | 1752 | if (!isset($data['properties']) || empty($data['properties'])) { |
|
| 78 | 12 | throw new \InvalidArgumentException(sprintf( |
|
| 79 | 12 | 'No mapping properties found for "%s".', |
|
| 80 | 12 | $classMetadata->getName() |
|
| 81 | 6 | )); |
|
| 82 | } |
||
| 83 | |||
| 84 | 1740 | $policy = $this->getExclusionPolicy($data); |
|
| 85 | 1728 | $readableClass = $this->getReadable($data); |
|
| 86 | 1720 | $writableClass = $this->getWritable($data); |
|
| 87 | 1712 | $properties = $classMetadata->getProperties(); |
|
| 88 | |||
| 89 | 1712 | foreach ($data['properties'] as $property => $value) { |
|
| 90 | 1712 | $propertyMetadata = $classMetadata->getProperty($property) |
|
| 91 | 1712 | ?: new PropertyMetadata($property, $classMetadata->getName()); |
|
| 92 | |||
| 93 | 1712 | $this->loadPropertyMetadata($propertyMetadata, $value, $readableClass, $writableClass); |
|
| 94 | |||
| 95 | 1548 | if ($this->isPropertyMetadataExposed($value, $policy)) { |
|
| 96 | 1548 | $properties[$property] = $propertyMetadata; |
|
| 97 | 774 | } else { |
|
| 98 | 822 | unset($properties[$property]); |
|
| 99 | } |
||
| 100 | 774 | } |
|
| 101 | |||
| 102 | 1548 | if (($order = $this->getOrder($data, $properties)) !== null) { |
|
| 103 | 144 | $properties = $this->sortProperties($properties, $order); |
|
| 104 | 72 | } |
|
| 105 | |||
| 106 | 1512 | $classMetadata->setProperties($properties); |
|
| 107 | |||
| 108 | 1512 | if (array_key_exists('xml_root', $data)) { |
|
| 109 | 104 | $this->loadClassMetadataXmlRoot($classMetadata, $data['xml_root']); |
|
| 110 | 48 | } |
|
| 111 | 1504 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 115 | * @param mixed $data |
||
| 116 | * @param bool $classReadable |
||
| 117 | * @param bool $classWritable |
||
| 118 | */ |
||
| 119 | 1712 | private function loadPropertyMetadata( |
|
| 120 | PropertyMetadataInterface $propertyMetadata, |
||
| 121 | $data, |
||
| 122 | $classReadable, |
||
| 123 | $classWritable |
||
| 124 | ) { |
||
| 125 | 1712 | if (!is_array($data)) { |
|
| 126 | 64 | $data = []; |
|
| 127 | 32 | } |
|
| 128 | |||
| 129 | 1712 | $propertyMetadata->setReadable($this->getReadable($data, $classReadable)); |
|
| 130 | 1700 | $propertyMetadata->setWritable($this->getWritable($data, $classWritable)); |
|
| 131 | |||
| 132 | 1696 | if (array_key_exists('exclude', $data)) { |
|
| 133 | 56 | $this->validatePropertyMetadataExclude($data['exclude']); |
|
| 134 | 24 | } |
|
| 135 | |||
| 136 | 1688 | if (array_key_exists('expose', $data)) { |
|
| 137 | 56 | $this->validatePropertyMetadataExpose($data['expose']); |
|
| 138 | 24 | } |
|
| 139 | |||
| 140 | 1680 | if (array_key_exists('alias', $data)) { |
|
| 141 | 156 | $this->loadPropertyMetadataAlias($propertyMetadata, $data['alias']); |
|
| 142 | 72 | } |
|
| 143 | |||
| 144 | 1668 | if (array_key_exists('type', $data)) { |
|
| 145 | 1220 | $this->loadPropertyMetadataType($propertyMetadata, $data['type']); |
|
| 146 | 604 | } |
|
| 147 | |||
| 148 | 1656 | if (array_key_exists('accessor', $data)) { |
|
| 149 | 60 | $this->loadPropertyMetadataAccessor($propertyMetadata, $data['accessor']); |
|
| 150 | 24 | } |
|
| 151 | |||
| 152 | 1644 | if (array_key_exists('mutator', $data)) { |
|
| 153 | 60 | $this->loadPropertyMetadataMutator($propertyMetadata, $data['mutator']); |
|
| 154 | 24 | } |
|
| 155 | |||
| 156 | 1632 | if (array_key_exists('since', $data)) { |
|
| 157 | 124 | $this->loadPropertyMetadataSinceVersion($propertyMetadata, $data['since']); |
|
| 158 | 56 | } |
|
| 159 | |||
| 160 | 1620 | if (array_key_exists('until', $data)) { |
|
| 161 | 124 | $this->loadPropertyMetadataUntilVersion($propertyMetadata, $data['until']); |
|
| 162 | 56 | } |
|
| 163 | |||
| 164 | 1608 | if (array_key_exists('max_depth', $data)) { |
|
| 165 | 88 | $this->loadPropertyMetadataMaxDepth($propertyMetadata, $data['max_depth']); |
|
| 166 | 40 | } |
|
| 167 | |||
| 168 | 1600 | if (array_key_exists('groups', $data)) { |
|
| 169 | 140 | $this->loadPropertyMetadataGroups($propertyMetadata, $data['groups']); |
|
| 170 | 64 | } |
|
| 171 | |||
| 172 | 1588 | if (array_key_exists('xml_attribute', $data)) { |
|
| 173 | 100 | $this->loadPropertyMetadataXmlAttribute($propertyMetadata, $data['xml_attribute']); |
|
| 174 | 48 | } |
|
| 175 | |||
| 176 | 1584 | if (array_key_exists('xml_value', $data)) { |
|
| 177 | 56 | $this->loadPropertyMetadataXmlValue($propertyMetadata, $data['xml_value']); |
|
| 178 | 24 | } |
|
| 179 | |||
| 180 | 1576 | if (array_key_exists('xml_inline', $data)) { |
|
| 181 | 52 | $this->loadPropertyMetadataXmlInline($propertyMetadata, $data['xml_inline']); |
|
| 182 | |||
| 183 | 48 | if (!array_key_exists('xml_key_as_attribute', $data)) { |
|
| 184 | 48 | $data['xml_key_as_attribute'] = true; |
|
| 185 | 24 | } |
|
| 186 | |||
| 187 | 48 | if (!array_key_exists('xml_key_as_node', $data)) { |
|
| 188 | 48 | $data['xml_key_as_node'] = false; |
|
| 189 | 24 | } |
|
| 190 | 24 | } |
|
| 191 | |||
| 192 | 1572 | if (array_key_exists('xml_entry', $data)) { |
|
| 193 | 56 | $this->loadPropertyMetadataXmlEntry($propertyMetadata, $data['xml_entry']); |
|
| 194 | 24 | } |
|
| 195 | |||
| 196 | 1564 | if (array_key_exists('xml_entry_attribute', $data)) { |
|
| 197 | 56 | $this->loadPropertyMetadataXmlEntryAttribute($propertyMetadata, $data['xml_entry_attribute']); |
|
| 198 | 24 | } |
|
| 199 | |||
| 200 | 1556 | if (array_key_exists('xml_key_as_attribute', $data)) { |
|
| 201 | 52 | $this->loadPropertyMetadataXmlKeyAsAttribute($propertyMetadata, $data['xml_key_as_attribute']); |
|
| 202 | 24 | } |
|
| 203 | |||
| 204 | 1552 | if (array_key_exists('xml_key_as_node', $data)) { |
|
| 205 | 52 | $this->loadPropertyMetadataXmlKeyAsNode($propertyMetadata, $data['xml_key_as_node']); |
|
| 206 | 24 | } |
|
| 207 | 1548 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param ClassMetadataInterface $classMetadata |
||
| 211 | * @param string $xmlRoot |
||
| 212 | */ |
||
| 213 | 104 | View Code Duplication | private function loadClassMetadataXmlRoot(ClassMetadataInterface $classMetadata, $xmlRoot) |
| 228 | |||
| 229 | /** |
||
| 230 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 231 | * @param string $alias |
||
| 232 | */ |
||
| 233 | 156 | View Code Duplication | private function loadPropertyMetadataAlias(PropertyMetadataInterface $propertyMetadata, $alias) |
| 250 | |||
| 251 | /** |
||
| 252 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 253 | * @param string $type |
||
| 254 | */ |
||
| 255 | 1220 | private function loadPropertyMetadataType(PropertyMetadataInterface $propertyMetadata, $type) |
|
| 256 | { |
||
| 257 | 1220 | if (!is_string($type)) { |
|
| 258 | 8 | throw new \InvalidArgumentException(sprintf( |
|
| 259 | 8 | 'The mapping property type must be a non empty string, got "%s".', |
|
| 260 | 8 | is_object($type) ? get_class($type) : gettype($type) |
|
| 261 | 4 | )); |
|
| 262 | } |
||
| 263 | |||
| 264 | 1212 | $propertyMetadata->setType($this->typeParser->parse($type)); |
|
| 265 | 1208 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 269 | * @param string $accessor |
||
| 270 | */ |
||
| 271 | 60 | View Code Duplication | private function loadPropertyMetadataAccessor(PropertyMetadataInterface $propertyMetadata, $accessor) |
| 272 | { |
||
| 273 | 60 | if (!is_string($accessor)) { |
|
| 274 | 8 | throw new \InvalidArgumentException(sprintf( |
|
| 275 | 8 | 'The mapping property accessor must be a non empty string, got "%s".', |
|
| 276 | 8 | is_object($accessor) ? get_class($accessor) : gettype($accessor) |
|
| 277 | 4 | )); |
|
| 278 | } |
||
| 279 | |||
| 280 | 52 | $accessor = trim($accessor); |
|
| 281 | |||
| 282 | 52 | if (empty($accessor)) { |
|
| 283 | 4 | throw new \InvalidArgumentException('The mapping property accessor must be a non empty string.'); |
|
| 284 | } |
||
| 285 | |||
| 286 | 48 | $propertyMetadata->setAccessor($accessor); |
|
| 287 | 48 | } |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 291 | * @param string $mutator |
||
| 292 | */ |
||
| 293 | 60 | View Code Duplication | private function loadPropertyMetadataMutator(PropertyMetadataInterface $propertyMetadata, $mutator) |
| 294 | { |
||
| 295 | 60 | if (!is_string($mutator)) { |
|
| 296 | 8 | throw new \InvalidArgumentException(sprintf( |
|
| 297 | 8 | 'The mapping property mutator must be a non empty string, got "%s".', |
|
| 298 | 8 | is_object($mutator) ? get_class($mutator) : gettype($mutator) |
|
| 299 | 4 | )); |
|
| 300 | } |
||
| 301 | |||
| 302 | 52 | $mutator = trim($mutator); |
|
| 303 | |||
| 304 | 52 | if (empty($mutator)) { |
|
| 305 | 4 | throw new \InvalidArgumentException('The mapping property mutator must be a non empty string.'); |
|
| 306 | } |
||
| 307 | |||
| 308 | 48 | $propertyMetadata->setMutator($mutator); |
|
| 309 | 48 | } |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 313 | * @param string $version |
||
| 314 | */ |
||
| 315 | 124 | View Code Duplication | private function loadPropertyMetadataSinceVersion(PropertyMetadataInterface $propertyMetadata, $version) |
| 316 | { |
||
| 317 | 124 | if (!is_string($version)) { |
|
| 318 | 8 | throw new \InvalidArgumentException(sprintf( |
|
| 319 | 8 | 'The mapping property since version must be a non empty string, got "%s".', |
|
| 320 | 8 | is_object($version) ? get_class($version) : gettype($version) |
|
| 321 | 4 | )); |
|
| 322 | } |
||
| 323 | |||
| 324 | 116 | $version = trim($version); |
|
| 325 | |||
| 326 | 116 | if (empty($version)) { |
|
| 327 | 4 | throw new \InvalidArgumentException('The mapping property since version must be a non empty string.'); |
|
| 328 | } |
||
| 329 | |||
| 330 | 112 | $propertyMetadata->setSinceVersion($version); |
|
| 331 | 112 | } |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 335 | * @param string $version |
||
| 336 | */ |
||
| 337 | 124 | View Code Duplication | private function loadPropertyMetadataUntilVersion(PropertyMetadataInterface $propertyMetadata, $version) |
| 338 | { |
||
| 339 | 124 | if (!is_string($version)) { |
|
| 340 | 8 | throw new \InvalidArgumentException(sprintf( |
|
| 341 | 8 | 'The mapping property until version must be a non empty string, got "%s".', |
|
| 342 | 8 | is_object($version) ? get_class($version) : gettype($version) |
|
| 343 | 4 | )); |
|
| 344 | } |
||
| 345 | |||
| 346 | 116 | $version = trim($version); |
|
| 347 | |||
| 348 | 116 | if (empty($version)) { |
|
| 349 | 4 | throw new \InvalidArgumentException('The mapping property until version must be a non empty string.'); |
|
| 350 | } |
||
| 351 | |||
| 352 | 112 | $propertyMetadata->setUntilVersion($version); |
|
| 353 | 112 | } |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 357 | * @param string|int $maxDepth |
||
| 358 | */ |
||
| 359 | 88 | private function loadPropertyMetadataMaxDepth(PropertyMetadataInterface $propertyMetadata, $maxDepth) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 382 | * @param string[] $groups |
||
| 383 | */ |
||
| 384 | 140 | private function loadPropertyMetadataGroups(PropertyMetadataInterface $propertyMetadata, $groups) |
|
| 385 | { |
||
| 386 | 140 | if (!is_array($groups)) { |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 415 | * @param bool $xmlAttribute |
||
| 416 | */ |
||
| 417 | 100 | View Code Duplication | private function loadPropertyMetadataXmlAttribute(PropertyMetadataInterface $propertyMetadata, $xmlAttribute) |
| 428 | |||
| 429 | /** |
||
| 430 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 431 | * @param bool $xmlValue |
||
| 432 | */ |
||
| 433 | 56 | View Code Duplication | private function loadPropertyMetadataXmlValue(PropertyMetadataInterface $propertyMetadata, $xmlValue) |
| 444 | |||
| 445 | /** |
||
| 446 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 447 | * @param bool $xmlInline |
||
| 448 | */ |
||
| 449 | 52 | View Code Duplication | private function loadPropertyMetadataXmlInline(PropertyMetadataInterface $propertyMetadata, $xmlInline) |
| 460 | |||
| 461 | /** |
||
| 462 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 463 | * @param string $xmlEntry |
||
| 464 | */ |
||
| 465 | 56 | View Code Duplication | private function loadPropertyMetadataXmlEntry(PropertyMetadataInterface $propertyMetadata, $xmlEntry) |
| 480 | |||
| 481 | /** |
||
| 482 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 483 | * @param string $xmlEntryAttribute |
||
| 484 | */ |
||
| 485 | 56 | View Code Duplication | private function loadPropertyMetadataXmlEntryAttribute( |
| 504 | |||
| 505 | /** |
||
| 506 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 507 | * @param string $xmlKeyAsAttribute |
||
| 508 | */ |
||
| 509 | 52 | View Code Duplication | private function loadPropertyMetadataXmlKeyAsAttribute( |
| 522 | |||
| 523 | /** |
||
| 524 | * @param PropertyMetadataInterface $propertyMetadata |
||
| 525 | * @param string $xmlKeyAsNode |
||
| 526 | */ |
||
| 527 | 52 | View Code Duplication | private function loadPropertyMetadataXmlKeyAsNode(PropertyMetadataInterface $propertyMetadata, $xmlKeyAsNode) |
| 538 | |||
| 539 | /** |
||
| 540 | * @param mixed[] $data |
||
| 541 | * |
||
| 542 | * @return string|null |
||
| 543 | */ |
||
| 544 | 1740 | private function getExclusionPolicy(array $data) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * @param mixed[] $data |
||
| 577 | * @param bool $default |
||
| 578 | * |
||
| 579 | * @return bool|null |
||
| 580 | */ |
||
| 581 | 1728 | View Code Duplication | private function getReadable(array $data, $default = true) |
| 598 | |||
| 599 | /** |
||
| 600 | * @param mixed[] $data |
||
| 601 | * @param bool $default |
||
| 602 | * |
||
| 603 | * @return bool|null |
||
| 604 | */ |
||
| 605 | 1720 | View Code Duplication | private function getWritable(array $data, $default = true) |
| 622 | |||
| 623 | /** |
||
| 624 | * @param mixed[] $data |
||
| 625 | * @param PropertyMetadataInterface[] $properties |
||
| 626 | * |
||
| 627 | * @return string|string[]|null |
||
| 628 | */ |
||
| 629 | 1548 | private function getOrder(array $data, array $properties) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * @param PropertyMetadataInterface[] $properties |
||
| 692 | * @param string|string[] $order |
||
| 693 | * |
||
| 694 | * @return PropertyMetadataInterface[] |
||
| 695 | */ |
||
| 696 | 144 | private function sortProperties(array $properties, $order) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * @param bool $exclude |
||
| 713 | */ |
||
| 714 | 56 | View Code Duplication | private function validatePropertyMetadataExclude($exclude) |
| 723 | |||
| 724 | /** |
||
| 725 | * @param bool $expose |
||
| 726 | */ |
||
| 727 | 56 | View Code Duplication | private function validatePropertyMetadataExpose($expose) |
| 736 | |||
| 737 | /** |
||
| 738 | * @param mixed[] $property |
||
| 739 | * @param string $policy |
||
| 740 | * |
||
| 741 | * @return bool |
||
| 742 | */ |
||
| 743 | 1548 | private function isPropertyMetadataExposed($property, $policy) |
|
| 750 | } |
||
| 751 |
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.