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