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 |
||
| 33 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
| 34 | { |
||
| 35 | /** @var string */ |
||
| 36 | protected $cacheSalt = '$MONGODBODMCLASSMETADATA'; |
||
| 37 | |||
| 38 | /** @var DocumentManager The DocumentManager instance */ |
||
| 39 | private $dm; |
||
| 40 | |||
| 41 | /** @var Configuration The Configuration instance */ |
||
| 42 | private $config; |
||
| 43 | |||
| 44 | /** @var MappingDriver The used metadata driver. */ |
||
| 45 | private $driver; |
||
| 46 | |||
| 47 | /** @var EventManager The event manager instance */ |
||
| 48 | private $evm; |
||
| 49 | |||
| 50 | 1695 | public function setDocumentManager(DocumentManager $dm) : void |
|
| 51 | { |
||
| 52 | 1695 | $this->dm = $dm; |
|
| 53 | 1695 | } |
|
| 54 | |||
| 55 | 1695 | public function setConfiguration(Configuration $config) : void |
|
| 56 | { |
||
| 57 | 1695 | $this->config = $config; |
|
| 58 | 1695 | } |
|
| 59 | |||
| 60 | 1431 | public function getMetadataFor($className) |
|
| 61 | { |
||
| 62 | 1431 | return parent::getMetadataFor($this->dm->getClassNameResolver()->getRealClass($className)); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Lazy initialization of this stuff, especially the metadata driver, |
||
| 67 | * since these are not needed at all when a metadata cache is active. |
||
| 68 | */ |
||
| 69 | 1430 | protected function initialize() : void |
|
| 70 | { |
||
| 71 | 1430 | $this->driver = $this->config->getMetadataDriverImpl(); |
|
| 72 | 1430 | $this->evm = $this->dm->getEventManager(); |
|
| 73 | 1430 | $this->initialized = true; |
|
| 74 | 1430 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritDoc} |
||
| 78 | */ |
||
| 79 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) : string |
||
| 80 | { |
||
| 81 | return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | 915 | protected function getDriver() |
|
| 88 | { |
||
| 89 | 915 | return $this->driver; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | 1426 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) : void |
|
| 96 | { |
||
| 97 | 1426 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritDoc} |
||
| 101 | */ |
||
| 102 | 1430 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) : void |
|
| 103 | { |
||
| 104 | 1430 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritDoc} |
||
| 108 | */ |
||
| 109 | 1426 | protected function isEntity(ClassMetadataInterface $class) : bool |
|
| 110 | { |
||
| 111 | 1426 | assert($class instanceof ClassMetadata); |
|
| 112 | 1426 | return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument; |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritDoc} |
||
| 117 | */ |
||
| 118 | 1430 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = []) : void |
|
| 119 | { |
||
| 120 | 1430 | assert($class instanceof ClassMetadata); |
|
| 121 | 1430 | if ($parent instanceof ClassMetadata) { |
|
| 122 | 913 | $class->setInheritanceType($parent->inheritanceType); |
|
| 123 | 913 | $class->setDiscriminatorField($parent->discriminatorField); |
|
| 124 | 913 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
| 125 | 913 | $class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
| 126 | 913 | $class->setIdGeneratorType($parent->generatorType); |
|
| 127 | 913 | $this->addInheritedFields($class, $parent); |
|
| 128 | 913 | $this->addInheritedRelations($class, $parent); |
|
| 129 | 913 | $this->addInheritedIndexes($class, $parent); |
|
| 130 | 913 | $this->setInheritedShardKey($class, $parent); |
|
| 131 | 913 | $class->setIdentifier($parent->identifier); |
|
| 132 | 913 | $class->setVersioned($parent->isVersioned); |
|
| 133 | 913 | $class->setVersionField($parent->versionField); |
|
| 134 | 913 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
| 135 | 913 | $class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
| 136 | 913 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
| 137 | 913 | $class->setReadPreference($parent->readPreference, $parent->readPreferenceTags); |
|
| 138 | 913 | $class->setWriteConcern($parent->writeConcern); |
|
| 139 | |||
| 140 | 913 | if ($parent->isMappedSuperclass) { |
|
| 141 | 849 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
| 142 | } |
||
| 143 | |||
| 144 | 913 | if ($parent->isFile) { |
|
| 145 | 1 | $class->isFile = true; |
|
| 146 | 1 | $class->setBucketName($parent->bucketName); |
|
| 147 | |||
| 148 | 1 | if ($parent->chunkSizeBytes !== null) { |
|
| 149 | 1 | $class->setChunkSizeBytes($parent->chunkSizeBytes); |
|
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | // Invoke driver |
||
| 155 | try { |
||
| 156 | 1430 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
|
|
|||
| 157 | 6 | } catch (ReflectionException $e) { |
|
| 158 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
| 159 | } |
||
| 160 | |||
| 161 | 1426 | $this->validateIdentifier($class); |
|
| 162 | |||
| 163 | 1426 | if ($parent instanceof ClassMetadata && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
| 164 | 121 | if ($parent->generatorType) { |
|
| 165 | 121 | $class->setIdGeneratorType($parent->generatorType); |
|
| 166 | } |
||
| 167 | 121 | if ($parent->generatorOptions) { |
|
| 168 | $class->setIdGeneratorOptions($parent->generatorOptions); |
||
| 169 | } |
||
| 170 | 121 | if ($parent->idGenerator) { |
|
| 171 | 121 | $class->setIdGenerator($parent->idGenerator); |
|
| 172 | } |
||
| 173 | } else { |
||
| 174 | 1426 | $this->completeIdGeneratorMapping($class); |
|
| 175 | } |
||
| 176 | |||
| 177 | 1426 | if ($parent instanceof ClassMetadata && $parent->isInheritanceTypeSingleCollection()) { |
|
| 178 | 107 | $class->setDatabase($parent->getDatabase()); |
|
| 179 | 107 | $class->setCollection($parent->getCollection()); |
|
| 180 | } |
||
| 181 | |||
| 182 | 1426 | $class->setParentClasses($nonSuperclassParents); |
|
| 183 | |||
| 184 | 1426 | if (! $this->evm->hasListeners(Events::loadClassMetadata)) { |
|
| 185 | 1424 | return; |
|
| 186 | } |
||
| 187 | |||
| 188 | 2 | $eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
|
| 189 | 2 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
| 190 | 2 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Validates the identifier mapping. |
||
| 194 | * |
||
| 195 | * @throws MappingException |
||
| 196 | */ |
||
| 197 | 1426 | protected function validateIdentifier(ClassMetadata $class) : void |
|
| 198 | { |
||
| 199 | 1426 | if (! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument) { |
|
| 200 | throw MappingException::identifierRequired($class->name); |
||
| 201 | } |
||
| 202 | 1426 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | 1430 | protected function newClassMetadataInstance($className) : ClassMetadata |
|
| 208 | { |
||
| 209 | 1430 | return new ClassMetadata($className); |
|
| 210 | } |
||
| 211 | |||
| 212 | 1426 | private function completeIdGeneratorMapping(ClassMetadata $class) : void |
|
| 213 | { |
||
| 214 | 1426 | $idGenOptions = $class->generatorOptions; |
|
| 215 | 1426 | switch ($class->generatorType) { |
|
| 216 | case ClassMetadata::GENERATOR_TYPE_AUTO: |
||
| 217 | 1358 | $class->setIdGenerator(new AutoGenerator()); |
|
| 218 | 1358 | break; |
|
| 219 | View Code Duplication | case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
| 220 | 9 | $incrementGenerator = new IncrementGenerator(); |
|
| 221 | 9 | if (isset($idGenOptions['key'])) { |
|
| 222 | $incrementGenerator->setKey((string) $idGenOptions['key']); |
||
| 223 | } |
||
| 224 | 9 | if (isset($idGenOptions['collection'])) { |
|
| 225 | $incrementGenerator->setCollection((string) $idGenOptions['collection']); |
||
| 226 | } |
||
| 227 | 9 | if (isset($idGenOptions['startingId'])) { |
|
| 228 | 1 | $incrementGenerator->setStartingId((int) $idGenOptions['startingId']); |
|
| 229 | } |
||
| 230 | 9 | $class->setIdGenerator($incrementGenerator); |
|
| 231 | 9 | break; |
|
| 232 | case ClassMetadata::GENERATOR_TYPE_UUID: |
||
| 233 | 4 | $uuidGenerator = new UuidGenerator(); |
|
| 234 | 4 | isset($idGenOptions['salt']) && $uuidGenerator->setSalt((string) $idGenOptions['salt']); |
|
| 235 | 4 | $class->setIdGenerator($uuidGenerator); |
|
| 236 | 4 | break; |
|
| 237 | View Code Duplication | case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
| 238 | 1 | $alnumGenerator = new AlnumGenerator(); |
|
| 239 | 1 | if (isset($idGenOptions['pad'])) { |
|
| 240 | $alnumGenerator->setPad((int) $idGenOptions['pad']); |
||
| 241 | } |
||
| 242 | 1 | if (isset($idGenOptions['chars'])) { |
|
| 243 | 1 | $alnumGenerator->setChars((string) $idGenOptions['chars']); |
|
| 244 | } elseif (isset($idGenOptions['awkwardSafe'])) { |
||
| 245 | $alnumGenerator->setAwkwardSafeMode((bool) $idGenOptions['awkwardSafe']); |
||
| 246 | } |
||
| 247 | |||
| 248 | 1 | $class->setIdGenerator($alnumGenerator); |
|
| 249 | 1 | break; |
|
| 250 | case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
||
| 251 | if (empty($idGenOptions['class'])) { |
||
| 252 | throw MappingException::missingIdGeneratorClass($class->name); |
||
| 253 | } |
||
| 254 | |||
| 255 | $customGenerator = new $idGenOptions['class'](); |
||
| 256 | unset($idGenOptions['class']); |
||
| 257 | if (! $customGenerator instanceof AbstractIdGenerator) { |
||
| 258 | throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
||
| 259 | } |
||
| 260 | |||
| 261 | $methods = get_class_methods($customGenerator); |
||
| 262 | foreach ($idGenOptions as $name => $value) { |
||
| 263 | $method = 'set' . ucfirst($name); |
||
| 264 | if (! in_array($method, $methods)) { |
||
| 265 | throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
||
| 266 | } |
||
| 267 | |||
| 268 | $customGenerator->$method($value); |
||
| 269 | } |
||
| 270 | $class->setIdGenerator($customGenerator); |
||
| 271 | break; |
||
| 272 | case ClassMetadata::GENERATOR_TYPE_NONE: |
||
| 273 | 150 | break; |
|
| 274 | default: |
||
| 275 | throw new MappingException('Unknown generator type: ' . $class->generatorType); |
||
| 276 | } |
||
| 277 | 1426 | } |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Adds inherited fields to the subclass mapping. |
||
| 281 | */ |
||
| 282 | 913 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) : void |
|
| 283 | { |
||
| 284 | 913 | foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
|
| 285 | 139 | View Code Duplication | if (! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
| 286 | 133 | $mapping['inherited'] = $parentClass->name; |
|
| 287 | } |
||
| 288 | 139 | if (! isset($mapping['declared'])) { |
|
| 289 | 139 | $mapping['declared'] = $parentClass->name; |
|
| 290 | } |
||
| 291 | 139 | $subClass->addInheritedFieldMapping($mapping); |
|
| 292 | } |
||
| 293 | 913 | foreach ($parentClass->reflFields as $name => $field) { |
|
| 294 | 139 | $subClass->reflFields[$name] = $field; |
|
| 295 | } |
||
| 296 | 913 | } |
|
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Adds inherited association mappings to the subclass mapping. |
||
| 301 | * |
||
| 302 | * @throws MappingException |
||
| 303 | */ |
||
| 304 | 913 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) : void |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Adds inherited indexes to the subclass mapping. |
||
| 323 | */ |
||
| 324 | 913 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) : void |
|
| 325 | { |
||
| 326 | 913 | foreach ($parentClass->indexes as $index) { |
|
| 327 | 65 | $subClass->addIndex($index['keys'], $index['options']); |
|
| 328 | } |
||
| 329 | 913 | } |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Adds inherited shard key to the subclass mapping. |
||
| 333 | */ |
||
| 334 | 913 | private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) : void |
|
| 335 | { |
||
| 345 | } |
||
| 346 |