XmlClassMetadataLoader::loadProperty()   F
last analyzed

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 82
c 0
b 0
f 0
ccs 41
cts 41
cp 1
rs 0
cc 20
nc 524288
nop 1
crap 20

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
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Loader;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class XmlClassMetadataLoader extends AbstractFileClassMetadataLoader
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 388
    protected function loadFile($file)
23
    {
24 388
        $data = [];
25 388
        $xml = simplexml_import_dom($this->loadDocument($file));
26
27 360
        foreach ($xml->class as $class) {
28 360
            $data[(string) $class['name']] = $this->loadClass($class);
29
        }
30
31 360
        return $data;
32
    }
33
34
    /**
35
     * @param \SimpleXMLElement $class
36
     *
37
     * @return mixed[]
38
     */
39 360
    private function loadClass(\SimpleXMLElement $class)
40
    {
41 360
        $definition = [];
42
43 360
        if (isset($class['exclusion-policy'])) {
44 248
            $definition['exclusion_policy'] = (string) $class['exclusion-policy'];
45
        }
46
47 360
        if (isset($class['order'])) {
48 264
            $definition['order'] = (string) $class['order'];
49
        }
50
51 360
        if (isset($class['readable'])) {
52 240
            $definition['readable'] = (string) $class['readable'] === 'true';
53
        }
54
55 360
        if (isset($class['writable'])) {
56 240
            $definition['writable'] = (string) $class['writable'] === 'true';
57
        }
58
59 360
        if (isset($class['xml-root'])) {
60 248
            $definition['xml_root'] = (string) $class['xml-root'];
61
        }
62
63 360
        $properties = [];
64
65 360
        foreach ($class->property as $property) {
66 352
            $properties[(string) $property['name']] = $this->loadProperty($property);
67
        }
68
69 360
        $definition['properties'] = $properties;
70
71 360
        return $definition;
72
    }
73
74
    /**
75
     * @param \SimpleXMLElement $element
76
     *
77
     * @return mixed[]
78
     */
79 352
    private function loadProperty(\SimpleXMLElement $element)
80
    {
81 352
        $property = [];
82
83 352
        if (isset($element['alias'])) {
84 248
            $property['alias'] = (string) $element['alias'];
85
        }
86
87 352
        if (isset($element['type'])) {
88 248
            $property['type'] = (string) $element['type'];
89
        }
90
91 352
        if (isset($element['exclude'])) {
92 248
            $property['exclude'] = (string) $element['exclude'] === 'true';
93
        }
94
95 352
        if (isset($element['expose'])) {
96 240
            $property['expose'] = (string) $element['expose'] === 'true';
97
        }
98
99 352
        if (isset($element['accessor'])) {
100 248
            $property['accessor'] = (string) $element['accessor'];
101
        }
102
103 352
        if (isset($element['readable'])) {
104 240
            $property['readable'] = (string) $element['readable'] === 'true';
105
        }
106
107 352
        if (isset($element['writable'])) {
108 240
            $property['writable'] = (string) $element['writable'] === 'true';
109
        }
110
111 352
        if (isset($element['mutator'])) {
112 248
            $property['mutator'] = (string) $element['mutator'];
113
        }
114
115 352
        if (isset($element['since'])) {
116 248
            $property['since'] = (string) $element['since'];
117
        }
118
119 352
        if (isset($element['until'])) {
120 248
            $property['until'] = (string) $element['until'];
121
        }
122
123 352
        if (isset($element['max-depth'])) {
124 240
            $property['max_depth'] = (int) $element['max-depth'];
125
        }
126
127 352
        foreach ($element->group as $group) {
128 248
            $property['groups'][] = (string) $group;
129
        }
130
131 352
        if (isset($element['xml-attribute'])) {
132 240
            $property['xml_attribute'] = (string) $element['xml-attribute'] === 'true';
133
        }
134
135 352
        if (isset($element['xml-value'])) {
136 240
            $property['xml_value'] = (string) $element['xml-value'] === 'true';
137
        }
138
139 352
        if (isset($element['xml-inline'])) {
140 240
            $property['xml_inline'] = (string) $element['xml-inline'] === 'true';
141
        }
142
143 352
        if (isset($element['xml-entry'])) {
144 248
            $property['xml_entry'] = (string) $element['xml-entry'];
145
        }
146
147 352
        if (isset($element['xml-entry-attribute'])) {
148 248
            $property['xml_entry_attribute'] = (string) $element['xml-entry-attribute'];
149
        }
150
151 352
        if (isset($element['xml-key-as-attribute'])) {
152 240
            $property['xml_key_as_attribute'] = (string) $element['xml-key-as-attribute'] === 'true';
153
        }
154
155 352
        if (isset($element['xml-key-as-node'])) {
156 240
            $property['xml_key_as_node'] = (string) $element['xml-key-as-node'] === 'true';
157
        }
158
159 352
        return $property;
160
    }
161
162
    /**
163
     * @param string $file
164
     *
165
     * @return \DOMDocument
166
     */
167 388
    private function loadDocument($file)
168
    {
169 388
        $data = trim(@file_get_contents($file));
170
171 388
        if (empty($data)) {
172 4
            throw new \InvalidArgumentException(sprintf('The XML mapping file "%s" is not valid.', $file));
173
        }
174
175 384
        $internalErrors = libxml_use_internal_errors();
176 384
        $disableEntities = libxml_disable_entity_loader();
177
178 384
        $this->setLibXmlState(true, true);
179
180 384
        $document = new \DOMDocument();
181 384
        $document->validateOnParse = true;
182
183 384
        if (!@$document->loadXML($data, LIBXML_NONET | LIBXML_COMPACT)) {
184 8
            throw $this->createException($file, $internalErrors, $disableEntities);
185
        }
186
187 376
        $document->normalizeDocument();
188
189 376
        if (!@$document->schemaValidateSource(file_get_contents(__DIR__.'/../Resource/mapping.xsd'))) {
190 16
            throw $this->createException($file, $internalErrors, $disableEntities);
191
        }
192
193 360
        foreach ($document->childNodes as $child) {
194 360
            if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
195 180
                throw new \InvalidArgumentException('The document type is not allowed.');
196
            }
197
        }
198
199 360
        $this->setLibXmlState($internalErrors, $disableEntities);
200
201 360
        return $document;
202
    }
203
204
    /**
205
     * @param string $file
206
     * @param bool   $internalErrors
207
     * @param bool   $disableEntities
208
     *
209
     * @return \InvalidArgumentException
210
     */
211 24
    private function createException($file, $internalErrors, $disableEntities)
212
    {
213 24
        $errors = [];
214
215 24 View Code Duplication
        foreach (libxml_get_errors() as $error) {
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...
216 24
            $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
217 24
                $error->level === LIBXML_ERR_WARNING ? 'WARNING' : 'ERROR',
218 24
                $error->code,
219 24
                trim($error->message),
220 18
                $file,
221 24
                $error->line,
222 24
                $error->column
223
            );
224
        }
225
226 24
        $this->setLibXmlState($internalErrors, $disableEntities);
227
228 24
        return new \InvalidArgumentException(implode(PHP_EOL, $errors));
229
    }
230
231
    /**
232
     * @param bool $internalErrors
233
     * @param bool $disableEntities
234
     */
235 384
    private function setLibXmlState($internalErrors, $disableEntities)
236
    {
237 384
        libxml_use_internal_errors($internalErrors);
238 384
        libxml_disable_entity_loader($disableEntities);
239 384
        libxml_clear_errors();
240 384
    }
241
}
242