Completed
Push — master ( 6b2ea7...c30cd0 )
by Asmir
06:14 queued 03:19
created

XmlDriver::loadMetadataFromFile()   F

Complexity

Conditions 88
Paths > 20000

Size

Total Lines 328
Code Lines 186

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 140
CRAP Score 183.2078

Importance

Changes 0
Metric Value
cc 88
eloc 186
nc 316450
nop 2
dl 0
loc 328
ccs 140
cts 182
cp 0.7692
crap 183.2078
rs 2
c 0
b 0
f 0

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
declare(strict_types=1);
4
5
/*
6
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
namespace JMS\Serializer\Metadata\Driver;
22
23
use JMS\Serializer\Annotation\ExclusionPolicy;
24
use JMS\Serializer\Exception\RuntimeException;
25
use JMS\Serializer\Exception\XmlErrorException;
26
use JMS\Serializer\Metadata\ClassMetadata;
27
use JMS\Serializer\Metadata\ExpressionPropertyMetadata;
28
use JMS\Serializer\Metadata\PropertyMetadata;
29
use JMS\Serializer\Metadata\VirtualPropertyMetadata;
30
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
31
use JMS\Serializer\Type\Parser;
32
use JMS\Serializer\Type\ParserInterface;
33
use Metadata\Driver\AbstractFileDriver;
34
use Metadata\Driver\FileLocatorInterface;
35
use Metadata\MethodMetadata;
36
37
class XmlDriver extends AbstractFileDriver
38
{
39
    private $typeParser;
40
    /**
41
     * @var PropertyNamingStrategyInterface
42
     */
43
    private $namingStrategy;
44
45 29
    public function __construct(FileLocatorInterface $locator, PropertyNamingStrategyInterface $namingStrategy, ParserInterface $typeParser = null)
46
    {
47 29
        parent::__construct($locator);
48 29
        $this->typeParser = $typeParser ?? new Parser();
49 29
        $this->namingStrategy = $namingStrategy;
50 29
    }
51
52 29
    protected function loadMetadataFromFile(\ReflectionClass $class, $path):?\Metadata\ClassMetadata
53
    {
54 29
        $previous = libxml_use_internal_errors(true);
55 29
        libxml_clear_errors();
56
57 29
        $elem = simplexml_load_file($path);
58 29
        libxml_use_internal_errors($previous);
59
60 29
        if (false === $elem) {
61 1
            throw new XmlErrorException(libxml_get_last_error());
62
        }
63
64 28
        $metadata = new ClassMetadata($name = $class->name);
65 28
        if (!$elems = $elem->xpath("./class[@name = '" . $name . "']")) {
66
            throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
67
        }
68 28
        $elem = reset($elems);
69
70 28
        $metadata->fileResources[] = $path;
71 28
        $metadata->fileResources[] = $class->getFileName();
72 28
        $exclusionPolicy = strtoupper((string)$elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
73 28
        $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower((string)$exclude) : false;
74 28
        $classAccessType = (string)($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
75
76 28
        $propertiesMetadata = [];
77 28
        $propertiesNodes = [];
78
79 28
        if (null !== $accessorOrder = $elem->attributes()->{'accessor-order'}) {
80
            $metadata->setAccessorOrder((string)$accessorOrder, preg_split('/\s*,\s*/', (string)$elem->attributes()->{'custom-accessor-order'}));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/\s*,\s*/', ...custom-accessor-order') can also be of type false; however, parameter $customOrder of JMS\Serializer\Metadata\...ata::setAccessorOrder() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $metadata->setAccessorOrder((string)$accessorOrder, /** @scrutinizer ignore-type */ preg_split('/\s*,\s*/', (string)$elem->attributes()->{'custom-accessor-order'}));
Loading history...
81
        }
82
83 28
        if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
84 7
            $metadata->xmlRootName = (string)$xmlRootName;
85
        }
86
87 28
        if (null !== $xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'}) {
88 1
            $metadata->xmlRootNamespace = (string)$xmlRootNamespace;
89
        }
90 28
        if (null !== $xmlRootPrefix = $elem->attributes()->{'xml-root-prefix'}) {
91 1
            $metadata->xmlRootPrefix = (string)$xmlRootPrefix;
92
        }
93
94 28
        $readOnlyClass = 'true' === strtolower((string)$elem->attributes()->{'read-only'});
95
96 28
        $discriminatorFieldName = (string)$elem->attributes()->{'discriminator-field-name'};
97 28
        $discriminatorMap = [];
98 28
        foreach ($elem->xpath('./discriminator-class') as $entry) {
99 5
            if (!isset($entry->attributes()->value)) {
100
                throw new RuntimeException('Each discriminator-class element must have a "value" attribute.');
101
            }
102
103 5
            $discriminatorMap[(string)$entry->attributes()->value] = (string)$entry;
104
        }
105
106 28
        if ('true' === (string)$elem->attributes()->{'discriminator-disabled'}) {
107
            $metadata->discriminatorDisabled = true;
108 28
        } elseif (!empty($discriminatorFieldName) || !empty($discriminatorMap)) {
109
110 5
            $discriminatorGroups = [];
111 5
            foreach ($elem->xpath('./discriminator-groups/group') as $entry) {
112 1
                $discriminatorGroups[] = (string)$entry;
113
            }
114 5
            $metadata->setDiscriminator($discriminatorFieldName, $discriminatorMap, $discriminatorGroups);
115
        }
116
117 28
        foreach ($elem->xpath('./xml-namespace') as $xmlNamespace) {
118 3
            if (!isset($xmlNamespace->attributes()->uri)) {
119
                throw new RuntimeException('The prefix attribute must be set for all xml-namespace elements.');
120
            }
121
122 3
            if (isset($xmlNamespace->attributes()->prefix)) {
123 3
                $prefix = (string)$xmlNamespace->attributes()->prefix;
124
            } else {
125 2
                $prefix = null;
126
            }
127
128 3
            $metadata->registerNamespace((string)$xmlNamespace->attributes()->uri, $prefix);
129
        }
130
131 28
        foreach ($elem->xpath('./xml-discriminator') as $xmlDiscriminator) {
132 3
            if (isset($xmlDiscriminator->attributes()->attribute)) {
133 2
                $metadata->xmlDiscriminatorAttribute = (string)$xmlDiscriminator->attributes()->attribute === 'true';
134
            }
135 3
            if (isset($xmlDiscriminator->attributes()->cdata)) {
136 1
                $metadata->xmlDiscriminatorCData = (string)$xmlDiscriminator->attributes()->cdata === 'true';
137
            }
138 3
            if (isset($xmlDiscriminator->attributes()->namespace)) {
139 3
                $metadata->xmlDiscriminatorNamespace = (string)$xmlDiscriminator->attributes()->namespace;
140
            }
141
        }
142
143 28
        foreach ($elem->xpath('./virtual-property') as $method) {
144
145 7
            if (isset($method->attributes()->expression)) {
146 2
                $virtualPropertyMetadata = new ExpressionPropertyMetadata($name, (string)$method->attributes()->name, (string)$method->attributes()->expression);
147
            } else {
148 6
                if (!isset($method->attributes()->method)) {
149
                    throw new RuntimeException('The method attribute must be set for all virtual-property elements.');
150
                }
151 6
                $virtualPropertyMetadata = new VirtualPropertyMetadata($name, (string)$method->attributes()->method);
152
            }
153
154 7
            $propertiesMetadata[] = $virtualPropertyMetadata;
155 7
            $propertiesNodes[] = $method;
156
        }
157
158 28
        if (!$excludeAll) {
159
160 28
            foreach ($class->getProperties() as $property) {
161 23
                if ($property->class !== $name || (isset($property->info) && $property->info['class'] !== $name)) {
0 ignored issues
show
Bug introduced by
The property info does not seem to exist on ReflectionProperty.
Loading history...
162 2
                    continue;
163
                }
164
165 22
                $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName());
166 22
                $pElems = $elem->xpath("./property[@name = '" . $pName . "']");
167
168 22
                $propertiesNodes[] = $pElems ? reset($pElems) : null;
169
            }
170
171 28
            foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
172
173 24
                $isExclude = false;
174 24
                $isExpose = $pMetadata instanceof VirtualPropertyMetadata
175 24
                    || $pMetadata instanceof ExpressionPropertyMetadata;
176
177 24
                $pElem = $propertiesNodes[$propertyKey];
178 24
                if (!empty($pElem)) {
179
180 24
                    if (null !== $exclude = $pElem->attributes()->exclude) {
181 2
                        $isExclude = 'true' === strtolower((string)$exclude);
182
                    }
183
184 24
                    if ($isExclude) {
185 2
                        continue;
186
                    }
187
188 22
                    if (null !== $expose = $pElem->attributes()->expose) {
189 2
                        $isExpose = 'true' === strtolower((string)$expose);
190
                    }
191
192 22
                    if (null !== $excludeIf = $pElem->attributes()->{'exclude-if'}) {
193 1
                        $pMetadata->excludeIf = (string)$excludeIf;
194
                    }
195
196 22
                    if (null !== $skip = $pElem->attributes()->{'skip-when-empty'}) {
197 1
                        $pMetadata->skipWhenEmpty = 'true' === strtolower((string)$skip);
198
                    }
199
200 22
                    if (null !== $excludeIf = $pElem->attributes()->{'expose-if'}) {
201 1
                        $pMetadata->excludeIf = "!(" . (string)$excludeIf . ")";
202 1
                        $isExpose = true;
203
                    }
204
205 22
                    if (null !== $version = $pElem->attributes()->{'since-version'}) {
206
                        $pMetadata->sinceVersion = (string)$version;
207
                    }
208
209 22
                    if (null !== $version = $pElem->attributes()->{'until-version'}) {
210
                        $pMetadata->untilVersion = (string)$version;
211
                    }
212
213 22
                    if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
214 6
                        $pMetadata->serializedName = (string)$serializedName;
215
                    }
216
217 22
                    if (null !== $type = $pElem->attributes()->type) {
218 15
                        $pMetadata->setType($this->typeParser->parse((string)$type));
219 10
                    } elseif (isset($pElem->type)) {
220 2
                        $pMetadata->setType($this->typeParser->parse((string)$pElem->type));
221
                    }
222
223 22
                    if (null !== $groups = $pElem->attributes()->groups) {
224 2
                        $pMetadata->groups = preg_split('/\s*,\s*/', trim((string)$groups));
225 21
                    } elseif (isset($pElem->groups)) {
226 1
                        $pMetadata->groups = (array)$pElem->groups->value;
227
                    }
228
229 22
                    if (isset($pElem->{'xml-list'})) {
230
231 2
                        $pMetadata->xmlCollection = true;
232
233 2
                        $colConfig = $pElem->{'xml-list'};
234 2
                        if (isset($colConfig->attributes()->inline)) {
235 1
                            $pMetadata->xmlCollectionInline = 'true' === (string)$colConfig->attributes()->inline;
236
                        }
237
238 2
                        if (isset($colConfig->attributes()->{'entry-name'})) {
239 1
                            $pMetadata->xmlEntryName = (string)$colConfig->attributes()->{'entry-name'};
240
                        }
241
242 2
                        if (isset($colConfig->attributes()->{'skip-when-empty'})) {
243 1
                            $pMetadata->xmlCollectionSkipWhenEmpty = 'true' === (string)$colConfig->attributes()->{'skip-when-empty'};
244
                        } else {
245 1
                            $pMetadata->xmlCollectionSkipWhenEmpty = true;
246
                        }
247
248 2
                        if (isset($colConfig->attributes()->namespace)) {
249
                            $pMetadata->xmlEntryNamespace = (string)$colConfig->attributes()->namespace;
250
                        }
251
                    }
252
253 22
                    if (isset($pElem->{'xml-map'})) {
254
                        $pMetadata->xmlCollection = true;
255
256
                        $colConfig = $pElem->{'xml-map'};
257
                        if (isset($colConfig->attributes()->inline)) {
258
                            $pMetadata->xmlCollectionInline = 'true' === (string)$colConfig->attributes()->inline;
259
                        }
260
261
                        if (isset($colConfig->attributes()->{'entry-name'})) {
262
                            $pMetadata->xmlEntryName = (string)$colConfig->attributes()->{'entry-name'};
263
                        }
264
265
                        if (isset($colConfig->attributes()->namespace)) {
266
                            $pMetadata->xmlEntryNamespace = (string)$colConfig->attributes()->namespace;
267
                        }
268
269
                        if (isset($colConfig->attributes()->{'key-attribute-name'})) {
270
                            $pMetadata->xmlKeyAttribute = (string)$colConfig->attributes()->{'key-attribute-name'};
271
                        }
272
                    }
273
274 22
                    if (isset($pElem->{'xml-element'})) {
275 4
                        $colConfig = $pElem->{'xml-element'};
276 4
                        if (isset($colConfig->attributes()->cdata)) {
277 2
                            $pMetadata->xmlElementCData = 'true' === (string)$colConfig->attributes()->cdata;
278
                        }
279
280 4
                        if (isset($colConfig->attributes()->namespace)) {
281 3
                            $pMetadata->xmlNamespace = (string)$colConfig->attributes()->namespace;
282
                        }
283
                    }
284
285 22
                    if (isset($pElem->attributes()->{'xml-attribute'})) {
286 4
                        $pMetadata->xmlAttribute = 'true' === (string)$pElem->attributes()->{'xml-attribute'};
287
                    }
288
289 22
                    if (isset($pElem->attributes()->{'xml-attribute-map'})) {
290
                        $pMetadata->xmlAttributeMap = 'true' === (string)$pElem->attributes()->{'xml-attribute-map'};
291
                    }
292
293 22
                    if (isset($pElem->attributes()->{'xml-value'})) {
294 2
                        $pMetadata->xmlValue = 'true' === (string)$pElem->attributes()->{'xml-value'};
295
                    }
296
297 22
                    if (isset($pElem->attributes()->{'xml-key-value-pairs'})) {
298 1
                        $pMetadata->xmlKeyValuePairs = 'true' === (string)$pElem->attributes()->{'xml-key-value-pairs'};
299
                    }
300
301 22
                    if (isset($pElem->attributes()->{'max-depth'})) {
302 1
                        $pMetadata->maxDepth = (int)$pElem->attributes()->{'max-depth'};
303
                    }
304
305
                    //we need read-only before setter and getter set, because that method depends on flag being set
306 22
                    if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
307 1
                        $pMetadata->readOnly = 'true' === strtolower((string)$readOnly);
308
                    } else {
309 21
                        $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
310
                    }
311
312 22
                    $getter = $pElem->attributes()->{'accessor-getter'};
313 22
                    $setter = $pElem->attributes()->{'accessor-setter'};
314 22
                    $pMetadata->setAccessor(
315 22
                        (string)($pElem->attributes()->{'access-type'} ?: $classAccessType),
316 22
                        $getter ? (string)$getter : null,
317 22
                        $setter ? (string)$setter : null
318
                    );
319
320 22
                    if (null !== $inline = $pElem->attributes()->inline) {
321
                        $pMetadata->inline = 'true' === strtolower((string)$inline);
322
                    }
323
                }
324
325 24
                if (!$pMetadata->serializedName) {
326 24
                    $pMetadata->serializedName = $this->namingStrategy->translateName($pMetadata);
327
                }
328
329 24
                if (!empty($pElem) && null !== $name = $pElem->attributes()->name) {
330 19
                    $pMetadata->name = (string)$name;
331
                }
332
333 24
                if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
334 24
                    || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)
335
                ) {
336
337
338 24
                    $metadata->addPropertyMetadata($pMetadata);
339
                }
340
            }
341
        }
342
343 28
        foreach ($elem->xpath('./callback-method') as $method) {
344
            if (!isset($method->attributes()->type)) {
345
                throw new RuntimeException('The type attribute must be set for all callback-method elements.');
346
            }
347
            if (!isset($method->attributes()->name)) {
348
                throw new RuntimeException('The name attribute must be set for all callback-method elements.');
349
            }
350
351
            switch ((string)$method->attributes()->type) {
352
                case 'pre-serialize':
353
                    $metadata->addPreSerializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
354
                    break;
355
356
                case 'post-serialize':
357
                    $metadata->addPostSerializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
358
                    break;
359
360
                case 'post-deserialize':
361
                    $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
362
                    break;
363
364
                case 'handler':
365
                    if (!isset($method->attributes()->format)) {
366
                        throw new RuntimeException('The format attribute must be set for "handler" callback methods.');
367
                    }
368
                    if (!isset($method->attributes()->direction)) {
369
                        throw new RuntimeException('The direction attribute must be set for "handler" callback methods.');
370
                    }
371
372
                    break;
373
374
                default:
375
                    throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
376
            }
377
        }
378
379 28
        return $metadata;
380
    }
381
382 28
    protected function getExtension():string
383
    {
384 28
        return 'xml';
385
    }
386
}
387