Completed
Push — master ( 93451b...8b825f )
by Andreas
15s
created

XmlDriver   D

Complexity

Total Complexity 158

Size/Duplication

Total Lines 502
Duplicated Lines 15.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 158
lcom 1
cbo 2
dl 80
loc 502
ccs 266
cts 302
cp 0.8808
rs 4.8717
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A loadMappingFile() 0 16 4
A __construct() 0 4 1
C getPartialFilterExpression() 0 34 11
F loadMetadataForClass() 10 156 55
C addFieldMapping() 10 38 11
F addEmbedMapping() 10 36 12
F addReferenceMapping() 18 69 28
F addIndex() 16 69 21
C setShardKey() 16 34 11
A transformReadPreference() 0 15 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
3
namespace Doctrine\ODM\MongoDB\Mapping\Driver;
4
5
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
6
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
7
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata;
8
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
9
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
10
11
/**
12
 * XmlDriver is a metadata driver that enables mapping through XML files.
13
 *
14
 * @since       1.0
15
 */
16
class XmlDriver extends FileDriver
17
{
18
    const DEFAULT_FILE_EXTENSION = '.dcm.xml';
19
20
    /**
21
     * {@inheritDoc}
22
     */
23 15
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
24
    {
25 15
        parent::__construct($locator, $fileExtension);
26 15
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 9
    public function loadMetadataForClass($className, ClassMetadata $class)
32
    {
33
        /* @var $class ClassMetadataInfo */
34
        /* @var $xmlRoot \SimpleXMLElement */
35 9
        $xmlRoot = $this->getElement($className);
36 9
        if ( ! $xmlRoot) {
37
            return;
38
        }
39
40 9
        if ($xmlRoot->getName() == 'document') {
41 9
            if (isset($xmlRoot['repository-class'])) {
42 9
                $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']);
43
            }
44 3
        } elseif ($xmlRoot->getName() == 'mapped-superclass') {
45 2
            $class->setCustomRepositoryClass(
46 2
                isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null
47
            );
48 2
            $class->isMappedSuperclass = true;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49 1
        } elseif ($xmlRoot->getName() == 'embedded-document') {
50 1
            $class->isEmbeddedDocument = true;
0 ignored issues
show
Bug introduced by
Accessing isEmbeddedDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
51 1
        } elseif ($xmlRoot->getName() == 'query-result-document') {
52 1
            $class->isQueryResultDocument = true;
0 ignored issues
show
Bug introduced by
Accessing isQueryResultDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53
        }
54 9
        if (isset($xmlRoot['db'])) {
55 4
            $class->setDatabase((string) $xmlRoot['db']);
56
        }
57 9
        if (isset($xmlRoot['collection'])) {
58 8
            if (isset($xmlRoot['capped-collection'])) {
59
                $config = array('name' => (string) $xmlRoot['collection']);
60
                $config['capped'] = (bool) $xmlRoot['capped-collection'];
61
                if (isset($xmlRoot['capped-collection-max'])) {
62
                    $config['max'] = (int) $xmlRoot['capped-collection-max'];
63
                }
64
                if (isset($xmlRoot['capped-collection-size'])) {
65
                    $config['size'] = (int) $xmlRoot['capped-collection-size'];
66
                }
67
                $class->setCollection($config);
68
            } else {
69 8
                $class->setCollection((string) $xmlRoot['collection']);
70
            }
71
        }
72 9
        if (isset($xmlRoot['writeConcern'])) {
73 1
            $class->setWriteConcern((string) $xmlRoot['writeConcern']);
74
        }
75 9
        if (isset($xmlRoot['inheritance-type'])) {
76
            $inheritanceType = (string) $xmlRoot['inheritance-type'];
77
            $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
78
        }
79 9 View Code Duplication
        if (isset($xmlRoot['change-tracking-policy'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
80 2
            $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy'])));
81
        }
82 9
        if (isset($xmlRoot->{'discriminator-field'})) {
83 1
            $discrField = $xmlRoot->{'discriminator-field'};
84
            /* XSD only allows for "name", which is consistent with association
85
             * configurations, but fall back to "fieldName" for BC.
86
             */
87 1
            $class->setDiscriminatorField(
88 1
                isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName']
89
            );
90
        }
91 9 View Code Duplication
        if (isset($xmlRoot->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
92 1
            $map = array();
93 1
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
94 1
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
95
            }
96 1
            $class->setDiscriminatorMap($map);
97
        }
98 9
        if (isset($xmlRoot->{'default-discriminator-value'})) {
99 1
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
100
        }
101 9
        if (isset($xmlRoot->{'indexes'})) {
102 3
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
103 3
                $this->addIndex($class, $index);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
104
            }
105
        }
106 9
        if (isset($xmlRoot->{'shard-key'})) {
107 1
            $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
108
        }
109 9
        if (isset($xmlRoot['slave-okay'])) {
110 1
            $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']);
111
        }
112 9
        if (isset($xmlRoot['read-only']) && 'true' === (string) $xmlRoot['read-only']) {
113 1
            $class->markReadOnly();
114
        }
115 9
        if (isset($xmlRoot->{'read-preference'})) {
116 1
            $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'}));
117
        }
118 9
        if (isset($xmlRoot->field)) {
119 9
            foreach ($xmlRoot->field as $field) {
120 9
                $mapping = array();
121 9
                $attributes = $field->attributes();
122 9
                foreach ($attributes as $key => $value) {
123 9
                    $mapping[$key] = (string) $value;
124 9
                    $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse');
125 9
                    if (in_array($key, $booleanAttributes)) {
126 9
                        $mapping[$key] = ('true' === $mapping[$key]);
127
                    }
128
                }
129 9
                if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
130 3
                    $mapping['options'] = array();
131 3
                    if (isset($field->{'id-generator-option'})) {
132 1
                        foreach ($field->{'id-generator-option'} as $generatorOptions) {
133 1
                            $attributesGenerator = iterator_to_array($generatorOptions->attributes());
134 1
                            if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) {
135 1
                                $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
136
                            }
137
                        }
138
                    }
139
                }
140
141 9
                if (isset($attributes['not-saved'])) {
142
                    $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
143
                }
144
145 9
                if (isset($attributes['also-load'])) {
146
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
147 9
                } elseif (isset($attributes['version'])) {
148 1
                    $mapping['version'] = ('true' === (string) $attributes['version']);
149 9
                } elseif (isset($attributes['lock'])) {
150 1
                    $mapping['lock'] = ('true' === (string) $attributes['lock']);
151
                }
152
153 9
                $this->addFieldMapping($class, $mapping);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
154
            }
155
        }
156 9
        if (isset($xmlRoot->{'embed-one'})) {
157 2
            foreach ($xmlRoot->{'embed-one'} as $embed) {
158 2
                $this->addEmbedMapping($class, $embed, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
159
            }
160
        }
161 9
        if (isset($xmlRoot->{'embed-many'})) {
162 2
            foreach ($xmlRoot->{'embed-many'} as $embed) {
163 2
                $this->addEmbedMapping($class, $embed, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
164
            }
165
        }
166 9
        if (isset($xmlRoot->{'reference-many'})) {
167 4
            foreach ($xmlRoot->{'reference-many'} as $reference) {
168 4
                $this->addReferenceMapping($class, $reference, 'many');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
169
            }
170
        }
171 9
        if (isset($xmlRoot->{'reference-one'})) {
172 3
            foreach ($xmlRoot->{'reference-one'} as $reference) {
173 3
                $this->addReferenceMapping($class, $reference, 'one');
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...ping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
174
            }
175
        }
176 9
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
177 2
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
178 2
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
179
            }
180
        }
181 9
        if (isset($xmlRoot->{'also-load-methods'})) {
182 1
            foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
183 1
                $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
184
            }
185
        }
186 9
    }
187
188 10
    private function addFieldMapping(ClassMetadataInfo $class, $mapping)
189
    {
190 10 View Code Duplication
        if (isset($mapping['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
191 10
            $name = $mapping['name'];
192 1
        } elseif (isset($mapping['fieldName'])) {
193 1
            $name = $mapping['fieldName'];
194
        } else {
195
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
196
        }
197
198 10
        $class->mapField($mapping);
199
200
        // Index this field if either "index", "unique", or "sparse" are set
201 10 View Code Duplication
        if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
202 10
            return;
203
        }
204
205 2
        $keys = array($name => $mapping['order'] ?? 'asc');
206 2
        $options = array();
207
208 2
        if (isset($mapping['background'])) {
209
            $options['background'] = (boolean) $mapping['background'];
210
        }
211 2
        if (isset($mapping['drop-dups'])) {
212 1
            $options['dropDups'] = (boolean) $mapping['drop-dups'];
213
        }
214 2
        if (isset($mapping['index-name'])) {
215
            $options['name'] = (string) $mapping['index-name'];
216
        }
217 2
        if (isset($mapping['sparse'])) {
218 1
            $options['sparse'] = (boolean) $mapping['sparse'];
219
        }
220 2
        if (isset($mapping['unique'])) {
221 2
            $options['unique'] = (boolean) $mapping['unique'];
222
        }
223
224 2
        $class->addIndex($keys, $options);
225 2
    }
226
227 2
    private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
228
    {
229 2
        $attributes = $embed->attributes();
230 2
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
231
        $mapping = array(
232 2
            'type'            => $type,
233
            'embedded'        => true,
234 2
            'targetDocument'  => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
235 2
            'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
236 2
            'name'            => (string) $attributes['field'],
237 2
            'strategy'        => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
238
        );
239 2
        if (isset($attributes['fieldName'])) {
240 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
241
        }
242 2 View Code Duplication
        if (isset($embed->{'discriminator-field'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
243 1
            $attr = $embed->{'discriminator-field'};
244 1
            $mapping['discriminatorField'] = (string) $attr['name'];
245
        }
246 2 View Code Duplication
        if (isset($embed->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
247 1
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
248 1
                $attr = $discriminatorMapping->attributes();
249 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
250
            }
251
        }
252 2
        if (isset($embed->{'default-discriminator-value'})) {
253 1
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
254
        }
255 2
        if (isset($attributes['not-saved'])) {
256
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
257
        }
258 2
        if (isset($attributes['also-load'])) {
259
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
260
        }
261 2
        $this->addFieldMapping($class, $mapping);
262 2
    }
263
264 5
    private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type)
265
    {
266 5
        $cascade = array_keys((array) $reference->cascade);
267 5
        if (1 === count($cascade)) {
268 2
            $cascade = current($cascade) ?: next($cascade);
269
        }
270 5
        $attributes = $reference->attributes();
271 5
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
272
        $mapping = array(
273 5
            'cascade'          => $cascade,
274 5
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
275 5
            'type'             => $type,
276
            'reference'        => true,
277 5
            'storeAs'          => isset($attributes['store-as']) ? (string) $attributes['store-as'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF,
278 5
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
279 5
            'collectionClass'  => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
280 5
            'name'             => (string) $attributes['field'],
281 5
            'strategy'         => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
282 5
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
283 5
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
284 5
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
285 5
            'limit'            => isset($attributes['limit']) ? (integer) $attributes['limit'] : null,
286 5
            'skip'             => isset($attributes['skip']) ? (integer) $attributes['skip'] : null,
287
            'prime'            => [],
288
        );
289
290 5
        if (isset($attributes['fieldName'])) {
291 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
292
        }
293 5 View Code Duplication
        if (isset($reference->{'discriminator-field'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
294 1
            $attr = $reference->{'discriminator-field'};
295 1
            $mapping['discriminatorField'] = (string) $attr['name'];
296
        }
297 5 View Code Duplication
        if (isset($reference->{'discriminator-map'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
298 1
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
299 1
                $attr = $discriminatorMapping->attributes();
300 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
301
            }
302
        }
303 5
        if (isset($reference->{'default-discriminator-value'})) {
304 1
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
305
        }
306 5
        if (isset($reference->{'sort'})) {
307 View Code Duplication
            foreach ($reference->{'sort'}->{'sort'} as $sort) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
308
                $attr = $sort->attributes();
309
                $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc';
310
            }
311
        }
312 5
        if (isset($reference->{'criteria'})) {
313 View Code Duplication
            foreach ($reference->{'criteria'}->{'criteria'} as $criteria) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
314
                $attr = $criteria->attributes();
315
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
316
            }
317
        }
318 5
        if (isset($attributes['not-saved'])) {
319
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
320
        }
321 5
        if (isset($attributes['also-load'])) {
322
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
323
        }
324 5
        if (isset($reference->{'prime'})) {
325 1
            foreach ($reference->{'prime'}->{'field'} as $field) {
326 1
                $attr = $field->attributes();
327 1
                $mapping['prime'][] = (string) $attr['name'];
328
            }
329
        }
330
331 5
        $this->addFieldMapping($class, $mapping);
332 5
    }
333
334 3
    private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
335
    {
336 3
        $attributes = $xmlIndex->attributes();
337
338 3
        $keys = array();
339
340 3 View Code Duplication
        foreach ($xmlIndex->{'key'} as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
341 3
            $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
342
        }
343
344 3
        $options = array();
345
346 3
        if (isset($attributes['background'])) {
347
            $options['background'] = ('true' === (string) $attributes['background']);
348
        }
349 3
        if (isset($attributes['drop-dups'])) {
350
            $options['dropDups'] = ('true' === (string) $attributes['drop-dups']);
351
        }
352 3
        if (isset($attributes['name'])) {
353
            $options['name'] = (string) $attributes['name'];
354
        }
355 3
        if (isset($attributes['sparse'])) {
356
            $options['sparse'] = ('true' === (string) $attributes['sparse']);
357
        }
358 3
        if (isset($attributes['unique'])) {
359 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
360
        }
361
362 3 View Code Duplication
        if (isset($xmlIndex->{'option'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
363 1
            foreach ($xmlIndex->{'option'} as $option) {
364 1
                $value = (string) $option['value'];
365 1
                if ($value === 'true') {
366
                    $value = true;
367 1
                } elseif ($value === 'false') {
368 1
                    $value = false;
369 1
                } elseif (is_numeric($value)) {
370 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
371
                }
372 1
                $options[(string) $option['name']] = $value;
373
            }
374
        }
375
376 3
        if (isset($xmlIndex->{'partial-filter-expression'})) {
377 3
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
378
379 3
            if (isset($partialFilterExpressionMapping->and)) {
380 2
                foreach ($partialFilterExpressionMapping->and as $and) {
381 2
                    if (! isset($and->field)) {
382 1
                        continue;
383
                    }
384
385 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
386 2
                    if (! $partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
387
                        continue;
388
                    }
389
390 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
391
                }
392 2
            } elseif (isset($partialFilterExpressionMapping->field)) {
393 2
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
394
395 2
                if ($partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
396 2
                    $options['partialFilterExpression'] = $partialFilterExpression;
397
                }
398
            }
399
        }
400
401 3
        $class->addIndex($keys, $options);
402 3
    }
403
404 3
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
405
    {
406 3
        $partialFilterExpression = [];
407 3
        foreach ($fields as $field) {
408 3
            $operator = (string) $field['operator'] ?: null;
409
410 3
            if (! isset($field['value'])) {
411 1
                if (! isset($field->field)) {
412
                    continue;
413
                }
414
415 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
416 1
                if (! $nestedExpression) {
417
                    continue;
418
                }
419
420 1
                $value = $nestedExpression;
421
            } else {
422 3
                $value = trim((string) $field['value']);
423
            }
424
425 3
            if ($value === 'true') {
426
                $value = true;
427 3
            } elseif ($value === 'false') {
428
                $value = false;
429 3
            } elseif (is_numeric($value)) {
430 2
                $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
431
            }
432
433 3
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
434
        }
435
436 3
        return $partialFilterExpression;
437
    }
438
439 2
    private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey)
440
    {
441 2
        $attributes = $xmlShardkey->attributes();
442
443 2
        $keys = array();
444 2
        $options = array();
445 2 View Code Duplication
        foreach ($xmlShardkey->{'key'} as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
446 2
            $keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc';
447
        }
448
449 2
        if (isset($attributes['unique'])) {
450 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
451
        }
452
453 2
        if (isset($attributes['numInitialChunks'])) {
454 1
            $options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
455
        }
456
457 2 View Code Duplication
        if (isset($xmlShardkey->{'option'})) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
458 1
            foreach ($xmlShardkey->{'option'} as $option) {
459 1
                $value = (string) $option['value'];
460 1
                if ($value === 'true') {
461 1
                    $value = true;
462 1
                } elseif ($value === 'false') {
463
                    $value = false;
464 1
                } elseif (is_numeric($value)) {
465 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
466
                }
467 1
                $options[(string) $option['name']] = $value;
468
            }
469
        }
470
471 2
        $class->setShardKey($keys, $options);
472 2
    }
473
474
    /**
475
     * Parses <read-preference> to a format suitable for the underlying driver.
476
     *
477
     * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference});
478
     *
479
     * @param \SimpleXMLElement $xmlReadPreference
480
     * @return array
481
     */
482 1
    private function transformReadPreference($xmlReadPreference)
483
    {
484 1
        $tags = null;
485 1
        if (isset($xmlReadPreference->{'tag-set'})) {
486 1
            $tags = [];
487 1
            foreach ($xmlReadPreference->{'tag-set'} as $tagSet) {
488 1
                $set = [];
489 1
                foreach ($tagSet->tag as $tag) {
490 1
                    $set[(string) $tag['name']] = (string) $tag['value'];
491
                }
492 1
                $tags[] = $set;
493
            }
494
        }
495 1
        return [(string) $xmlReadPreference['mode'], $tags];
496
    }
497
498
    /**
499
     * {@inheritDoc}
500
     */
501 9
    protected function loadMappingFile($file)
502
    {
503 9
        $result = array();
504 9
        $xmlElement = simplexml_load_file($file);
505
506 9
        foreach (array('document', 'embedded-document', 'mapped-superclass', 'query-result-document') as $type) {
507 9
            if (isset($xmlElement->$type)) {
508 9
                foreach ($xmlElement->$type as $documentElement) {
509 9
                    $documentName = (string) $documentElement['name'];
510 9
                    $result[$documentName] = $documentElement;
511
                }
512
            }
513
        }
514
515 9
        return $result;
516
    }
517
}
518