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 | 1125 | public function setDocumentManager(DocumentManager $dm) |
|
67 | |||
68 | /** |
||
69 | * Sets the Configuration instance |
||
70 | * |
||
71 | * @param Configuration $config |
||
72 | */ |
||
73 | 1125 | public function setConfiguration(Configuration $config) |
|
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 | 891 | protected function initialize() |
|
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
96 | |||
97 | /** |
||
98 | * {@inheritDoc} |
||
99 | */ |
||
100 | 354 | protected function getDriver() |
|
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | */ |
||
108 | 887 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
111 | |||
112 | /** |
||
113 | * {@inheritDoc} |
||
114 | */ |
||
115 | 891 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
118 | |||
119 | /** |
||
120 | * {@inheritDoc} |
||
121 | */ |
||
122 | 887 | protected function isEntity(ClassMetadataInterface $class) |
|
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | 891 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
131 | { |
||
132 | /** @var $class ClassMetadata */ |
||
133 | /** @var $parent ClassMetadata */ |
||
134 | 891 | if ($parent) { |
|
135 | 352 | $class->setInheritanceType($parent->inheritanceType); |
|
136 | 352 | $class->setDiscriminatorField($parent->discriminatorField); |
|
137 | 352 | $class->setDiscriminatorMap($parent->discriminatorMap); |
|
138 | 352 | $class->setDefaultDiscriminatorValue($parent->defaultDiscriminatorValue); |
|
139 | 352 | $class->setIdGeneratorType($parent->generatorType); |
|
140 | 352 | $this->addInheritedFields($class, $parent); |
|
141 | 352 | $this->addInheritedRelations($class, $parent); |
|
142 | 352 | $this->addInheritedIndexes($class, $parent); |
|
143 | 352 | $this->setInheritedShardKey($class, $parent); |
|
144 | 352 | $class->setIdentifier($parent->identifier); |
|
145 | 352 | $class->setVersioned($parent->isVersioned); |
|
146 | 352 | $class->setVersionField($parent->versionField); |
|
147 | 352 | $class->setLifecycleCallbacks($parent->lifecycleCallbacks); |
|
148 | 352 | $class->setAlsoLoadMethods($parent->alsoLoadMethods); |
|
149 | 352 | $class->setChangeTrackingPolicy($parent->changeTrackingPolicy); |
|
150 | 352 | $class->setWriteConcern($parent->writeConcern); |
|
151 | 352 | $class->setFile($parent->getFile()); |
|
152 | 352 | if ($parent->isMappedSuperclass) { |
|
153 | 292 | $class->setCustomRepositoryClass($parent->customRepositoryClassName); |
|
154 | } |
||
155 | } |
||
156 | |||
157 | // Invoke driver |
||
158 | try { |
||
159 | 891 | $this->driver->loadMetadataForClass($class->getName(), $class); |
|
160 | 6 | } catch (\ReflectionException $e) { |
|
161 | throw MappingException::reflectionFailure($class->getName(), $e); |
||
162 | } |
||
163 | |||
164 | 887 | $this->validateIdentifier($class); |
|
165 | |||
166 | 887 | if ($parent && $rootEntityFound && $parent->generatorType === $class->generatorType) { |
|
167 | 106 | if ($parent->generatorType) { |
|
168 | 106 | $class->setIdGeneratorType($parent->generatorType); |
|
169 | } |
||
170 | 106 | if ($parent->generatorOptions) { |
|
171 | $class->setIdGeneratorOptions($parent->generatorOptions); |
||
172 | } |
||
173 | 106 | if ($parent->idGenerator) { |
|
174 | 106 | $class->setIdGenerator($parent->idGenerator); |
|
175 | } |
||
176 | } else { |
||
177 | 887 | $this->completeIdGeneratorMapping($class); |
|
178 | } |
||
179 | |||
180 | 887 | if ($parent && $parent->isInheritanceTypeSingleCollection()) { |
|
181 | 91 | $class->setDatabase($parent->getDatabase()); |
|
182 | 91 | $class->setCollection($parent->getCollection()); |
|
183 | } |
||
184 | |||
185 | 887 | $class->setParentClasses($nonSuperclassParents); |
|
186 | |||
187 | 887 | View Code Duplication | if ($this->evm->hasListeners(Events::loadClassMetadata)) { |
188 | 2 | $eventArgs = new LoadClassMetadataEventArgs($class, $this->dm); |
|
189 | 2 | $this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs); |
|
190 | } |
||
191 | 887 | } |
|
192 | |||
193 | /** |
||
194 | * Validates the identifier mapping. |
||
195 | * |
||
196 | * @param ClassMetadata $class |
||
197 | * @throws MappingException |
||
198 | */ |
||
199 | 887 | protected function validateIdentifier($class) |
|
200 | { |
||
201 | 887 | if ( ! $class->identifier && ! $class->isMappedSuperclass && ! $class->isEmbeddedDocument && ! $class->isQueryResultDocument) { |
|
202 | throw MappingException::identifierRequired($class->name); |
||
203 | } |
||
204 | 887 | } |
|
205 | |||
206 | /** |
||
207 | * Creates a new ClassMetadata instance for the given class name. |
||
208 | * |
||
209 | * @param string $className |
||
210 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
211 | */ |
||
212 | 891 | protected function newClassMetadataInstance($className) |
|
216 | |||
217 | 887 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
218 | { |
||
219 | 887 | $idGenOptions = $class->generatorOptions; |
|
220 | 887 | switch ($class->generatorType) { |
|
221 | 887 | case ClassMetadata::GENERATOR_TYPE_AUTO: |
|
222 | 817 | $class->setIdGenerator(new AutoGenerator()); |
|
223 | 817 | break; |
|
224 | 149 | case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
225 | 8 | $incrementGenerator = new IncrementGenerator(); |
|
226 | 8 | if (isset($idGenOptions['key'])) { |
|
227 | $incrementGenerator->setKey($idGenOptions['key']); |
||
228 | } |
||
229 | 8 | if (isset($idGenOptions['collection'])) { |
|
230 | $incrementGenerator->setCollection($idGenOptions['collection']); |
||
231 | } |
||
232 | 8 | $class->setIdGenerator($incrementGenerator); |
|
233 | 8 | break; |
|
234 | 141 | case ClassMetadata::GENERATOR_TYPE_UUID: |
|
235 | 4 | $uuidGenerator = new UuidGenerator(); |
|
236 | 4 | isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
|
237 | 4 | $class->setIdGenerator($uuidGenerator); |
|
238 | 4 | break; |
|
239 | 137 | case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
240 | 1 | $alnumGenerator = new AlnumGenerator(); |
|
241 | 1 | if (isset($idGenOptions['pad'])) { |
|
242 | $alnumGenerator->setPad($idGenOptions['pad']); |
||
243 | } |
||
244 | 1 | if (isset($idGenOptions['chars'])) { |
|
245 | 1 | $alnumGenerator->setChars($idGenOptions['chars']); |
|
246 | } elseif (isset($idGenOptions['awkwardSafe'])) { |
||
247 | $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
||
248 | } |
||
249 | |||
250 | 1 | $class->setIdGenerator($alnumGenerator); |
|
251 | 1 | break; |
|
252 | 136 | case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
|
253 | if (empty($idGenOptions['class'])) { |
||
254 | throw MappingException::missingIdGeneratorClass($class->name); |
||
255 | } |
||
256 | |||
257 | $customGenerator = new $idGenOptions['class']; |
||
258 | unset($idGenOptions['class']); |
||
259 | if ( ! $customGenerator instanceof AbstractIdGenerator) { |
||
260 | throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
||
261 | } |
||
262 | |||
263 | $methods = get_class_methods($customGenerator); |
||
264 | foreach ($idGenOptions as $name => $value) { |
||
265 | $method = 'set' . ucfirst($name); |
||
266 | if ( ! in_array($method, $methods)) { |
||
267 | throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
||
268 | } |
||
269 | |||
270 | $customGenerator->$method($value); |
||
271 | } |
||
272 | $class->setIdGenerator($customGenerator); |
||
273 | break; |
||
274 | 136 | case ClassMetadata::GENERATOR_TYPE_NONE; |
|
275 | 136 | break; |
|
276 | default: |
||
277 | throw new MappingException('Unknown generator type: ' . $class->generatorType); |
||
278 | } |
||
279 | 887 | } |
|
280 | |||
281 | /** |
||
282 | * Adds inherited fields to the subclass mapping. |
||
283 | * |
||
284 | * @param ClassMetadata $subClass |
||
285 | * @param ClassMetadata $parentClass |
||
286 | */ |
||
287 | 352 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
302 | |||
303 | |||
304 | /** |
||
305 | * Adds inherited association mappings to the subclass mapping. |
||
306 | * |
||
307 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
||
308 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
||
309 | * |
||
310 | * @return void |
||
311 | * |
||
312 | * @throws MappingException |
||
313 | */ |
||
314 | 352 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
330 | |||
331 | /** |
||
332 | * Adds inherited indexes to the subclass mapping. |
||
333 | * |
||
334 | * @param ClassMetadata $subClass |
||
335 | * @param ClassMetadata $parentClass |
||
336 | */ |
||
337 | 352 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
343 | |||
344 | /** |
||
345 | * Adds inherited shard key to the subclass mapping. |
||
346 | * |
||
347 | * @param ClassMetadata $subClass |
||
348 | * @param ClassMetadata $parentClass |
||
349 | */ |
||
350 | 352 | private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
359 | } |
||
360 |
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: