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 | 1051 | public function setDocumentManager(DocumentManager $dm) |
|
| 64 | { |
||
| 65 | 1051 | $this->dm = $dm; |
|
| 66 | 1051 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the Configuration instance |
||
| 70 | * |
||
| 71 | * @param Configuration $config |
||
| 72 | */ |
||
| 73 | 1051 | public function setConfiguration(Configuration $config) |
|
| 74 | { |
||
| 75 | 1051 | $this->config = $config; |
|
| 76 | 1051 | } |
|
| 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 | 842 | protected function initialize() |
|
| 83 | { |
||
| 84 | 842 | $this->driver = $this->config->getMetadataDriverImpl(); |
|
| 85 | 842 | $this->evm = $this->dm->getEventManager(); |
|
| 86 | 842 | $this->initialized = true; |
|
| 87 | 842 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritDoc} |
||
| 91 | */ |
||
| 92 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
| 93 | { |
||
| 94 | return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritDoc} |
||
| 99 | */ |
||
| 100 | 352 | protected function getDriver() |
|
| 101 | { |
||
| 102 | 352 | return $this->driver; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | 838 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 109 | { |
||
| 110 | 838 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritDoc} |
||
| 114 | */ |
||
| 115 | 842 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
| 116 | { |
||
| 117 | 842 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | */ |
||
| 122 | 838 | protected function isEntity(ClassMetadataInterface $class) |
|
| 123 | { |
||
| 124 | 838 | return ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isAggregationResultDocument; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 842 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Validates the identifier mapping. |
||
| 194 | * |
||
| 195 | * @param ClassMetadata $class |
||
| 196 | * @throws MappingException |
||
| 197 | */ |
||
| 198 | 838 | protected function validateIdentifier($class) |
|
| 199 | { |
||
| 200 | 838 | if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isAggregationResultDocument) { |
|
| 201 | throw MappingException::identifierRequired($class->name); |
||
| 202 | } |
||
| 203 | 838 | } |
|
| 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 | 842 | protected function newClassMetadataInstance($className) |
|
| 212 | { |
||
| 213 | 842 | return new ClassMetadata($className); |
|
| 214 | } |
||
| 215 | |||
| 216 | 838 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
| 217 | { |
||
| 218 | 838 | $idGenOptions = $class->generatorOptions; |
|
| 219 | 838 | switch ($class->generatorType) { |
|
| 220 | 838 | case ClassMetadata::GENERATOR_TYPE_AUTO: |
|
| 221 | 773 | $class->setIdGenerator(new AutoGenerator()); |
|
| 222 | 773 | break; |
|
| 223 | 101 | case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
| 224 | 6 | $incrementGenerator = new IncrementGenerator(); |
|
| 225 | 6 | if (isset($idGenOptions['key'])) { |
|
| 226 | $incrementGenerator->setKey($idGenOptions['key']); |
||
| 227 | } |
||
| 228 | 6 | if (isset($idGenOptions['collection'])) { |
|
| 229 | $incrementGenerator->setCollection($idGenOptions['collection']); |
||
| 230 | } |
||
| 231 | 6 | $class->setIdGenerator($incrementGenerator); |
|
| 232 | 6 | break; |
|
| 233 | 95 | case ClassMetadata::GENERATOR_TYPE_UUID: |
|
| 234 | 4 | $uuidGenerator = new UuidGenerator(); |
|
| 235 | 4 | isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
|
| 236 | 4 | $class->setIdGenerator($uuidGenerator); |
|
| 237 | 4 | break; |
|
| 238 | 91 | case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
| 239 | 1 | $alnumGenerator = new AlnumGenerator(); |
|
| 240 | 1 | if (isset($idGenOptions['pad'])) { |
|
| 241 | $alnumGenerator->setPad($idGenOptions['pad']); |
||
| 242 | } |
||
| 243 | 1 | if (isset($idGenOptions['chars'])) { |
|
| 244 | 1 | $alnumGenerator->setChars($idGenOptions['chars']); |
|
| 245 | } elseif (isset($idGenOptions['awkwardSafe'])) { |
||
| 246 | $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
||
| 247 | } |
||
| 248 | |||
| 249 | 1 | $class->setIdGenerator($alnumGenerator); |
|
| 250 | 1 | break; |
|
| 251 | 90 | case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
|
| 252 | if (empty($idGenOptions['class'])) { |
||
| 253 | throw MappingException::missingIdGeneratorClass($class->name); |
||
| 254 | } |
||
| 255 | |||
| 256 | $customGenerator = new $idGenOptions['class']; |
||
| 257 | unset($idGenOptions['class']); |
||
| 258 | if ( ! $customGenerator instanceof AbstractIdGenerator) { |
||
| 259 | throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
||
| 260 | } |
||
| 261 | |||
| 262 | $methods = get_class_methods($customGenerator); |
||
| 263 | foreach ($idGenOptions as $name => $value) { |
||
| 264 | $method = 'set' . ucfirst($name); |
||
| 265 | if ( ! in_array($method, $methods)) { |
||
| 266 | throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
||
| 267 | } |
||
| 268 | |||
| 269 | $customGenerator->$method($value); |
||
| 270 | } |
||
| 271 | $class->setIdGenerator($customGenerator); |
||
| 272 | break; |
||
| 273 | 90 | case ClassMetadata::GENERATOR_TYPE_NONE; |
|
| 274 | 90 | break; |
|
| 275 | default: |
||
| 276 | throw new MappingException('Unknown generator type: ' . $class->generatorType); |
||
| 277 | } |
||
| 278 | 838 | } |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Adds inherited fields to the subclass mapping. |
||
| 282 | * |
||
| 283 | * @param ClassMetadata $subClass |
||
| 284 | * @param ClassMetadata $parentClass |
||
| 285 | */ |
||
| 286 | 299 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 287 | { |
||
| 288 | 299 | foreach ($parentClass->fieldMappings as $fieldName => $mapping) { |
|
| 289 | 75 | View Code Duplication | if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
| 290 | 69 | $mapping['inherited'] = $parentClass->name; |
|
| 291 | } |
||
| 292 | 75 | if ( ! isset($mapping['declared'])) { |
|
| 293 | 75 | $mapping['declared'] = $parentClass->name; |
|
| 294 | } |
||
| 295 | 75 | $subClass->addInheritedFieldMapping($mapping); |
|
| 296 | } |
||
| 297 | 299 | foreach ($parentClass->reflFields as $name => $field) { |
|
| 298 | 75 | $subClass->reflFields[$name] = $field; |
|
| 299 | } |
||
| 300 | 299 | } |
|
| 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 | 299 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 314 | { |
||
| 315 | 299 | foreach ($parentClass->associationMappings as $field => $mapping) { |
|
| 316 | 31 | if ($parentClass->isMappedSuperclass) { |
|
| 317 | 3 | $mapping['sourceDocument'] = $subClass->name; |
|
| 318 | } |
||
| 319 | |||
| 320 | 31 | View Code Duplication | if ( ! isset($mapping['inherited']) && ! $parentClass->isMappedSuperclass) { |
| 321 | 28 | $mapping['inherited'] = $parentClass->name; |
|
| 322 | } |
||
| 323 | 31 | if ( ! isset($mapping['declared'])) { |
|
| 324 | 31 | $mapping['declared'] = $parentClass->name; |
|
| 325 | } |
||
| 326 | 31 | $subClass->addInheritedAssociationMapping($mapping); |
|
| 327 | } |
||
| 328 | 299 | } |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Adds inherited indexes to the subclass mapping. |
||
| 332 | * |
||
| 333 | * @param ClassMetadata $subClass |
||
| 334 | * @param ClassMetadata $parentClass |
||
| 335 | */ |
||
| 336 | 299 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
| 337 | { |
||
| 338 | 299 | foreach ($parentClass->indexes as $index) { |
|
| 339 | 2 | $subClass->addIndex($index['keys'], $index['options']); |
|
| 340 | } |
||
| 341 | 299 | } |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Adds inherited shard key to the subclass mapping. |
||
| 345 | * |
||
| 346 | * @param ClassMetadata $subClass |
||
| 347 | * @param ClassMetadata $parentClass |
||
| 348 | */ |
||
| 349 | 299 | 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: