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 XmlDriver 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 XmlDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class XmlDriver extends FileDriver |
||
| 35 | { |
||
| 36 | const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritDoc} |
||
| 40 | */ |
||
| 41 | 10 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritDoc} |
||
| 48 | */ |
||
| 49 | 6 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 50 | { |
||
| 51 | /* @var $class ClassMetadataInfo */ |
||
| 52 | /* @var $xmlRoot \SimpleXMLElement */ |
||
| 53 | 6 | $xmlRoot = $this->getElement($className); |
|
| 54 | 6 | if ( ! $xmlRoot) { |
|
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | 6 | if ($xmlRoot->getName() == 'document') { |
|
| 59 | 6 | if (isset($xmlRoot['repository-class'])) { |
|
| 60 | $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
||
| 61 | } |
||
| 62 | 6 | View Code Duplication | } elseif ($xmlRoot->getName() == 'mapped-superclass') { |
|
|
|||
| 63 | 2 | $class->setCustomRepositoryClass( |
|
| 64 | 2 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
| 65 | 2 | ); |
|
| 66 | 2 | $class->isMappedSuperclass = true; |
|
| 67 | 3 | } elseif ($xmlRoot->getName() == 'embedded-document') { |
|
| 68 | 1 | $class->isEmbeddedDocument = true; |
|
| 69 | 2 | } |
|
| 70 | 6 | if (isset($xmlRoot['db'])) { |
|
| 71 | 2 | $class->setDatabase((string) $xmlRoot['db']); |
|
| 72 | 2 | } |
|
| 73 | 6 | if (isset($xmlRoot['collection'])) { |
|
| 74 | 5 | if (isset($xmlRoot['capped-collection'])) { |
|
| 75 | $config = array('name' => (string) $xmlRoot['collection']); |
||
| 76 | $config['capped'] = (bool) $xmlRoot['capped-collection']; |
||
| 77 | if (isset($xmlRoot['capped-collection-max'])) { |
||
| 78 | $config['max'] = (int) $xmlRoot['capped-collection-max']; |
||
| 79 | } |
||
| 80 | if (isset($xmlRoot['capped-collection-size'])) { |
||
| 81 | $config['size'] = (int) $xmlRoot['capped-collection-size']; |
||
| 82 | } |
||
| 83 | $class->setCollection($config); |
||
| 84 | } else { |
||
| 85 | 5 | $class->setCollection((string) $xmlRoot['collection']); |
|
| 86 | } |
||
| 87 | 5 | } |
|
| 88 | 6 | if (isset($xmlRoot['inheritance-type'])) { |
|
| 89 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
||
| 90 | $class->setInheritanceType(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType)); |
||
| 91 | } |
||
| 92 | 6 | View Code Duplication | if (isset($xmlRoot['change-tracking-policy'])) { |
| 93 | 2 | $class->setChangeTrackingPolicy(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
| 94 | 2 | } |
|
| 95 | 6 | if (isset($xmlRoot->{'discriminator-field'})) { |
|
| 96 | 1 | $discrField = $xmlRoot->{'discriminator-field'}; |
|
| 97 | /* XSD only allows for "name", which is consistent with association |
||
| 98 | * configurations, but fall back to "fieldName" for BC. |
||
| 99 | */ |
||
| 100 | 1 | $class->setDiscriminatorField( |
|
| 101 | 1 | isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName'] |
|
| 102 | 1 | ); |
|
| 103 | 1 | } |
|
| 104 | 6 | View Code Duplication | if (isset($xmlRoot->{'discriminator-map'})) { |
| 105 | 1 | $map = array(); |
|
| 106 | 1 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) { |
|
| 107 | 1 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
|
| 108 | 1 | } |
|
| 109 | 1 | $class->setDiscriminatorMap($map); |
|
| 110 | 1 | } |
|
| 111 | 6 | if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
| 112 | 1 | $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
|
| 113 | 1 | } |
|
| 114 | 6 | if (isset($xmlRoot->{'indexes'})) { |
|
| 115 | 1 | foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
| 116 | 1 | $this->addIndex($class, $index); |
|
| 117 | 1 | } |
|
| 118 | 1 | } |
|
| 119 | 6 | if (isset($xmlRoot['require-indexes'])) { |
|
| 120 | 1 | $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']); |
|
| 121 | 1 | } |
|
| 122 | 6 | if (isset($xmlRoot['slave-okay'])) { |
|
| 123 | 1 | $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']); |
|
| 124 | 1 | } |
|
| 125 | 6 | if (isset($xmlRoot->field)) { |
|
| 126 | 6 | foreach ($xmlRoot->field as $field) { |
|
| 127 | 6 | $mapping = array(); |
|
| 128 | 6 | $attributes = $field->attributes(); |
|
| 129 | 6 | foreach ($attributes as $key => $value) { |
|
| 130 | 6 | $mapping[$key] = (string) $value; |
|
| 131 | 6 | $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance'); |
|
| 132 | 6 | if (in_array($key, $booleanAttributes)) { |
|
| 133 | 6 | $mapping[$key] = ('true' === $mapping[$key]); |
|
| 134 | 6 | } |
|
| 135 | 6 | } |
|
| 136 | 6 | if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) { |
|
| 137 | 3 | $mapping['options'] = array(); |
|
| 138 | 3 | if (isset($field->{'id-generator-option'})) { |
|
| 139 | 1 | foreach ($field->{'id-generator-option'} as $generatorOptions) { |
|
| 140 | 1 | $attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
| 141 | 1 | if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) { |
|
| 142 | 1 | $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
| 143 | 1 | } |
|
| 144 | 1 | } |
|
| 145 | 1 | } |
|
| 146 | 3 | } |
|
| 147 | |||
| 148 | 6 | if (isset($attributes['not-saved'])) { |
|
| 149 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 150 | } |
||
| 151 | |||
| 152 | 6 | if (isset($attributes['also-load'])) { |
|
| 153 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 154 | 6 | } elseif (isset($attributes['version'])) { |
|
| 155 | 1 | $mapping['version'] = ('true' === (string) $attributes['version']); |
|
| 156 | 6 | } elseif (isset($attributes['lock'])) { |
|
| 157 | 1 | $mapping['lock'] = ('true' === (string) $attributes['lock']); |
|
| 158 | 1 | } |
|
| 159 | |||
| 160 | 6 | $this->addFieldMapping($class, $mapping); |
|
| 161 | 6 | } |
|
| 162 | 6 | } |
|
| 163 | 6 | if (isset($xmlRoot->{'embed-one'})) { |
|
| 164 | 2 | foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
| 165 | 2 | $this->addEmbedMapping($class, $embed, 'one'); |
|
| 166 | 2 | } |
|
| 167 | 2 | } |
|
| 168 | 6 | if (isset($xmlRoot->{'embed-many'})) { |
|
| 169 | 2 | foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
| 170 | 2 | $this->addEmbedMapping($class, $embed, 'many'); |
|
| 171 | 2 | } |
|
| 172 | 2 | } |
|
| 173 | 6 | if (isset($xmlRoot->{'reference-many'})) { |
|
| 174 | 3 | foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
| 175 | 3 | $this->addReferenceMapping($class, $reference, 'many'); |
|
| 176 | 3 | } |
|
| 177 | 3 | } |
|
| 178 | 6 | if (isset($xmlRoot->{'reference-one'})) { |
|
| 179 | 3 | foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
| 180 | 3 | $this->addReferenceMapping($class, $reference, 'one'); |
|
| 181 | 3 | } |
|
| 182 | 3 | } |
|
| 183 | 6 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
| 184 | 2 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
| 185 | 2 | $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
| 186 | 2 | } |
|
| 187 | 2 | } |
|
| 188 | 6 | if (isset($xmlRoot->{'also-load-methods'})) { |
|
| 189 | 1 | foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
| 190 | 1 | $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
| 191 | 1 | } |
|
| 192 | 1 | } |
|
| 193 | 6 | } |
|
| 194 | |||
| 195 | 6 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 196 | { |
||
| 197 | 6 | View Code Duplication | if (isset($mapping['name'])) { |
| 198 | 6 | $name = $mapping['name']; |
|
| 199 | 6 | } elseif (isset($mapping['fieldName'])) { |
|
| 200 | 1 | $name = $mapping['fieldName']; |
|
| 201 | 1 | } else { |
|
| 202 | throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
||
| 203 | } |
||
| 204 | |||
| 205 | 6 | $class->mapField($mapping); |
|
| 206 | |||
| 207 | // Index this field if either "index", "unique", or "sparse" are set |
||
| 208 | 6 | View Code Duplication | if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
| 209 | 6 | return; |
|
| 210 | } |
||
| 211 | |||
| 212 | 2 | $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); |
|
| 213 | 2 | $options = array(); |
|
| 214 | |||
| 215 | 2 | if (isset($mapping['background'])) { |
|
| 216 | $options['background'] = (boolean) $mapping['background']; |
||
| 217 | } |
||
| 218 | 2 | if (isset($mapping['drop-dups'])) { |
|
| 219 | 1 | $options['dropDups'] = (boolean) $mapping['drop-dups']; |
|
| 220 | 1 | } |
|
| 221 | 2 | if (isset($mapping['index-name'])) { |
|
| 222 | $options['name'] = (string) $mapping['index-name']; |
||
| 223 | } |
||
| 224 | 2 | if (isset($mapping['safe'])) { |
|
| 225 | $options['safe'] = (boolean) $mapping['safe']; |
||
| 226 | } |
||
| 227 | 2 | if (isset($mapping['sparse'])) { |
|
| 228 | 1 | $options['sparse'] = (boolean) $mapping['sparse']; |
|
| 229 | 1 | } |
|
| 230 | 2 | if (isset($mapping['unique'])) { |
|
| 231 | 2 | $options['unique'] = (boolean) $mapping['unique']; |
|
| 232 | 2 | } |
|
| 233 | |||
| 234 | 2 | $class->addIndex($keys, $options); |
|
| 235 | 2 | } |
|
| 236 | |||
| 237 | 2 | private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type) |
|
| 238 | { |
||
| 239 | 2 | $attributes = $embed->attributes(); |
|
| 240 | $mapping = array( |
||
| 241 | 2 | 'type' => $type, |
|
| 242 | 2 | 'embedded' => true, |
|
| 243 | 2 | 'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
| 244 | 2 | 'name' => (string) $attributes['field'], |
|
| 245 | 2 | 'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : CollectionHelper::DEFAULT_STRATEGY, |
|
| 246 | 2 | ); |
|
| 247 | 2 | if (isset($attributes['fieldName'])) { |
|
| 248 | 1 | $mapping['fieldName'] = (string) $attributes['fieldName']; |
|
| 249 | 1 | } |
|
| 250 | 2 | View Code Duplication | if (isset($embed->{'discriminator-field'})) { |
| 251 | 1 | $attr = $embed->{'discriminator-field'}; |
|
| 252 | 1 | $mapping['discriminatorField'] = (string) $attr['name']; |
|
| 253 | 1 | } |
|
| 254 | 2 | View Code Duplication | if (isset($embed->{'discriminator-map'})) { |
| 255 | 1 | foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
|
| 256 | 1 | $attr = $discriminatorMapping->attributes(); |
|
| 257 | 1 | $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
|
| 258 | 1 | } |
|
| 259 | 1 | } |
|
| 260 | 2 | if (isset($embed->{'default-discriminator-value'})) { |
|
| 261 | 1 | $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value']; |
|
| 262 | 1 | } |
|
| 263 | 2 | if (isset($attributes['not-saved'])) { |
|
| 264 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 265 | } |
||
| 266 | 2 | if (isset($attributes['also-load'])) { |
|
| 267 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 268 | } |
||
| 269 | 2 | $this->addFieldMapping($class, $mapping); |
|
| 270 | 2 | } |
|
| 271 | |||
| 272 | 3 | private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type) |
|
| 273 | { |
||
| 274 | 3 | $cascade = array_keys((array) $reference->cascade); |
|
| 275 | 3 | if (1 === count($cascade)) { |
|
| 276 | 2 | $cascade = current($cascade) ?: next($cascade); |
|
| 277 | 2 | } |
|
| 278 | 3 | $attributes = $reference->attributes(); |
|
| 279 | $mapping = array( |
||
| 280 | 3 | 'cascade' => $cascade, |
|
| 281 | 3 | 'orphanRemoval' => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false, |
|
| 282 | 3 | 'type' => $type, |
|
| 283 | 3 | 'reference' => true, |
|
| 284 | 3 | 'simple' => isset($attributes['simple']) ? ('true' === (string) $attributes['simple']) : false, |
|
| 285 | 3 | 'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
| 286 | 3 | 'name' => (string) $attributes['field'], |
|
| 287 | 3 | 'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : CollectionHelper::DEFAULT_STRATEGY, |
|
| 288 | 3 | 'inversedBy' => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null, |
|
| 289 | 3 | 'mappedBy' => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null, |
|
| 290 | 3 | 'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null, |
|
| 291 | 3 | 'limit' => isset($attributes['limit']) ? (integer) $attributes['limit'] : null, |
|
| 292 | 3 | 'skip' => isset($attributes['skip']) ? (integer) $attributes['skip'] : null, |
|
| 293 | 3 | ); |
|
| 294 | |||
| 295 | 3 | if (isset($attributes['fieldName'])) { |
|
| 296 | 1 | $mapping['fieldName'] = (string) $attributes['fieldName']; |
|
| 297 | 1 | } |
|
| 298 | 3 | View Code Duplication | if (isset($reference->{'discriminator-field'})) { |
| 299 | 1 | $attr = $reference->{'discriminator-field'}; |
|
| 300 | 1 | $mapping['discriminatorField'] = (string) $attr['name']; |
|
| 301 | 1 | } |
|
| 302 | 3 | View Code Duplication | if (isset($reference->{'discriminator-map'})) { |
| 303 | 1 | foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
|
| 304 | 1 | $attr = $discriminatorMapping->attributes(); |
|
| 305 | 1 | $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
|
| 306 | 1 | } |
|
| 307 | 1 | } |
|
| 308 | 3 | if (isset($reference->{'default-discriminator-value'})) { |
|
| 309 | 1 | $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value']; |
|
| 310 | 1 | } |
|
| 311 | 3 | if (isset($reference->{'sort'})) { |
|
| 312 | View Code Duplication | foreach ($reference->{'sort'}->{'sort'} as $sort) { |
|
| 313 | $attr = $sort->attributes(); |
||
| 314 | $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc'; |
||
| 315 | } |
||
| 316 | } |
||
| 317 | 3 | if (isset($reference->{'criteria'})) { |
|
| 318 | View Code Duplication | foreach ($reference->{'criteria'}->{'criteria'} as $criteria) { |
|
| 319 | $attr = $criteria->attributes(); |
||
| 320 | $mapping['criteria'][(string) $attr['field']] = (string) $attr['value']; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | 3 | if (isset($attributes['not-saved'])) { |
|
| 324 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 325 | } |
||
| 326 | 3 | if (isset($attributes['also-load'])) { |
|
| 327 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 328 | } |
||
| 329 | 3 | $this->addFieldMapping($class, $mapping); |
|
| 330 | 3 | } |
|
| 331 | |||
| 332 | 1 | private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) |
|
| 333 | { |
||
| 334 | 1 | $attributes = $xmlIndex->attributes(); |
|
| 335 | |||
| 336 | 1 | $keys = array(); |
|
| 337 | |||
| 338 | 1 | foreach ($xmlIndex->{'key'} as $key) { |
|
| 339 | 1 | $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; |
|
| 340 | 1 | } |
|
| 341 | |||
| 342 | 1 | $options = array(); |
|
| 343 | |||
| 344 | 1 | if (isset($attributes['background'])) { |
|
| 345 | $options['background'] = ('true' === (string) $attributes['background']); |
||
| 346 | } |
||
| 347 | 1 | if (isset($attributes['drop-dups'])) { |
|
| 348 | $options['dropDups'] = ('true' === (string) $attributes['drop-dups']); |
||
| 349 | } |
||
| 350 | 1 | if (isset($attributes['name'])) { |
|
| 351 | $options['name'] = (string) $attributes['name']; |
||
| 352 | } |
||
| 353 | 1 | if (isset($attributes['safe'])) { |
|
| 354 | $options['safe'] = ('true' === (string) $attributes['safe']); |
||
| 355 | } |
||
| 356 | 1 | if (isset($attributes['sparse'])) { |
|
| 357 | $options['sparse'] = ('true' === (string) $attributes['sparse']); |
||
| 358 | } |
||
| 359 | 1 | if (isset($attributes['unique'])) { |
|
| 360 | 1 | $options['unique'] = ('true' === (string) $attributes['unique']); |
|
| 361 | 1 | } |
|
| 362 | |||
| 363 | 1 | if (isset($xmlIndex->{'option'})) { |
|
| 364 | 1 | foreach ($xmlIndex->{'option'} as $option) { |
|
| 365 | 1 | $value = (string) $option['value']; |
|
| 366 | 1 | if ($value === 'true') { |
|
| 367 | $value = true; |
||
| 368 | 1 | } elseif ($value === 'false') { |
|
| 369 | 1 | $value = false; |
|
| 370 | 1 | } elseif (is_numeric($value)) { |
|
| 371 | 1 | $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
|
| 372 | 1 | } |
|
| 373 | 1 | $options[(string) $option['name']] = $value; |
|
| 374 | 1 | } |
|
| 375 | 1 | } |
|
| 376 | |||
| 377 | 1 | $class->addIndex($keys, $options); |
|
| 378 | 1 | } |
|
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritDoc} |
||
| 382 | */ |
||
| 383 | 6 | protected function loadMappingFile($file) |
|
| 399 | } |
||
| 400 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.