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