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 ClassMetadataFactory 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 ClassMetadataFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var EntityManagerInterface|null |
||
| 50 | */ |
||
| 51 | private $em; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \Doctrine\DBAL\Platforms\AbstractPlatform |
||
| 55 | */ |
||
| 56 | private $targetPlatform; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver |
||
| 60 | */ |
||
| 61 | private $driver; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \Doctrine\Common\EventManager |
||
| 65 | */ |
||
| 66 | private $evm; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $embeddablesActiveNesting = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritDoc} |
||
| 75 | */ |
||
| 76 | 477 | protected function loadMetadata($name) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param EntityManagerInterface $em |
||
| 87 | */ |
||
| 88 | 2417 | public function setEntityManager(EntityManagerInterface $em) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritDoc} |
||
| 95 | */ |
||
| 96 | 546 | protected function initialize() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | 3 | protected function onNotFoundMetadata($className) |
|
| 107 | { |
||
| 108 | 3 | if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) { |
|
| 109 | 1 | return; |
|
| 110 | } |
||
| 111 | |||
| 112 | 2 | $eventArgs = new OnClassMetadataNotFoundEventArgs($className, $this->em); |
|
|
|
|||
| 113 | |||
| 114 | 2 | $this->evm->dispatchEvent(Events::onClassMetadataNotFound, $eventArgs); |
|
| 115 | |||
| 116 | 2 | return $eventArgs->getFoundMetadata(); |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | */ |
||
| 122 | 474 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents) |
|
| 123 | { |
||
| 124 | /* @var $class ClassMetadata */ |
||
| 125 | /* @var $parent ClassMetadata */ |
||
| 126 | 474 | if ($parent) { |
|
| 127 | 146 | $class->setInheritanceType($parent->inheritanceType); |
|
| 128 | 146 | $class->setDiscriminatorColumn($parent->discriminatorColumn); |
|
| 129 | 146 | $class->setIdGeneratorType($parent->generatorType); |
|
| 130 | 146 | $this->addInheritedFields($class, $parent); |
|
| 131 | 146 | $this->addInheritedRelations($class, $parent); |
|
| 132 | 145 | $this->addInheritedEmbeddedClasses($class, $parent); |
|
| 133 | 145 | $class->setIdentifier($parent->identifier); |
|
| 134 | 145 | $class->setVersioned($parent->isVersioned); |
|
| 135 | 145 | $class->setVersionField($parent->versionField); |
|
| 136 | 145 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
| 137 | 145 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
| 138 | 145 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
| 139 | |||
| 140 | 145 | if ( ! empty($parent->customGeneratorDefinition)) { |
|
| 141 | 1 | $class->setCustomGeneratorDefinition($parent->customGeneratorDefinition); |
|
| 142 | } |
||
| 143 | |||
| 144 | 145 | if ($parent->isMappedSuperclass) { |
|
| 145 | 53 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | // Invoke driver |
||
| 150 | try { |
||
| 151 | 474 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
| 152 | 2 | } catch (ReflectionException $e) { |
|
| 153 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
| 154 | } |
||
| 155 | |||
| 156 | // If this class has a parent the id generator strategy is inherited. |
||
| 157 | // However this is only true if the hierarchy of parents contains the root entity, |
||
| 158 | // if it consists of mapped superclasses these don't necessarily include the id field. |
||
| 159 | 472 | if ($parent && $rootEntityFound) { |
|
| 160 | 98 | $this->inheritIdGeneratorMapping($class, $parent); |
|
| 161 | } else { |
||
| 162 | 468 | $this->completeIdGeneratorMapping($class); |
|
| 163 | } |
||
| 164 | |||
| 165 | 470 | if (!$class->isMappedSuperclass) { |
|
| 166 | 468 | foreach ($class->embeddedClasses as $property => $embeddableClass) { |
|
| 167 | |||
| 168 | 14 | if (isset($embeddableClass['inherited'])) { |
|
| 169 | 1 | continue; |
|
| 170 | } |
||
| 171 | |||
| 172 | 14 | if ( ! (isset($embeddableClass['class']) && $embeddableClass['class'])) { |
|
| 173 | 1 | throw MappingException::missingEmbeddedClass($property); |
|
| 174 | } |
||
| 175 | |||
| 176 | 13 | if (isset($this->embeddablesActiveNesting[$embeddableClass['class']])) { |
|
| 177 | 2 | throw MappingException::infiniteEmbeddableNesting($class->name, $property); |
|
| 178 | } |
||
| 179 | |||
| 180 | 13 | $this->embeddablesActiveNesting[$class->name] = true; |
|
| 181 | |||
| 182 | 13 | $embeddableMetadata = $this->getMetadataFor($embeddableClass['class']); |
|
| 183 | |||
| 184 | 12 | if ($embeddableMetadata->isEmbeddedClass) { |
|
| 185 | 12 | $this->addNestedEmbeddedClasses($embeddableMetadata, $class, $property); |
|
| 186 | } |
||
| 187 | |||
| 188 | 12 | $identifier = $embeddableMetadata->getIdentifier(); |
|
| 189 | |||
| 190 | 12 | if (! empty($identifier)) { |
|
| 191 | 5 | $this->inheritIdGeneratorMapping($class, $embeddableMetadata); |
|
| 192 | } |
||
| 193 | |||
| 194 | 12 | $class->inlineEmbeddable($property, $embeddableMetadata); |
|
| 195 | |||
| 196 | 12 | unset($this->embeddablesActiveNesting[$class->name]); |
|
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | 467 | if ($parent) { |
|
| 201 | 145 | if ($parent->isInheritanceTypeSingleTable()) { |
|
| 202 | 37 | $class->setPrimaryTable($parent->table); |
|
| 203 | } |
||
| 204 | |||
| 205 | 145 | if ($parent) { |
|
| 206 | 145 | $this->addInheritedIndexes($class, $parent); |
|
| 207 | } |
||
| 208 | |||
| 209 | 145 | if ($parent->cache) { |
|
| 210 | 3 | $class->cache = $parent->cache; |
|
| 211 | } |
||
| 212 | |||
| 213 | 145 | if ($parent->containsForeignIdentifier) { |
|
| 214 | 1 | $class->containsForeignIdentifier = true; |
|
| 215 | } |
||
| 216 | |||
| 217 | 145 | if ( ! empty($parent->namedQueries)) { |
|
| 218 | 1 | $this->addInheritedNamedQueries($class, $parent); |
|
| 219 | } |
||
| 220 | |||
| 221 | 145 | if ( ! empty($parent->namedNativeQueries)) { |
|
| 222 | 9 | $this->addInheritedNamedNativeQueries($class, $parent); |
|
| 223 | } |
||
| 224 | |||
| 225 | 145 | if ( ! empty($parent->sqlResultSetMappings)) { |
|
| 226 | 9 | $this->addInheritedSqlResultSetMappings($class, $parent); |
|
| 227 | } |
||
| 228 | |||
| 229 | 145 | if ( ! empty($parent->entityListeners) && empty($class->entityListeners)) { |
|
| 230 | 15 | $class->entityListeners = $parent->entityListeners; |
|
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | 467 | $class->setParentClasses($nonSuperclassParents); |
|
| 235 | |||
| 236 | 467 | if ($class->isRootEntity() && ! $class->isInheritanceTypeNone() && ! $class->discriminatorMap) { |
|
| 237 | 1 | $this->addDefaultDiscriminatorMap($class); |
|
| 238 | } |
||
| 239 | |||
| 240 | 467 | if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
|
| 241 | 6 | $eventArgs = new LoadClassMetadataEventArgs($class, $this->em); |
|
| 242 | 6 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
| 243 | } |
||
| 244 | |||
| 245 | 466 | $this->validateRuntimeMetadata($class, $parent); |
|
| 246 | 465 | } |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Validate runtime metadata is correctly defined. |
||
| 250 | * |
||
| 251 | * @param ClassMetadata $class |
||
| 252 | * @param ClassMetadataInterface|null $parent |
||
| 253 | * |
||
| 254 | * @return void |
||
| 255 | * |
||
| 256 | * @throws MappingException |
||
| 257 | */ |
||
| 258 | 466 | protected function validateRuntimeMetadata($class, $parent) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritDoc} |
||
| 287 | */ |
||
| 288 | 469 | protected function newClassMetadataInstance($className) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Populates the discriminator value of the given metadata (if not set) by iterating over discriminator |
||
| 295 | * map classes and looking for a fitting one. |
||
| 296 | * |
||
| 297 | * @param ClassMetadata $metadata |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | * |
||
| 301 | * @throws MappingException |
||
| 302 | */ |
||
| 303 | 458 | private function resolveDiscriminatorValue(ClassMetadata $metadata) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Adds a default discriminator map if no one is given |
||
| 337 | * |
||
| 338 | * If an entity is of any inheritance type and does not contain a |
||
| 339 | * discriminator map, then the map is generated automatically. This process |
||
| 340 | * is expensive computation wise. |
||
| 341 | * |
||
| 342 | * The automatically generated discriminator map contains the lowercase short name of |
||
| 343 | * each class as key. |
||
| 344 | * |
||
| 345 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
||
| 346 | * |
||
| 347 | * @throws MappingException |
||
| 348 | */ |
||
| 349 | 1 | private function addDefaultDiscriminatorMap(ClassMetadata $class) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Gets the lower-case short name of a class. |
||
| 377 | * |
||
| 378 | * @param string $className |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 1 | private function getShortName($className) |
|
| 383 | { |
||
| 384 | 1 | if (strpos($className, "\\") === false) { |
|
| 385 | return strtolower($className); |
||
| 386 | } |
||
| 387 | |||
| 388 | 1 | $parts = explode("\\", $className); |
|
| 389 | |||
| 390 | 1 | return strtolower(end($parts)); |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Adds inherited fields to the subclass mapping. |
||
| 395 | * |
||
| 396 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 397 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | 146 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 402 | { |
||
| 403 | 146 | View Code Duplication | foreach ($parentClass->fieldMappings as $mapping) { |
| 404 | 130 | if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
| 405 | 98 | $mapping['inherited'] = $parentClass->name; |
|
| 406 | } |
||
| 407 | 130 | if ( ! isset($mapping['declared'])) { |
|
| 408 | 130 | $mapping['declared'] = $parentClass->name; |
|
| 409 | } |
||
| 410 | 130 | $subClass->addInheritedFieldMapping($mapping); |
|
| 411 | } |
||
| 412 | 146 | foreach ($parentClass->reflFields as $name => $field) { |
|
| 413 | 131 | $subClass->reflFields[$name] = $field; |
|
| 414 | } |
||
| 415 | 146 | } |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Adds inherited association mappings to the subclass mapping. |
||
| 419 | * |
||
| 420 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 421 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | * |
||
| 425 | * @throws MappingException |
||
| 426 | */ |
||
| 427 | 146 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 447 | |||
| 448 | 145 | private function addInheritedEmbeddedClasses(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 449 | { |
||
| 450 | 145 | View Code Duplication | foreach ($parentClass->embeddedClasses as $field => $embeddedClass) { |
| 451 | 2 | if ( ! isset($embeddedClass['inherited']) && ! $parentClass->isMappedSuperclass) { |
|
| 452 | 1 | $embeddedClass['inherited'] = $parentClass->name; |
|
| 453 | } |
||
| 454 | 2 | if ( ! isset($embeddedClass['declared'])) { |
|
| 455 | 2 | $embeddedClass['declared'] = $parentClass->name; |
|
| 456 | } |
||
| 457 | |||
| 458 | 2 | $subClass->embeddedClasses[$field] = $embeddedClass; |
|
| 459 | } |
||
| 460 | 145 | } |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Adds nested embedded classes metadata to a parent class. |
||
| 464 | * |
||
| 465 | * @param ClassMetadata $subClass Sub embedded class metadata to add nested embedded classes metadata from. |
||
| 466 | * @param ClassMetadata $parentClass Parent class to add nested embedded classes metadata to. |
||
| 467 | * @param string $prefix Embedded classes' prefix to use for nested embedded classes field names. |
||
| 468 | */ |
||
| 469 | 12 | private function addNestedEmbeddedClasses(ClassMetadata $subClass, ClassMetadata $parentClass, $prefix) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Copy the table indices from the parent class superclass to the child class |
||
| 494 | * |
||
| 495 | * @param ClassMetadata $subClass |
||
| 496 | * @param ClassMetadata $parentClass |
||
| 497 | * |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | 145 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Adds inherited named queries to the subclass mapping. |
||
| 521 | * |
||
| 522 | * @since 2.2 |
||
| 523 | * |
||
| 524 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 525 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 1 | private function addInheritedNamedQueries(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Adds inherited named native queries to the subclass mapping. |
||
| 545 | * |
||
| 546 | * @since 2.3 |
||
| 547 | * |
||
| 548 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 549 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 550 | * |
||
| 551 | * @return void |
||
| 552 | */ |
||
| 553 | 9 | private function addInheritedNamedNativeQueries(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Adds inherited sql result set mappings to the subclass mapping. |
||
| 572 | * |
||
| 573 | * @since 2.3 |
||
| 574 | * |
||
| 575 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 576 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 577 | * |
||
| 578 | * @return void |
||
| 579 | */ |
||
| 580 | 9 | private function addInheritedSqlResultSetMappings(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Completes the ID generator mapping. If "auto" is specified we choose the generator |
||
| 607 | * most appropriate for the targeted database platform. |
||
| 608 | * |
||
| 609 | * @param ClassMetadataInfo $class |
||
| 610 | * |
||
| 611 | * @return void |
||
| 612 | * |
||
| 613 | * @throws ORMException |
||
| 614 | */ |
||
| 615 | 468 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Inherits the ID generator mapping from a parent class. |
||
| 720 | * |
||
| 721 | * @param ClassMetadataInfo $class |
||
| 722 | * @param ClassMetadataInfo $parent |
||
| 723 | */ |
||
| 724 | 103 | private function inheritIdGeneratorMapping(ClassMetadataInfo $class, ClassMetadataInfo $parent) |
|
| 740 | |||
| 741 | /** |
||
| 742 | * {@inheritDoc} |
||
| 743 | */ |
||
| 744 | 2082 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * {@inheritDoc} |
||
| 752 | */ |
||
| 753 | 474 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * {@inheritDoc} |
||
| 761 | */ |
||
| 762 | 4 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
| 766 | |||
| 767 | /** |
||
| 768 | * {@inheritDoc} |
||
| 769 | */ |
||
| 770 | 256 | protected function getDriver() |
|
| 774 | |||
| 775 | /** |
||
| 776 | * {@inheritDoc} |
||
| 777 | */ |
||
| 778 | 465 | protected function isEntity(ClassMetadataInterface $class) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * @return Platforms\AbstractPlatform |
||
| 785 | */ |
||
| 786 | 353 | private function getTargetPlatform() |
|
| 794 | } |
||
| 795 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: