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 | 964 | public function setDocumentManager(DocumentManager $dm) |
|
| 64 | { |
||
| 65 | 964 | $this->dm = $dm; |
|
| 66 | 964 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the Configuration instance |
||
| 70 | * |
||
| 71 | * @param Configuration $config |
||
| 72 | */ |
||
| 73 | 964 | public function setConfiguration(Configuration $config) |
|
| 74 | { |
||
| 75 | 964 | $this->config = $config; |
|
| 76 | 964 | } |
|
| 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 | 827 | protected function initialize() |
|
| 83 | { |
||
| 84 | 827 | $this->driver = $this->config->getMetadataDriverImpl(); |
|
| 85 | 827 | $this->evm = $this->dm->getEventManager(); |
|
| 86 | 827 | $this->initialized = true; |
|
| 87 | 827 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritDoc} |
||
| 91 | */ |
||
| 92 | 2 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
| 93 | { |
||
| 94 | 2 | return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritDoc} |
||
| 99 | */ |
||
| 100 | 332 | protected function getDriver() |
|
| 101 | { |
||
| 102 | 332 | return $this->driver; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | 824 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 109 | { |
||
| 110 | 824 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritDoc} |
||
| 114 | */ |
||
| 115 | 827 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 116 | { |
||
| 117 | 827 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | */ |
||
| 122 | 824 | protected function isEntity(ClassMetadataInterface $class) |
|
| 123 | { |
||
| 124 | 824 | return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isAggregationResultDocument; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 827 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
| 131 | 2 | { |
|
| 132 | /** @var $class ClassMetadata */ |
||
| 133 | /** @var $parent ClassMetadata */ |
||
| 134 | 827 | if ($parent) { |
|
| 135 | 331 | $class->setInheritanceType($parent->inheritanceType); |
|
| 136 | 331 | $class->setDiscriminatorField($parent->discriminatorField); |
|
| 137 | 331 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
| 138 | 331 | $class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
| 139 | 331 | $class->setIdGeneratorType($parent->generatorType); |
|
| 140 | 331 | $this->addInheritedFields($class, $parent); |
|
| 141 | 331 | $this->addInheritedRelations($class, $parent); |
|
| 142 | 331 | $this->addInheritedIndexes($class, $parent); |
|
| 143 | 331 | $class->setIdentifier($parent->identifier); |
|
| 144 | 331 | $class->setVersioned($parent->isVersioned); |
|
| 145 | 331 | $class->setVersionField($parent->versionField); |
|
| 146 | 331 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
| 147 | 331 | $class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
| 148 | 331 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
| 149 | 331 | $class->setFile($parent->getFile()); |
|
| 150 | 331 | if ($parent->isMappedSuperclass) { |
|
| 151 | 278 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
| 152 | 278 | } |
|
| 153 | 331 | } |
|
| 154 | |||
| 155 | // Invoke driver |
||
| 156 | try { |
||
| 157 | 827 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
| 158 | 827 | } catch (\ReflectionException $e) { |
|
| 159 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
| 160 | } |
||
| 161 | |||
| 162 | 824 | $this->validateIdentifier($class); |
|
| 163 | |||
| 164 | 824 | if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
| 165 | 92 | if ($parent->generatorType) { |
|
| 166 | 92 | $class->setIdGeneratorType($parent->generatorType); |
|
| 167 | 92 | } |
|
| 168 | 92 | if ($parent->generatorOptions) { |
|
| 169 | $class->setIdGeneratorOptions($parent->generatorOptions); |
||
| 170 | } |
||
| 171 | 92 | if ($parent->idGenerator) { |
|
| 172 | 92 | $class->setIdGenerator($parent->idGenerator); |
|
| 173 | 92 | } |
|
| 174 | 92 | } else { |
|
| 175 | 824 | $this->completeIdGeneratorMapping($class); |
|
| 176 | } |
||
| 177 | |||
| 178 | 824 | if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
|
| 179 | 79 | $class->setDatabase($parent->getDatabase()); |
|
| 180 | 79 | $class->setCollection($parent->getCollection()); |
|
| 181 | 79 | } |
|
| 182 | |||
| 183 | 824 | $class->setParentClasses($nonSuperclassParents); |
|
| 184 | |||
| 185 | 824 | View Code Duplication | if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
| 186 | 2 | $eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
|
| 187 | 2 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
| 188 | 2 | } |
|
| 189 | 824 | } |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Validates the identifier mapping. |
||
| 193 | * |
||
| 194 | * @param ClassMetadata $class |
||
| 195 | * @throws MappingException |
||
| 196 | */ |
||
| 197 | 824 | protected function validateIdentifier($class) |
|
| 198 | { |
||
| 199 | 824 | if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isAggregationResultDocument) { |
|
| 200 | throw MappingException::identifierRequired($class->name); |
||
| 201 | } |
||
| 202 | 824 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Creates a new ClassMetadata instance for the given class name. |
||
| 206 | * |
||
| 207 | * @param string $className |
||
| 208 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
| 209 | */ |
||
| 210 | 827 | protected function newClassMetadataInstance($className) |
|
| 211 | { |
||
| 212 | 827 | return new ClassMetadata($className); |
|
| 213 | } |
||
| 214 | |||
| 215 | 824 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Adds inherited fields to the subclass mapping. |
||
| 281 | * |
||
| 282 | * @param ClassMetadata $subClass |
||
| 283 | * @param ClassMetadata $parentClass |
||
| 284 | */ |
||
| 285 | 331 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 286 | { |
||
| 287 | 331 | foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
|
| 288 | 108 | View Code Duplication | if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
| 289 | 103 | $mapping['inherited'] = $parentClass->name; |
|
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Adds inherited association mappings to the subclass mapping. |
||
| 304 | * |
||
| 305 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
||
| 306 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
||
| 307 | * |
||
| 308 | * @return void |
||
| 309 | * |
||
| 310 | * @throws MappingException |
||
| 311 | */ |
||
| 312 | 331 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Adds inherited indexes to the subclass mapping. |
||
| 331 | * |
||
| 332 | * @param ClassMetadata $subClass |
||
| 333 | * @param ClassMetadata $parentClass |
||
| 334 | */ |
||
| 335 | 331 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 341 | } |
||
| 342 |
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: