Completed
Pull Request — 1.0.x (#1431)
by Andreas
05:30
created

XmlDriver::loadMetadataForClass()   F

Complexity

Conditions 50
Paths > 20000

Size

Total Lines 145
Code Lines 92

Duplication

Lines 16
Ratio 11.03 %

Code Coverage

Tests 114
CRAP Score 57.2951

Importance

Changes 0
Metric Value
dl 16
loc 145
ccs 114
cts 133
cp 0.8571
rs 2
c 0
b 0
f 0
cc 50
eloc 92
nc 2949121
nop 2
crap 57.2951

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