Completed
Pull Request — master (#1385)
by Andreas
08:02
created

XmlDriver::loadMetadataForClass()   F

Complexity

Conditions 51
Paths > 20000

Size

Total Lines 148
Code Lines 94

Duplication

Lines 16
Ratio 10.81 %

Code Coverage

Tests 82
CRAP Score 56.4164

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 16
loc 148
ccs 82
cts 94
cp 0.8723
rs 2
cc 51
eloc 94
nc 5898241
nop 2
crap 56.4164

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Mapping\Driver;
21
22
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
24
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata;
25
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
27
28
/**
29
 * XmlDriver is a metadata driver that enables mapping through XML files.
30
 *
31
 * @since       1.0
32
 */
33
class XmlDriver extends FileDriver
34
{
35
    const DEFAULT_FILE_EXTENSION = '.dcm.xml';
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 13
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
41
    {
42 13
        parent::__construct($locator, $fileExtension);
43 13
    }
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') {
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...
62 2
            $class->setCustomRepositoryClass(
63 2
                isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null
64
            );
65 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...
66 1
        } elseif ($xmlRoot->getName() == 'embedded-document') {
67 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...
68
        }
69 8
        if (isset($xmlRoot['db'])) {
70 4
            $class->setDatabase((string) $xmlRoot['db']);
71
        }
72 8
        if (isset($xmlRoot['collection'])) {
73 7
            if (isset($xmlRoot['capped-collection'])) {
74
                $config = array('name' => (string) $xmlRoot['collection']);
75
                $config['capped'] = (bool) $xmlRoot['capped-collection'];
76
                if (isset($xmlRoot['capped-collection-max'])) {
77
                    $config['max'] = (int) $xmlRoot['capped-collection-max'];
78
                }
79
                if (isset($xmlRoot['capped-collection-size'])) {
80
                    $config['size'] = (int) $xmlRoot['capped-collection-size'];
81
                }
82
                $class->setCollection($config);
83
            } else {
84 7
                $class->setCollection((string) $xmlRoot['collection']);
85
            }
86
        }
87 8
        if (isset($xmlRoot['inheritance-type'])) {
88
            $inheritanceType = (string) $xmlRoot['inheritance-type'];
89
            $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
90
        }
91 8 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...
92 2
            $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy'])));
93
        }
94 8
        if (isset($xmlRoot->{'discriminator-field'})) {
95 1
            $discrField = $xmlRoot->{'discriminator-field'};
96
            /* XSD only allows for "name", which is consistent with association
97
             * configurations, but fall back to "fieldName" for BC.
98
             */
99 1
            $class->setDiscriminatorField(
100 1
                isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName']
101
            );
102
        }
103 8 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...
104 1
            $map = array();
105 1
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
106 1
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
107
            }
108 1
            $class->setDiscriminatorMap($map);
109
        }
110 8
        if (isset($xmlRoot->{'default-discriminator-value'})) {
111 1
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
112
        }
113 8
        if (isset($xmlRoot->{'indexes'})) {
114 3
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
115 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...
116
            }
117
        }
118 8
        if (isset($xmlRoot->{'shard-key'})) {
119 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...
120
        }
121 8
        if (isset($xmlRoot['require-indexes'])) {
122 1
            $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']);
123
        }
124 8
        if (isset($xmlRoot['slave-okay'])) {
125 1
            $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']);
126
        }
127 8
        if (isset($xmlRoot->field)) {
128 8
            foreach ($xmlRoot->field as $field) {
129 8
                $mapping = array();
130 8
                $attributes = $field->attributes();
131 8
                foreach ($attributes as $key => $value) {
132 8
                    $mapping[$key] = (string) $value;
133 8
                    $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance');
134 8
                    if (in_array($key, $booleanAttributes)) {
135 8
                        $mapping[$key] = ('true' === $mapping[$key]);
136
                    }
137
                }
138 8
                if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
139 3
                    $mapping['options'] = array();
140 3
                    if (isset($field->{'id-generator-option'})) {
141 1
                        foreach ($field->{'id-generator-option'} as $generatorOptions) {
142 1
                            $attributesGenerator = iterator_to_array($generatorOptions->attributes());
143 1
                            if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) {
144 1
                                $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
145
                            }
146
                        }
147
                    }
148
                }
149
150 8
                if (isset($attributes['not-saved'])) {
151
                    $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
152
                }
153
154 8
                if (isset($attributes['also-load'])) {
155
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
156 8
                } elseif (isset($attributes['version'])) {
157 1
                    $mapping['version'] = ('true' === (string) $attributes['version']);
158 8
                } elseif (isset($attributes['lock'])) {
159 1
                    $mapping['lock'] = ('true' === (string) $attributes['lock']);
160
                }
161
162 8
                $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...
163
            }
164
        }
165 8
        if (isset($xmlRoot->{'embed-one'})) {
166 2
            foreach ($xmlRoot->{'embed-one'} as $embed) {
167 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...
168
            }
169
        }
170 8
        if (isset($xmlRoot->{'embed-many'})) {
171 2
            foreach ($xmlRoot->{'embed-many'} as $embed) {
172 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...
173
            }
174
        }
175 8
        if (isset($xmlRoot->{'reference-many'})) {
176 3
            foreach ($xmlRoot->{'reference-many'} as $reference) {
177 3
                $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...
178
            }
179
        }
180 8
        if (isset($xmlRoot->{'reference-one'})) {
181 3
            foreach ($xmlRoot->{'reference-one'} as $reference) {
182 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...
183
            }
184
        }
185 8
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
186 2
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
187 2
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
188
            }
189
        }
190 8
        if (isset($xmlRoot->{'also-load-methods'})) {
191 1
            foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
192 1
                $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
193
            }
194
        }
195 8
    }
196
197 8
    private function addFieldMapping(ClassMetadataInfo $class, $mapping)
198
    {
199 8 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...
200 8
            $name = $mapping['name'];
201 1
        } elseif (isset($mapping['fieldName'])) {
202 1
            $name = $mapping['fieldName'];
203
        } else {
204
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
205
        }
206
207 8
        $class->mapField($mapping);
208
209
        // Index this field if either "index", "unique", or "sparse" are set
210 8 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...
211 8
            return;
212
        }
213
214 2
        $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc');
215 2
        $options = array();
216
217 2
        if (isset($mapping['background'])) {
218
            $options['background'] = (boolean) $mapping['background'];
219
        }
220 2
        if (isset($mapping['drop-dups'])) {
221 1
            $options['dropDups'] = (boolean) $mapping['drop-dups'];
222
        }
223 2
        if (isset($mapping['index-name'])) {
224
            $options['name'] = (string) $mapping['index-name'];
225
        }
226 2
        if (isset($mapping['safe'])) {
227
            $options['safe'] = (boolean) $mapping['safe'];
228
        }
229 2
        if (isset($mapping['sparse'])) {
230 1
            $options['sparse'] = (boolean) $mapping['sparse'];
231
        }
232 2
        if (isset($mapping['unique'])) {
233 2
            $options['unique'] = (boolean) $mapping['unique'];
234
        }
235
236 2
        $class->addIndex($keys, $options);
237 2
    }
238
239 2
    private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type)
240
    {
241 2
        $attributes = $embed->attributes();
242 2
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
243
        $mapping = array(
244 2
            'type'           => $type,
245
            'embedded'       => true,
246 2
            'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
247 2
            'name'           => (string) $attributes['field'],
248 2
            'strategy'       => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
249
        );
250 2
        if (isset($attributes['fieldName'])) {
251 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
252
        }
253 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...
254 1
            $attr = $embed->{'discriminator-field'};
255 1
            $mapping['discriminatorField'] = (string) $attr['name'];
256
        }
257 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...
258 1
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
259 1
                $attr = $discriminatorMapping->attributes();
260 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
261
            }
262
        }
263 2
        if (isset($embed->{'default-discriminator-value'})) {
264 1
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
265
        }
266 2
        if (isset($attributes['not-saved'])) {
267
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
268
        }
269 2
        if (isset($attributes['also-load'])) {
270
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
271
        }
272 2
        $this->addFieldMapping($class, $mapping);
273 2
    }
274
275 3
    private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type)
276
    {
277 3
        $cascade = array_keys((array) $reference->cascade);
278 3
        if (1 === count($cascade)) {
279 2
            $cascade = current($cascade) ?: next($cascade);
280
        }
281 3
        $attributes = $reference->attributes();
282 3
        $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
283
        $mapping = array(
284 3
            'cascade'          => $cascade,
285 3
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ('true' === (string) $attributes['orphan-removal']) : false,
286 3
            'type'             => $type,
287
            'reference'        => true,
288 3
            'simple'           => isset($attributes['simple']) ? ('true' === (string) $attributes['simple']) : false,
289 3
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
290 3
            'name'             => (string) $attributes['field'],
291 3
            'strategy'         => isset($attributes['strategy']) ? (string) $attributes['strategy'] : $defaultStrategy,
292 3
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
293 3
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
294 3
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
295 3
            'limit'            => isset($attributes['limit']) ? (integer) $attributes['limit'] : null,
296 3
            'skip'             => isset($attributes['skip']) ? (integer) $attributes['skip'] : null,
297
        );
298
299 3
        if (isset($attributes['fieldName'])) {
300 1
            $mapping['fieldName'] = (string) $attributes['fieldName'];
301
        }
302 3 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...
303 1
            $attr = $reference->{'discriminator-field'};
304 1
            $mapping['discriminatorField'] = (string) $attr['name'];
305
        }
306 3 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...
307 1
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
308 1
                $attr = $discriminatorMapping->attributes();
309 1
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
310
            }
311
        }
312 3
        if (isset($reference->{'default-discriminator-value'})) {
313 1
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
314
        }
315 3
        if (isset($reference->{'sort'})) {
316 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...
317
                $attr = $sort->attributes();
318
                $mapping['sort'][(string) $attr['field']] = isset($attr['order']) ? (string) $attr['order'] : 'asc';
319
            }
320
        }
321 3
        if (isset($reference->{'criteria'})) {
322 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...
323
                $attr = $criteria->attributes();
324
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
325
            }
326
        }
327 3
        if (isset($attributes['not-saved'])) {
328
            $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']);
329
        }
330 3
        if (isset($attributes['also-load'])) {
331
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
332
        }
333 3
        $this->addFieldMapping($class, $mapping);
334 3
    }
335
336 3
    private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex)
337
    {
338 3
        $attributes = $xmlIndex->attributes();
339
340 3
        $keys = array();
341
342 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...
343 3
            $keys[(string) $key['name']] = isset($key['order']) ? (string) $key['order'] : 'asc';
344
        }
345
346 3
        $options = array();
347
348 3
        if (isset($attributes['background'])) {
349
            $options['background'] = ('true' === (string) $attributes['background']);
350
        }
351 3
        if (isset($attributes['drop-dups'])) {
352
            $options['dropDups'] = ('true' === (string) $attributes['drop-dups']);
353
        }
354 3
        if (isset($attributes['name'])) {
355
            $options['name'] = (string) $attributes['name'];
356
        }
357 3
        if (isset($attributes['safe'])) {
358
            $options['safe'] = ('true' === (string) $attributes['safe']);
359
        }
360 3
        if (isset($attributes['sparse'])) {
361
            $options['sparse'] = ('true' === (string) $attributes['sparse']);
362
        }
363 3
        if (isset($attributes['unique'])) {
364 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
365
        }
366
367 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...
368 1
            foreach ($xmlIndex->{'option'} as $option) {
369 1
                $value = (string) $option['value'];
370 1
                if ($value === 'true') {
371
                    $value = true;
372 1
                } elseif ($value === 'false') {
373 1
                    $value = false;
374 1
                } elseif (is_numeric($value)) {
375 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
376
                }
377 1
                $options[(string) $option['name']] = $value;
378
            }
379
        }
380
381 3
        if (isset($xmlIndex->{'partial-filter-expression'})) {
382 3
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
383
384 3
            if (isset($partialFilterExpressionMapping->and)) {
385 2
                foreach ($partialFilterExpressionMapping->and as $and) {
386 2
                    if (! isset($and->field)) {
387 1
                        continue;
388
                    }
389
390 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
391 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...
392
                        continue;
393
                    }
394
395 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
396
                }
397
            } elseif (isset($partialFilterExpressionMapping->field)) {
398 2
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
399
400 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...
401 2
                    $options['partialFilterExpression'] = $partialFilterExpression;
402
                }
403
            }
404
        }
405
406 3
        $class->addIndex($keys, $options);
407 3
    }
408
409 3
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
410
    {
411 3
        $partialFilterExpression = [];
412 3
        foreach ($fields as $field) {
413 3
            $operator = (string) $field['operator'] ?: null;
414
415 3
            if (! isset($field['value'])) {
416 1
                if (! isset($field->field)) {
417
                    continue;
418
                }
419
420 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
421 1
                if (! $nestedExpression) {
422
                    continue;
423
                }
424
425 1
                $value = $nestedExpression;
426
            } else {
427 3
                $value = trim((string) $field['value']);
428
            }
429
430 3
            if ($value === 'true') {
431
                $value = true;
432 3
            } elseif ($value === 'false') {
433
                $value = false;
434 3
            } elseif (is_numeric($value)) {
435 2
                $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
436
            }
437
438 3
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
439
        }
440
441 3
        return $partialFilterExpression;
442
    }
443
444 2
    private function setShardKey(ClassMetadataInfo $class, \SimpleXmlElement $xmlShardkey)
445
    {
446 2
        $attributes = $xmlShardkey->attributes();
447
448 2
        $keys = array();
449 2
        $options = array();
450 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...
451 2
            $keys[(string) $key['name']] = isset($key['order']) ? (string)$key['order'] : 'asc';
452
        }
453
454 2
        if (isset($attributes['unique'])) {
455 1
            $options['unique'] = ('true' === (string) $attributes['unique']);
456
        }
457
458 2
        if (isset($attributes['numInitialChunks'])) {
459 1
            $options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
460
        }
461
462 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...
463 1
            foreach ($xmlShardkey->{'option'} as $option) {
464 1
                $value = (string) $option['value'];
465 1
                if ($value === 'true') {
466 1
                    $value = true;
467 1
                } elseif ($value === 'false') {
468
                    $value = false;
469 1
                } elseif (is_numeric($value)) {
470 1
                    $value = preg_match('/^[-]?\d+$/', $value) ? (integer) $value : (float) $value;
471
                }
472 1
                $options[(string) $option['name']] = $value;
473
            }
474
        }
475
476 2
        $class->setShardKey($keys, $options);
477 2
    }
478
479
    /**
480
     * {@inheritDoc}
481
     */
482 8
    protected function loadMappingFile($file)
483
    {
484 8
        $result = array();
485 8
        $xmlElement = simplexml_load_file($file);
486
487 8
        foreach (array('document', 'embedded-document', 'mapped-superclass') as $type) {
488 8
            if (isset($xmlElement->$type)) {
489 8
                foreach ($xmlElement->$type as $documentElement) {
490 8
                    $documentName = (string) $documentElement['name'];
491 8
                    $result[$documentName] = $documentElement;
492
                }
493
            }
494
        }
495
496 8
        return $result;
497
    }
498
}
499