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 |
||
| 33 | class XmlDriver extends FileDriver |
||
| 34 | { |
||
| 35 | const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | */ |
||
| 40 | 14 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | 8 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 49 | { |
||
| 50 | /* @var $class ClassMetadataInfo */ |
||
| 51 | /* @var $xmlRoot \SimpleXMLElement */ |
||
| 52 | 8 | $xmlRoot = $this->getElement($className); |
|
| 53 | 8 | if ( ! $xmlRoot) { |
|
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | 8 | if ($xmlRoot->getName() == 'document') { |
|
| 58 | 8 | if (isset($xmlRoot['repository-class'])) { |
|
| 59 | 8 | $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
|
| 60 | } |
||
| 61 | 3 | View Code Duplication | } elseif ($xmlRoot->getName() == 'mapped-superclass') { |
|
|
|||
| 62 | 2 | $class->setCustomRepositoryClass( |
|
| 63 | 2 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
| 64 | ); |
||
| 65 | 2 | $class->isMappedSuperclass = true; |
|
| 66 | 1 | } elseif ($xmlRoot->getName() == 'embedded-document') { |
|
| 67 | 1 | $class->isEmbeddedDocument = true; |
|
| 68 | 1 | } elseif ($xmlRoot->getName() == 'query-result-document') { |
|
| 69 | 1 | $class->isQueryResultDocument = true; |
|
| 70 | } |
||
| 71 | 8 | if (isset($xmlRoot['db'])) { |
|
| 72 | 4 | $class->setDatabase((string) $xmlRoot['db']); |
|
| 73 | } |
||
| 74 | 8 | if (isset($xmlRoot['collection'])) { |
|
| 75 | 7 | if (isset($xmlRoot['capped-collection'])) { |
|
| 76 | $config = array('name' => (string) $xmlRoot['collection']); |
||
| 77 | $config['capped'] = (bool) $xmlRoot['capped-collection']; |
||
| 78 | if (isset($xmlRoot['capped-collection-max'])) { |
||
| 79 | $config['max'] = (int) $xmlRoot['capped-collection-max']; |
||
| 80 | } |
||
| 81 | if (isset($xmlRoot['capped-collection-size'])) { |
||
| 82 | $config['size'] = (int) $xmlRoot['capped-collection-size']; |
||
| 83 | } |
||
| 84 | $class->setCollection($config); |
||
| 85 | } else { |
||
| 86 | 7 | $class->setCollection((string) $xmlRoot['collection']); |
|
| 87 | } |
||
| 88 | } |
||
| 89 | 8 | if (isset($xmlRoot['writeConcern'])) { |
|
| 90 | $class->setWriteConcern((string) $xmlRoot['writeConcern']); |
||
| 91 | } |
||
| 92 | 8 | if (isset($xmlRoot['inheritance-type'])) { |
|
| 93 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
||
| 94 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType)); |
||
| 95 | } |
||
| 96 | 8 | View Code Duplication | if (isset($xmlRoot['change-tracking-policy'])) { |
| 97 | 2 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
| 98 | } |
||
| 99 | 8 | if (isset($xmlRoot->{'discriminator-field'})) { |
|
| 100 | $discrField = $xmlRoot->{'discriminator-field'}; |
||
| 101 | /* XSD only allows for "name", which is consistent with association |
||
| 102 | * configurations, but fall back to "fieldName" for BC. |
||
| 103 | */ |
||
| 104 | $class->setDiscriminatorField( |
||
| 105 | isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName'] |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | 8 | View Code Duplication | if (isset($xmlRoot->{'discriminator-map'})) { |
| 109 | $map = array(); |
||
| 110 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) { |
||
| 111 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
||
| 112 | } |
||
| 113 | $class->setDiscriminatorMap($map); |
||
| 114 | } |
||
| 115 | 8 | if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
| 116 | $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
||
| 117 | } |
||
| 118 | 8 | if (isset($xmlRoot->{'indexes'})) { |
|
| 119 | 2 | foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
| 120 | 2 | $this->addIndex($class, $index); |
|
| 121 | } |
||
| 122 | } |
||
| 123 | 8 | if (isset($xmlRoot->{'shard-key'})) { |
|
| 124 | $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]); |
||
| 125 | } |
||
| 126 | 8 | if (isset($xmlRoot['require-indexes'])) { |
|
| 127 | 1 | $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']); |
|
| 128 | } |
||
| 129 | 8 | if (isset($xmlRoot['slave-okay'])) { |
|
| 130 | 1 | $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']); |
|
| 131 | } |
||
| 132 | 8 | if (isset($xmlRoot['read-only']) && 'true' === (string) $xmlRoot['read-only']) { |
|
| 133 | $class->markReadOnly(); |
||
| 134 | } |
||
| 135 | 8 | if (isset($xmlRoot->{'read-preference'})) { |
|
| 136 | $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'})); |
||
| 137 | } |
||
| 138 | 8 | if (isset($xmlRoot->field)) { |
|
| 139 | 8 | foreach ($xmlRoot->field as $field) { |
|
| 140 | 8 | $mapping = array(); |
|
| 141 | 8 | $attributes = $field->attributes(); |
|
| 142 | 8 | foreach ($attributes as $key => $value) { |
|
| 143 | 8 | $mapping[$key] = (string) $value; |
|
| 144 | 8 | $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance'); |
|
| 145 | 8 | if (in_array($key, $booleanAttributes)) { |
|
| 146 | 8 | $mapping[$key] = ('true' === $mapping[$key]); |
|
| 147 | } |
||
| 148 | } |
||
| 149 | 8 | if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) { |
|
| 150 | 3 | $mapping['options'] = array(); |
|
| 151 | 3 | if (isset($field->{'id-generator-option'})) { |
|
| 152 | 1 | foreach ($field->{'id-generator-option'} as $generatorOptions) { |
|
| 153 | 1 | $attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
| 154 | 1 | if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) { |
|
| 155 | 1 | $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | 8 | if (isset($attributes['not-saved'])) { |
|
| 162 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 163 | } |
||
| 164 | |||
| 165 | 8 | if (isset($attributes['also-load'])) { |
|
| 166 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 167 | 8 | } elseif (isset($attributes['version'])) { |
|
| 168 | $mapping['version'] = ('true' === (string) $attributes['version']); |
||
| 169 | 8 | } elseif (isset($attributes['lock'])) { |
|
| 170 | $mapping['lock'] = ('true' === (string) $attributes['lock']); |
||
| 171 | } |
||
| 172 | |||
| 173 | 8 | $this->addFieldMapping($class, $mapping); |
|
| 174 | } |
||
| 175 | } |
||
| 176 | 8 | if (isset($xmlRoot->{'embed-one'})) { |
|
| 177 | 1 | foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
| 178 | 1 | $this->addEmbedMapping($class, $embed, 'one'); |
|
| 179 | } |
||
| 180 | } |
||
| 181 | 8 | if (isset($xmlRoot->{'embed-many'})) { |
|
| 182 | 1 | foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
| 183 | 1 | $this->addEmbedMapping($class, $embed, 'many'); |
|
| 184 | } |
||
| 185 | } |
||
| 186 | 8 | if (isset($xmlRoot->{'reference-many'})) { |
|
| 187 | 3 | foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
| 188 | 3 | $this->addReferenceMapping($class, $reference, 'many'); |
|
| 189 | } |
||
| 190 | } |
||
| 191 | 8 | if (isset($xmlRoot->{'reference-one'})) { |
|
| 192 | 2 | foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
| 193 | 2 | $this->addReferenceMapping($class, $reference, 'one'); |
|
| 194 | } |
||
| 195 | } |
||
| 196 | 8 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
| 197 | 1 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
| 198 | 1 | $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
| 199 | } |
||
| 200 | } |
||
| 201 | 8 | if (isset($xmlRoot->{'also-load-methods'})) { |
|
| 202 | 1 | foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
| 203 | 1 | $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
| 204 | } |
||
| 205 | } |
||
| 206 | 8 | } |
|
| 207 | |||
| 208 | 9 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 209 | { |
||
| 210 | 9 | View Code Duplication | if (isset($mapping['name'])) { |
| 211 | 9 | $name = $mapping['name']; |
|
| 212 | } elseif (isset($mapping['fieldName'])) { |
||
| 213 | $name = $mapping['fieldName']; |
||
| 214 | } else { |
||
| 215 | throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
||
| 216 | } |
||
| 217 | |||
| 218 | 9 | $class->mapField($mapping); |
|
| 219 | |||
| 220 | // Index this field if either "index", "unique", or "sparse" are set |
||
| 221 | 9 | View Code Duplication | if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
| 222 | 9 | return; |
|
| 223 | } |
||
| 224 | |||
| 225 | 1 | $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); |
|
| 226 | 1 | $options = array(); |
|
| 227 | |||
| 228 | 1 | if (isset($mapping['background'])) { |
|
| 229 | $options['background'] = (boolean) $mapping['background']; |
||
| 230 | } |
||
| 231 | 1 | if (isset($mapping['drop-dups'])) { |
|
| 232 | $options['dropDups'] = (boolean) $mapping['drop-dups']; |
||
| 233 | } |
||
| 234 | 1 | if (isset($mapping['index-name'])) { |
|
| 235 | $options['name'] = (string) $mapping['index-name']; |
||
| 236 | } |
||
| 237 | 1 | if (isset($mapping['safe'])) { |
|
| 238 | $options['safe'] = (boolean) $mapping['safe']; |
||
| 239 | } |
||
| 240 | 1 | if (isset($mapping['sparse'])) { |
|
| 241 | 1 | $options['sparse'] = (boolean) $mapping['sparse']; |
|
| 242 | } |
||
| 243 | 1 | if (isset($mapping['unique'])) { |
|
| 244 | 1 | $options['unique'] = (boolean) $mapping['unique']; |
|
| 245 | } |
||
| 246 | |||
| 247 | 1 | $class->addIndex($keys, $options); |
|
| 248 | 1 | } |
|
| 249 | |||
| 250 | 1 | private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type) |
|
| 251 | { |
||
| 252 | 1 | $attributes = $embed->attributes(); |
|
| 253 | 1 | $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
| 254 | $mapping = array( |
||
| 255 | 1 | 'type' => $type, |
|
| 256 | 'embedded' => true, |
||
| 257 | 1 | 'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
| 258 | 1 | 'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null, |
|
| 259 | 1 | 'name' => (string) $attributes['field'], |
|
| 260 | 1 | 'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy, |
|
| 261 | ); |
||
| 262 | 1 | if (isset($attributes['fieldName'])) { |
|
| 263 | $mapping['fieldName'] = (string) $attributes['fieldName']; |
||
| 264 | } |
||
| 265 | 1 | View Code Duplication | if (isset($embed->{'discriminator-field'})) { |
| 266 | $attr = $embed->{'discriminator-field'}; |
||
| 267 | $mapping['discriminatorField'] = (string) $attr['name']; |
||
| 268 | } |
||
| 269 | 1 | View Code Duplication | if (isset($embed->{'discriminator-map'})) { |
| 270 | foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
||
| 271 | $attr = $discriminatorMapping->attributes(); |
||
| 272 | $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | 1 | if (isset($embed->{'default-discriminator-value'})) { |
|
| 276 | $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value']; |
||
| 277 | } |
||
| 278 | 1 | if (isset($attributes['not-saved'])) { |
|
| 279 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 280 | } |
||
| 281 | 1 | if (isset($attributes['also-load'])) { |
|
| 282 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 283 | } |
||
| 284 | 1 | $this->addFieldMapping($class, $mapping); |
|
| 285 | 1 | } |
|
| 286 | |||
| 287 | 4 | private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type) |
|
| 288 | { |
||
| 289 | 4 | $cascade = array_keys((array) $reference->cascade); |
|
| 290 | 4 | if (1 === count($cascade)) { |
|
| 291 | 1 | $cascade = current($cascade) ?: next($cascade); |
|
| 292 | } |
||
| 293 | 4 | $attributes = $reference->attributes(); |
|
| 294 | 4 | $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
| 295 | $mapping = array( |
||
| 296 | 4 | 'cascade' => $cascade, |
|
| 297 | 4 | 'orphanRemoval' => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false, |
|
| 298 | 4 | 'type' => $type, |
|
| 299 | 'reference' => true, |
||
| 300 | 4 | 'simple' => isset($attributes['simple']) ? ('true' === (string) $attributes['simple']) : false, // deprecated |
|
| 301 | 4 | 'storeAs' => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB, |
|
| 302 | 4 | 'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
| 303 | 4 | 'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null, |
|
| 304 | 4 | 'name' => (string) $attributes['field'], |
|
| 305 | 4 | 'strategy' => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy, |
|
| 306 | 4 | 'inversedBy' => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null, |
|
| 307 | 4 | 'mappedBy' => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null, |
|
| 308 | 4 | 'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null, |
|
| 309 | 4 | 'limit' => isset($attributes['limit']) ? (integer) $attributes['limit'] : null, |
|
| 310 | 4 | 'skip' => isset($attributes['skip']) ? (integer) $attributes['skip'] : null, |
|
| 311 | 'prime' => [], |
||
| 312 | ); |
||
| 313 | |||
| 314 | 4 | if (isset($attributes['fieldName'])) { |
|
| 315 | $mapping['fieldName'] = (string) $attributes['fieldName']; |
||
| 316 | } |
||
| 317 | 4 | View Code Duplication | if (isset($reference->{'discriminator-field'})) { |
| 318 | $attr = $reference->{'discriminator-field'}; |
||
| 319 | $mapping['discriminatorField'] = (string) $attr['name']; |
||
| 320 | } |
||
| 321 | 4 | View Code Duplication | if (isset($reference->{'discriminator-map'})) { |
| 322 | foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
||
| 323 | $attr = $discriminatorMapping->attributes(); |
||
| 324 | $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | 4 | if (isset($reference->{'default-discriminator-value'})) { |
|
| 328 | $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value']; |
||
| 329 | } |
||
| 330 | 4 | if (isset($reference->{'sort'})) { |
|
| 331 | View Code Duplication | foreach ($reference->{'sort'}->{'sort'} as $sort) { |
|
| 332 | $attr = $sort->attributes(); |
||
| 333 | $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc'; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | 4 | if (isset($reference->{'criteria'})) { |
|
| 337 | View Code Duplication | foreach ($reference->{'criteria'}->{'criteria'} as $criteria) { |
|
| 338 | $attr = $criteria->attributes(); |
||
| 339 | $mapping['criteria'][(string) $attr['field']] = (string) $attr['value']; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | 4 | if (isset($attributes['not-saved'])) { |
|
| 343 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
| 344 | } |
||
| 345 | 4 | if (isset($attributes['also-load'])) { |
|
| 346 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
| 347 | } |
||
| 348 | 4 | if (isset($reference->{'prime'})) { |
|
| 349 | 1 | foreach ($reference->{'prime'}->{'field'} as $field) { |
|
| 350 | 1 | $attr = $field->attributes(); |
|
| 351 | 1 | $mapping['prime'][] = (string) $attr['name']; |
|
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | 4 | $this->addFieldMapping($class, $mapping); |
|
| 356 | 4 | } |
|
| 357 | |||
| 358 | 2 | private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) |
|
| 359 | { |
||
| 360 | 2 | $attributes = $xmlIndex->attributes(); |
|
| 361 | |||
| 362 | 2 | $keys = array(); |
|
| 363 | |||
| 364 | 2 | View Code Duplication | foreach ($xmlIndex->{'key'} as $key) { |
| 365 | 2 | $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc'; |
|
| 366 | } |
||
| 367 | |||
| 368 | 2 | $options = array(); |
|
| 369 | |||
| 370 | 2 | if (isset($attributes['background'])) { |
|
| 371 | $options['background'] = ('true' === (string) $attributes['background']); |
||
| 372 | } |
||
| 373 | 2 | if (isset($attributes['drop-dups'])) { |
|
| 374 | $options['dropDups'] = ('true' === (string) $attributes['drop-dups']); |
||
| 375 | } |
||
| 376 | 2 | if (isset($attributes['name'])) { |
|
| 377 | $options['name'] = (string) $attributes['name']; |
||
| 378 | } |
||
| 379 | 2 | if (isset($attributes['safe'])) { |
|
| 380 | $options['safe'] = ('true' === (string) $attributes['safe']); |
||
| 381 | } |
||
| 382 | 2 | if (isset($attributes['sparse'])) { |
|
| 383 | $options['sparse'] = ('true' === (string) $attributes['sparse']); |
||
| 384 | } |
||
| 385 | 2 | if (isset($attributes['unique'])) { |
|
| 386 | $options['unique'] = ('true' === (string) $attributes['unique']); |
||
| 387 | } |
||
| 388 | |||
| 389 | 2 | View Code Duplication | if (isset($xmlIndex->{'option'})) { |
| 390 | foreach ($xmlIndex->{'option'} as $option) { |
||
| 391 | $value = (string) $option['value']; |
||
| 392 | if ($value === 'true') { |
||
| 393 | $value = true; |
||
| 394 | } elseif ($value === 'false') { |
||
| 395 | $value = false; |
||
| 396 | } elseif (is_numeric($value)) { |
||
| 397 | $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
||
| 398 | } |
||
| 399 | $options[(string) $option['name']] = $value; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | 2 | if (isset($xmlIndex->{'partial-filter-expression'})) { |
|
| 404 | 2 | $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'}; |
|
| 405 | |||
| 406 | 2 | if (isset($partialFilterExpressionMapping->and)) { |
|
| 407 | 2 | foreach ($partialFilterExpressionMapping->and as $and) { |
|
| 408 | 2 | if (! isset($and->field)) { |
|
| 409 | 1 | continue; |
|
| 410 | } |
||
| 411 | |||
| 412 | 2 | $partialFilterExpression = $this->getPartialFilterExpression($and->field); |
|
| 413 | 2 | if (! $partialFilterExpression) { |
|
| 414 | continue; |
||
| 415 | } |
||
| 416 | |||
| 417 | 2 | $options['partialFilterExpression']['$and'][] = $partialFilterExpression; |
|
| 418 | } |
||
| 419 | 1 | } elseif (isset($partialFilterExpressionMapping->field)) { |
|
| 420 | 1 | $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field); |
|
| 421 | |||
| 422 | 1 | if ($partialFilterExpression) { |
|
| 423 | 1 | $options['partialFilterExpression'] = $partialFilterExpression; |
|
| 424 | } |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | 2 | $class->addIndex($keys, $options); |
|
| 429 | 2 | } |
|
| 430 | |||
| 431 | 2 | private function getPartialFilterExpression(\SimpleXMLElement $fields) |
|
| 465 | |||
| 466 | 1 | private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey) |
|
| 467 | { |
||
| 468 | 1 | $attributes = $xmlShardkey->attributes(); |
|
| 469 | |||
| 470 | 1 | $keys = array(); |
|
| 471 | 1 | $options = array(); |
|
| 472 | 1 | View Code Duplication | foreach ($xmlShardkey->{'key'} as $key) { |
| 473 | 1 | $keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc'; |
|
| 474 | } |
||
| 475 | |||
| 476 | 1 | if (isset($attributes['unique'])) { |
|
| 477 | 1 | $options['unique'] = ('true' === (string) $attributes['unique']); |
|
| 478 | } |
||
| 479 | |||
| 480 | 1 | if (isset($attributes['numInitialChunks'])) { |
|
| 481 | 1 | $options['numInitialChunks'] = (int) $attributes['numInitialChunks']; |
|
| 482 | } |
||
| 483 | |||
| 484 | 1 | View Code Duplication | if (isset($xmlShardkey->{'option'})) { |
| 485 | foreach ($xmlShardkey->{'option'} as $option) { |
||
| 486 | $value = (string) $option['value']; |
||
| 487 | if ($value === 'true') { |
||
| 488 | $value = true; |
||
| 489 | } elseif ($value === 'false') { |
||
| 490 | $value = false; |
||
| 491 | } elseif (is_numeric($value)) { |
||
| 492 | $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value; |
||
| 493 | } |
||
| 494 | $options[(string) $option['name']] = $value; |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | 1 | $class->setShardKey($keys, $options); |
|
| 499 | 1 | } |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Parses <read-preference> to a format suitable for the underlying driver. |
||
| 503 | * |
||
| 504 | * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference}); |
||
| 505 | * |
||
| 506 | * @param \SimpleXMLElement $xmlReadPreference |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | private function transformReadPreference($xmlReadPreference) |
||
| 510 | { |
||
| 511 | $tags = null; |
||
| 512 | if (isset($xmlReadPreference->{'tag-set'})) { |
||
| 513 | $tags = []; |
||
| 514 | foreach ($xmlReadPreference->{'tag-set'} as $tagSet) { |
||
| 515 | $set = []; |
||
| 516 | foreach ($tagSet->tag as $tag) { |
||
| 517 | $set[(string) $tag['name']] = (string) $tag['value']; |
||
| 518 | } |
||
| 519 | $tags[] = $set; |
||
| 520 | } |
||
| 521 | } |
||
| 522 | return [(string) $xmlReadPreference['mode'], $tags]; |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritDoc} |
||
| 527 | */ |
||
| 528 | 8 | protected function loadMappingFile($file) |
|
| 544 | } |
||
| 545 |
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.