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 |
||
| 42 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
| 43 | { |
||
| 44 | protected $cacheSalt = "\$MONGODBODMCLASSMETADATA"; |
||
| 45 | |||
| 46 | /** @var DocumentManager The DocumentManager instance */ |
||
| 47 | private $dm; |
||
| 48 | |||
| 49 | /** @var Configuration The Configuration instance */ |
||
| 50 | private $config; |
||
| 51 | |||
| 52 | /** @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver The used metadata driver. */ |
||
| 53 | private $driver; |
||
| 54 | |||
| 55 | /** @var \Doctrine\Common\EventManager The event manager instance */ |
||
| 56 | private $evm; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Sets the DocumentManager instance for this class. |
||
| 60 | * |
||
| 61 | * @param DocumentManager $dm The DocumentManager instance |
||
| 62 | */ |
||
| 63 | 1045 | public function setDocumentManager(DocumentManager $dm) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the Configuration instance |
||
| 70 | * |
||
| 71 | * @param Configuration $config |
||
| 72 | */ |
||
| 73 | 1045 | public function setConfiguration(Configuration $config) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Lazy initialization of this stuff, especially the metadata driver, |
||
| 80 | * since these are not needed at all when a metadata cache is active. |
||
| 81 | */ |
||
| 82 | 839 | protected function initialize() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritDoc} |
||
| 91 | */ |
||
| 92 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritDoc} |
||
| 99 | */ |
||
| 100 | 349 | protected function getDriver() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | 824 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritDoc} |
||
| 114 | */ |
||
| 115 | 839 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | */ |
||
| 122 | 824 | protected function isEntity(ClassMetadataInterface $class) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 839 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
| 131 | { |
||
| 132 | /** @var $class ClassMetadata */ |
||
| 133 | /** @var $parent ClassMetadata */ |
||
| 134 | 839 | if ($parent) { |
|
| 135 | 346 | $class->setInheritanceType($parent->inheritanceType); |
|
| 136 | 346 | $class->setDiscriminatorField($parent->discriminatorField); |
|
| 137 | 346 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
| 138 | 346 | $class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
| 139 | 346 | $class->setIdGeneratorType($parent->generatorType); |
|
| 140 | 346 | $this->addInheritedFields($class, $parent); |
|
| 141 | 346 | $this->addInheritedRelations($class, $parent); |
|
| 142 | 346 | $this->addInheritedIndexes($class, $parent); |
|
| 143 | 346 | $this->setInheritedShardKey($class, $parent); |
|
| 144 | 346 | $class->setIdentifier($parent->identifier); |
|
| 145 | 346 | $class->setVersioned($parent->isVersioned); |
|
| 146 | 346 | $class->setVersionField($parent->versionField); |
|
| 147 | 346 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
| 148 | 346 | $class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
| 149 | 346 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
| 150 | 346 | $class->setFile($parent->getFile()); |
|
| 151 | 346 | if ($parent->isMappedSuperclass) { |
|
| 152 | 284 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | // Invoke driver |
||
| 157 | try { |
||
| 158 | 839 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
| 159 | 68 | } catch (\ReflectionException $e) { |
|
| 160 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
| 161 | } |
||
| 162 | |||
| 163 | 824 | $this->validateIdentifier($class); |
|
| 164 | |||
| 165 | 824 | if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
| 166 | 108 | if ($parent->generatorType) { |
|
| 167 | 108 | $class->setIdGeneratorType($parent->generatorType); |
|
| 168 | } |
||
| 169 | 108 | if ($parent->generatorOptions) { |
|
| 170 | $class->setIdGeneratorOptions($parent->generatorOptions); |
||
| 171 | } |
||
| 172 | 108 | if ($parent->idGenerator) { |
|
| 173 | 108 | $class->setIdGenerator($parent->idGenerator); |
|
| 174 | } |
||
| 175 | } else { |
||
| 176 | 824 | $this->completeIdGeneratorMapping($class); |
|
| 177 | } |
||
| 178 | |||
| 179 | 824 | if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
|
| 180 | 93 | $class->setDatabase($parent->getDatabase()); |
|
| 181 | 93 | $class->setCollection($parent->getCollection()); |
|
| 182 | } |
||
| 183 | |||
| 184 | 824 | $class->setParentClasses($nonSuperclassParents); |
|
| 185 | |||
| 186 | 824 | View Code Duplication | if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
| 187 | 2 | $eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
|
| 188 | 2 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
| 189 | } |
||
| 190 | 824 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Validates the identifier mapping. |
||
| 194 | * |
||
| 195 | * @param ClassMetadata $class |
||
| 196 | * @throws MappingException |
||
| 197 | */ |
||
| 198 | 824 | protected function validateIdentifier($class) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Creates a new ClassMetadata instance for the given class name. |
||
| 207 | * |
||
| 208 | * @param string $className |
||
| 209 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
| 210 | */ |
||
| 211 | 839 | protected function newClassMetadataInstance($className) |
|
| 215 | |||
| 216 | 824 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Adds inherited fields to the subclass mapping. |
||
| 282 | * |
||
| 283 | * @param ClassMetadata $subClass |
||
| 284 | * @param ClassMetadata $parentClass |
||
| 285 | */ |
||
| 286 | 346 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds inherited association mappings to the subclass mapping. |
||
| 305 | * |
||
| 306 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
||
| 307 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | * |
||
| 311 | * @throws MappingException |
||
| 312 | */ |
||
| 313 | 346 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Adds inherited indexes to the subclass mapping. |
||
| 332 | * |
||
| 333 | * @param ClassMetadata $subClass |
||
| 334 | * @param ClassMetadata $parentClass |
||
| 335 | */ |
||
| 336 | 346 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Adds inherited shard key to the subclass mapping. |
||
| 345 | * |
||
| 346 | * @param ClassMetadata $subClass |
||
| 347 | * @param ClassMetadata $parentClass |
||
| 348 | */ |
||
| 349 | 346 | private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 358 | } |
||
| 359 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: