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 |
||
| 40 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
| 41 | { |
||
| 42 | protected $cacheSalt = "\$MONGODBODMCLASSMETADATA"; |
||
| 43 | |||
| 44 | /** @var DocumentManager The DocumentManager instance */ |
||
| 45 | private $dm; |
||
| 46 | |||
| 47 | /** @var Configuration The Configuration instance */ |
||
| 48 | private $config; |
||
| 49 | |||
| 50 | /** @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver The used metadata driver. */ |
||
| 51 | private $driver; |
||
| 52 | |||
| 53 | /** @var \Doctrine\Common\EventManager The event manager instance */ |
||
| 54 | private $evm; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Sets the DocumentManager instance for this class. |
||
| 58 | * |
||
| 59 | * @param DocumentManager $dm The DocumentManager instance |
||
| 60 | */ |
||
| 61 | 965 | public function setDocumentManager(DocumentManager $dm) |
|
| 62 | { |
||
| 63 | 965 | $this->dm = $dm; |
|
| 64 | 965 | } |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Sets the Configuration instance |
||
| 68 | * |
||
| 69 | * @param Configuration $config |
||
| 70 | */ |
||
| 71 | 965 | public function setConfiguration(Configuration $config) |
|
| 72 | { |
||
| 73 | 965 | $this->config = $config; |
|
| 74 | 965 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Lazy initialization of this stuff, especially the metadata driver, |
||
| 78 | * since these are not needed at all when a metadata cache is active. |
||
| 79 | */ |
||
| 80 | 772 | protected function initialize() |
|
| 81 | { |
||
| 82 | 772 | $this->driver = $this->config->getMetadataDriverImpl(); |
|
| 83 | 772 | $this->evm = $this->dm->getEventManager(); |
|
| 84 | 772 | $this->initialized = true; |
|
| 85 | 772 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritDoc} |
||
| 89 | */ |
||
| 90 | 2 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
| 91 | { |
||
| 92 | return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
||
| 93 | 2 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritDoc} |
||
| 97 | */ |
||
| 98 | 292 | protected function getDriver() |
|
| 99 | { |
||
| 100 | 292 | return $this->driver; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritDoc} |
||
| 105 | */ |
||
| 106 | 769 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 107 | { |
||
| 108 | 769 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritDoc} |
||
| 112 | */ |
||
| 113 | 772 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 114 | { |
||
| 115 | 772 | } |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritDoc} |
||
| 119 | */ |
||
| 120 | 769 | protected function isEntity(ClassMetadataInterface $class) |
|
| 121 | { |
||
| 122 | 769 | return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritDoc} |
||
| 127 | */ |
||
| 128 | 772 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
| 129 | { |
||
| 130 | /** @var $class ClassMetadata */ |
||
| 131 | /** @var $parent ClassMetadata */ |
||
| 132 | 772 | if ($parent) { |
|
| 133 | 290 | $class->setInheritanceType($parent->inheritanceType); |
|
| 134 | 290 | $class->setDiscriminatorField($parent->discriminatorField); |
|
| 135 | 290 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
| 136 | 290 | $class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
| 137 | 290 | $class->setIdGeneratorType($parent->generatorType); |
|
| 138 | 290 | $this->addInheritedFields($class, $parent); |
|
| 139 | 290 | $this->addInheritedRelations($class, $parent); |
|
| 140 | 290 | $this->addInheritedIndexes($class, $parent); |
|
| 141 | 290 | $class->setIdentifier($parent->identifier); |
|
| 142 | 290 | $class->setVersioned($parent->isVersioned); |
|
| 143 | 290 | $class->setVersionField($parent->versionField); |
|
| 144 | 290 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
| 145 | 290 | $class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
| 146 | 290 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
| 147 | 290 | $class->setFile($parent->getFile()); |
|
| 148 | 290 | if ($parent->isMappedSuperclass) { |
|
| 149 | 243 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
| 150 | 243 | } |
|
| 151 | 290 | } |
|
| 152 | |||
| 153 | // Invoke driver |
||
| 154 | try { |
||
| 155 | 772 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
| 156 | 772 | } catch (\ReflectionException $e) { |
|
| 157 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
| 158 | } |
||
| 159 | |||
| 160 | 769 | $this->validateIdentifier($class); |
|
| 161 | |||
| 162 | 769 | if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
| 163 | 58 | if ($parent->generatorType) { |
|
| 164 | 58 | $class->setIdGeneratorType($parent->generatorType); |
|
| 165 | 58 | } |
|
| 166 | 58 | if ($parent->generatorOptions) { |
|
| 167 | $class->setIdGeneratorOptions($parent->generatorOptions); |
||
| 168 | } |
||
| 169 | 58 | if ($parent->idGenerator) { |
|
| 170 | 58 | $class->setIdGenerator($parent->idGenerator); |
|
| 171 | 58 | } |
|
| 172 | 58 | } else { |
|
| 173 | 769 | $this->completeIdGeneratorMapping($class); |
|
| 174 | } |
||
| 175 | |||
| 176 | 769 | if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
|
| 177 | 45 | $class->setDatabase($parent->getDatabase()); |
|
| 178 | 45 | $class->setCollection($parent->getCollection()); |
|
| 179 | 45 | } |
|
| 180 | |||
| 181 | 769 | $class->setParentClasses($nonSuperclassParents); |
|
| 182 | |||
| 183 | 769 | View Code Duplication | if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
| 184 | 2 | $eventArgs = new \Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs($class, $this->dm); |
|
| 185 | 2 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
| 186 | 2 | } |
|
| 187 | 769 | } |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Validates the identifier mapping. |
||
| 191 | * |
||
| 192 | * @param ClassMetadata $class |
||
| 193 | * @throws MappingException |
||
| 194 | */ |
||
| 195 | 769 | protected function validateIdentifier($class) |
|
| 196 | { |
||
| 197 | 769 | if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument) { |
|
| 198 | throw MappingException::identifierRequired($class->name); |
||
| 199 | } |
||
| 200 | 769 | } |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Creates a new ClassMetadata instance for the given class name. |
||
| 204 | * |
||
| 205 | * @param string $className |
||
| 206 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
| 207 | */ |
||
| 208 | 772 | protected function newClassMetadataInstance($className) |
|
| 209 | { |
||
| 210 | 772 | return new ClassMetadata($className); |
|
| 211 | } |
||
| 212 | |||
| 213 | 769 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Adds inherited fields to the subclass mapping. |
||
| 279 | * |
||
| 280 | * @param ClassMetadata $subClass |
||
| 281 | * @param ClassMetadata $parentClass |
||
| 282 | */ |
||
| 283 | 290 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 284 | { |
||
| 285 | 290 | foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
|
| 286 | 74 | View Code Duplication | if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Adds inherited association mappings to the subclass mapping. |
||
| 302 | * |
||
| 303 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
| 304 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
| 305 | * |
||
| 306 | * @return void |
||
| 307 | * |
||
| 308 | * @throws MappingException |
||
| 309 | */ |
||
| 310 | 290 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Adds inherited indexes to the subclass mapping. |
||
| 329 | * |
||
| 330 | * @param ClassMetadata $subClass |
||
| 331 | * @param ClassMetadata $parentClass |
||
| 332 | */ |
||
| 333 | 290 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 339 | } |
||
| 340 |
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: