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 |
||
40 | class ClassMetadataFactory extends AbstractClassMetadataFactory |
||
41 | { |
||
42 | protected $cacheSalt = "\$MONGODBODMCLASSMETADATA"; |
||
43 | |||
44 | /** @var DocumentManager The DocumentManager instance */ |
||
45 | private $dm; |
||
46 | |||
47 | /** @var Configuration The Configuration instance */ |
||
48 | private $config; |
||
49 | |||
50 | /** @var \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver The used metadata driver. */ |
||
51 | private $driver; |
||
52 | |||
53 | /** @var \Doctrine\Common\EventManager The event manager instance */ |
||
54 | private $evm; |
||
55 | |||
56 | /** |
||
57 | * Sets the DocumentManager instance for this class. |
||
58 | * |
||
59 | * @param DocumentManager $dm The DocumentManager instance |
||
60 | */ |
||
61 | 968 | public function setDocumentManager(DocumentManager $dm) |
|
65 | |||
66 | /** |
||
67 | * Sets the Configuration instance |
||
68 | * |
||
69 | * @param Configuration $config |
||
70 | */ |
||
71 | 968 | public function setConfiguration(Configuration $config) |
|
75 | |||
76 | /** |
||
77 | * Lazy initialization of this stuff, especially the metadata driver, |
||
78 | * since these are not needed at all when a metadata cache is active. |
||
79 | */ |
||
80 | 775 | protected function initialize() |
|
86 | |||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | 2 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
|
91 | { |
||
92 | return $this->config->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName; |
||
93 | 2 | } |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | 293 | protected function getDriver() |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 772 | protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
109 | |||
110 | /** |
||
111 | * {@inheritDoc} |
||
112 | */ |
||
113 | 775 | protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService) |
|
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | 772 | protected function isEntity(ClassMetadataInterface $class) |
|
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | 775 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents = array()) |
|
188 | |||
189 | /** |
||
190 | * Validates the identifier mapping. |
||
191 | * |
||
192 | * @param ClassMetadata $class |
||
193 | * @throws MappingException |
||
194 | */ |
||
195 | 772 | protected function validateIdentifier($class) |
|
201 | |||
202 | /** |
||
203 | * Creates a new ClassMetadata instance for the given class name. |
||
204 | * |
||
205 | * @param string $className |
||
206 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
207 | */ |
||
208 | 775 | protected function newClassMetadataInstance($className) |
|
212 | |||
213 | 772 | private function completeIdGeneratorMapping(ClassMetadataInfo $class) |
|
214 | { |
||
215 | 772 | $idGenOptions = $class->generatorOptions; |
|
216 | 772 | switch ($class->generatorType) { |
|
217 | 772 | case ClassMetadata::GENERATOR_TYPE_AUTO: |
|
218 | 706 | $class->setIdGenerator(new \Doctrine\ODM\MongoDB\Id\AutoGenerator()); |
|
219 | 706 | break; |
|
220 | 110 | case ClassMetadata::GENERATOR_TYPE_INCREMENT: |
|
221 | 6 | $incrementGenerator = new \Doctrine\ODM\MongoDB\Id\IncrementGenerator(); |
|
222 | 6 | if (isset($idGenOptions['key'])) { |
|
223 | $incrementGenerator->setKey($idGenOptions['key']); |
||
224 | } |
||
225 | 6 | if (isset($idGenOptions['collection'])) { |
|
226 | $incrementGenerator->setCollection($idGenOptions['collection']); |
||
227 | } |
||
228 | 6 | $class->setIdGenerator($incrementGenerator); |
|
229 | 6 | break; |
|
230 | 104 | case ClassMetadata::GENERATOR_TYPE_UUID: |
|
231 | 4 | $uuidGenerator = new \Doctrine\ODM\MongoDB\Id\UuidGenerator(); |
|
232 | 4 | isset($idGenOptions['salt']) && $uuidGenerator->setSalt($idGenOptions['salt']); |
|
233 | 4 | $class->setIdGenerator($uuidGenerator); |
|
234 | 4 | break; |
|
235 | 100 | case ClassMetadata::GENERATOR_TYPE_ALNUM: |
|
236 | 1 | $alnumGenerator = new \Doctrine\ODM\MongoDB\Id\AlnumGenerator(); |
|
237 | 1 | if (isset($idGenOptions['pad'])) { |
|
238 | $alnumGenerator->setPad($idGenOptions['pad']); |
||
239 | } |
||
240 | 1 | if (isset($idGenOptions['chars'])) { |
|
241 | 1 | $alnumGenerator->setChars($idGenOptions['chars']); |
|
242 | 1 | } elseif (isset($idGenOptions['awkwardSafe'])) { |
|
243 | $alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']); |
||
244 | } |
||
245 | |||
246 | 1 | $class->setIdGenerator($alnumGenerator); |
|
247 | 1 | break; |
|
248 | 99 | case ClassMetadata::GENERATOR_TYPE_CUSTOM: |
|
249 | if (empty($idGenOptions['class'])) { |
||
250 | throw MappingException::missingIdGeneratorClass($class->name); |
||
251 | } |
||
252 | |||
253 | $customGenerator = new $idGenOptions['class']; |
||
254 | unset($idGenOptions['class']); |
||
255 | if ( ! $customGenerator instanceof \Doctrine\ODM\MongoDB\Id\AbstractIdGenerator) { |
||
256 | throw MappingException::classIsNotAValidGenerator(get_class($customGenerator)); |
||
257 | } |
||
258 | |||
259 | $methods = get_class_methods($customGenerator); |
||
260 | foreach ($idGenOptions as $name => $value) { |
||
261 | $method = 'set' . ucfirst($name); |
||
262 | if ( ! in_array($method, $methods)) { |
||
263 | throw MappingException::missingGeneratorSetter(get_class($customGenerator), $name); |
||
264 | } |
||
265 | |||
266 | $customGenerator->$method($value); |
||
267 | } |
||
268 | $class->setIdGenerator($customGenerator); |
||
269 | break; |
||
270 | 99 | case ClassMetadata::GENERATOR_TYPE_NONE; |
|
271 | 99 | break; |
|
272 | default: |
||
273 | throw new MappingException("Unknown generator type: " . $class->generatorType); |
||
274 | 772 | } |
|
275 | 772 | } |
|
276 | |||
277 | /** |
||
278 | * Adds inherited fields to the subclass mapping. |
||
279 | * |
||
280 | * @param ClassMetadata $subClass |
||
281 | * @param ClassMetadata $parentClass |
||
282 | */ |
||
283 | 291 | private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
298 | |||
299 | |||
300 | /** |
||
301 | * Adds inherited association mappings to the subclass mapping. |
||
302 | * |
||
303 | * @param \Doctrine\ORM\Mapping\ClassMetadata $subClass |
||
304 | * @param \Doctrine\ORM\Mapping\ClassMetadata $parentClass |
||
305 | * |
||
306 | * @return void |
||
307 | * |
||
308 | * @throws MappingException |
||
309 | */ |
||
310 | 291 | private function addInheritedRelations(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
326 | |||
327 | /** |
||
328 | * Adds inherited indexes to the subclass mapping. |
||
329 | * |
||
330 | * @param ClassMetadata $subClass |
||
331 | * @param ClassMetadata $parentClass |
||
332 | */ |
||
333 | 291 | private function addInheritedIndexes(ClassMetadata $subClass, ClassMetadata $parentClass) |
|
339 | } |
||
340 |
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: