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 |
||
25 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
26 | { |
||
27 | protected $cacheSalt = "\$MONGODBODMCLASSMETADATA"; |
||
28 | |||
29 | /** @var DocumentManager The DocumentManager instance */ |
||
30 | private $dm; |
||
31 | |||
32 | /** @var Configuration The Configuration instance */ |
||
33 | private $config; |
||
34 | |||
35 | /** @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver The used metadata driver. */ |
||
36 | private $driver; |
||
37 | |||
38 | /** @var \Doctrine\Common\EventManager The event manager instance */ |
||
39 | private $evm; |
||
40 | |||
41 | /** |
||
42 | * Sets the DocumentManager instance for this class. |
||
43 | * |
||
44 | * @param DocumentManager $dm The DocumentManager instance |
||
45 | */ |
||
46 | 1619 | public function setDocumentManager(DocumentManager $dm) |
|
50 | |||
51 | /** |
||
52 | * Sets the Configuration instance |
||
53 | * |
||
54 | * @param Configuration $config |
||
55 | */ |
||
56 | 1619 | public function setConfiguration(Configuration $config) |
|
60 | |||
61 | /** |
||
62 | * Lazy initialization of this stuff, especially the metadata driver, |
||
63 | * since these are not needed at all when a metadata cache is active. |
||
64 | */ |
||
65 | 1364 | protected function initialize() |
|
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | 887 | protected function getDriver() |
|
87 | |||
88 | /** |
||
89 | * {@inheritDoc} |
||
90 | */ |
||
91 | 1360 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | 1364 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | */ |
||
105 | 1360 | protected function isEntity(ClassMetadataInterface $class) |
|
109 | |||
110 | /** |
||
111 | * {@inheritDoc} |
||
112 | */ |
||
113 | 1364 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
175 | |||
176 | /** |
||
177 | * Validates the identifier mapping. |
||
178 | * |
||
179 | * @param ClassMetadata $class |
||
180 | * @throws MappingException |
||
181 | */ |
||
182 | 1360 | protected function validateIdentifier($class) |
|
188 | |||
189 | /** |
||
190 | * Creates a new ClassMetadata instance for the given class name. |
||
191 | * |
||
192 | * @param string $className |
||
193 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
194 | */ |
||
195 | 1364 | protected function newClassMetadataInstance($className) |
|
199 | |||
200 | 1360 | private function completeIdGeneratorMapping(ClassMetadata $class) |
|
201 | { |
||
202 | 1360 | $idGenOptions = $class->generatorOptions; |
|
203 | 1360 | switch ($class->generatorType) { |
|
204 | case ClassMetadata::GENERATOR_TYPE_AUTO: |
||
205 | 1291 | $class->setIdGenerator(new AutoGenerator()); |
|
206 | 1291 | break; |
|
207 | View Code Duplication | case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
208 | 9 | $incrementGenerator = new IncrementGenerator(); |
|
209 | 9 | if (isset($idGenOptions['key'])) { |
|
210 | $incrementGenerator->setKey($idGenOptions['key']); |
||
211 | } |
||
212 | 9 | if (isset($idGenOptions['collection'])) { |
|
213 | $incrementGenerator->setCollection($idGenOptions['collection']); |
||
214 | } |
||
215 | 9 | if (isset($idGenOptions['startingId'])) { |
|
216 | 1 | $incrementGenerator->setStartingId((int) $idGenOptions['startingId']); |
|
217 | } |
||
218 | 9 | $class->setIdGenerator($incrementGenerator); |
|
219 | 9 | break; |
|
220 | case ClassMetadata::GENERATOR_TYPE_UUID: |
||
221 | 4 | $uuidGenerator = new UuidGenerator(); |
|
222 | 4 | isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
|
223 | 4 | $class->setIdGenerator($uuidGenerator); |
|
224 | 4 | break; |
|
225 | View Code Duplication | case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
226 | 1 | $alnumGenerator = new AlnumGenerator(); |
|
227 | 1 | if (isset($idGenOptions['pad'])) { |
|
228 | $alnumGenerator->setPad($idGenOptions['pad']); |
||
229 | } |
||
230 | 1 | if (isset($idGenOptions['chars'])) { |
|
231 | 1 | $alnumGenerator->setChars($idGenOptions['chars']); |
|
232 | } elseif (isset($idGenOptions['awkwardSafe'])) { |
||
233 | $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
||
234 | } |
||
235 | |||
236 | 1 | $class->setIdGenerator($alnumGenerator); |
|
237 | 1 | break; |
|
238 | case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
||
239 | if (empty($idGenOptions['class'])) { |
||
240 | throw MappingException::missingIdGeneratorClass($class->name); |
||
241 | } |
||
242 | |||
243 | $customGenerator = new $idGenOptions['class']; |
||
244 | unset($idGenOptions['class']); |
||
245 | if ( ! $customGenerator instanceof AbstractIdGenerator) { |
||
246 | throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
||
247 | } |
||
248 | |||
249 | $methods = get_class_methods($customGenerator); |
||
250 | foreach ($idGenOptions as $name => $value) { |
||
251 | $method = 'set' . ucfirst($name); |
||
252 | if ( ! in_array($method, $methods)) { |
||
253 | throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
||
254 | } |
||
255 | |||
256 | $customGenerator->$method($value); |
||
257 | } |
||
258 | $class->setIdGenerator($customGenerator); |
||
259 | break; |
||
260 | case ClassMetadata::GENERATOR_TYPE_NONE; |
||
261 | 131 | break; |
|
262 | default: |
||
263 | throw new MappingException('Unknown generator type: ' . $class->generatorType); |
||
264 | } |
||
265 | 1360 | } |
|
266 | |||
267 | /** |
||
268 | * Adds inherited fields to the subclass mapping. |
||
269 | * |
||
270 | * @param ClassMetadata $subClass |
||
271 | * @param ClassMetadata $parentClass |
||
272 | */ |
||
273 | 885 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
288 | |||
289 | |||
290 | /** |
||
291 | * Adds inherited association mappings to the subclass mapping. |
||
292 | * |
||
293 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $subClass |
||
294 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentClass |
||
295 | * |
||
296 | * @return void |
||
297 | * |
||
298 | * @throws MappingException |
||
299 | */ |
||
300 | 885 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
316 | |||
317 | /** |
||
318 | * Adds inherited indexes to the subclass mapping. |
||
319 | * |
||
320 | * @param ClassMetadata $subClass |
||
321 | * @param ClassMetadata $parentClass |
||
322 | */ |
||
323 | 885 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
329 | |||
330 | /** |
||
331 | * Adds inherited shard key to the subclass mapping. |
||
332 | * |
||
333 | * @param ClassMetadata $subClass |
||
334 | * @param ClassMetadata $parentClass |
||
335 | */ |
||
336 | 885 | private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
345 | } |
||
346 |
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: