Completed
Push — master ( 3e05ae...99069b )
by Asmir
12s
created

XmlDriver   F

Complexity

Total Complexity 93

Size/Duplication

Total Lines 353
Duplicated Lines 0 %

Test Coverage

Coverage 78.65%

Importance

Changes 0
Metric Value
dl 0
loc 353
ccs 151
cts 192
cp 0.7865
rs 1.5789
c 0
b 0
f 0
wmc 93

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getExtension() 0 3 1
F loadMetadataFromFile() 0 333 91

How to fix   Complexity   

Complex Class

Complex classes like XmlDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use XmlDriver, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
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 31
    public function __construct(FileLocatorInterface $locator, PropertyNamingStrategyInterface $namingStrategy, ParserInterface $typeParser = null)
46
    {
47 31
        parent::__construct($locator);
48 31
        $this->typeParser = $typeParser ?? new Parser();
49 31
        $this->namingStrategy = $namingStrategy;
50 31
    }
51
52 31
    protected function loadMetadataFromFile(\ReflectionClass $class, $path):?\Metadata\ClassMetadata
53
    {
54 31
        $previous = libxml_use_internal_errors(true);
55 31
        libxml_clear_errors();
56
57 31
        $elem = simplexml_load_file($path);
58 31
        libxml_use_internal_errors($previous);
59
60 31
        if (false === $elem) {
61 1
            throw new XmlErrorException(libxml_get_last_error());
62
        }
63
64 30
        $metadata = new ClassMetadata($name = $class->name);
65 30
        if (!$elems = $elem->xpath("./class[@name = '" . $name . "']")) {
66
            throw new RuntimeException(sprintf('Could not find class %s inside XML element.', $name));
67
        }
68 30
        $elem = reset($elems);
69
70 30
        $metadata->fileResources[] = $path;
71 30
        $metadata->fileResources[] = $class->getFileName();
72 30
        $exclusionPolicy = strtoupper((string)$elem->attributes()->{'exclusion-policy'}) ?: 'NONE';
73 30
        $excludeAll = null !== ($exclude = $elem->attributes()->exclude) ? 'true' === strtolower((string)$exclude) : false;
74 30
        $classAccessType = (string)($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
75
76 30
        $propertiesMetadata = [];
77 30
        $propertiesNodes = [];
78
79 30
        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 30
        if (null !== $xmlRootName = $elem->attributes()->{'xml-root-name'}) {
84 7
            $metadata->xmlRootName = (string)$xmlRootName;
85
        }
86
87 30
        if (null !== $xmlRootNamespace = $elem->attributes()->{'xml-root-namespace'}) {
88 1
            $metadata->xmlRootNamespace = (string)$xmlRootNamespace;
89
        }
90 30
        if (null !== $xmlRootPrefix = $elem->attributes()->{'xml-root-prefix'}) {
91 1
            $metadata->xmlRootPrefix = (string)$xmlRootPrefix;
92
        }
93
94 30
        $readOnlyClass = 'true' === strtolower((string)$elem->attributes()->{'read-only'});
95
96 30
        $discriminatorFieldName = (string)$elem->attributes()->{'discriminator-field-name'};
97 30
        $discriminatorMap = [];
98 30
        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 30
        if ('true' === (string)$elem->attributes()->{'discriminator-disabled'}) {
107
            $metadata->discriminatorDisabled = true;
108 30
        } 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 30
        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 30
        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 30
        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 30
        if (!$excludeAll) {
159
160 30
            foreach ($class->getProperties() as $property) {
161 25
                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 24
                $propertiesMetadata[] = new PropertyMetadata($name, $pName = $property->getName());
166 24
                $pElems = $elem->xpath("./property[@name = '" . $pName . "']");
167
168 24
                $propertiesNodes[] = $pElems ? reset($pElems) : null;
169
            }
170
171 30
            foreach ($propertiesMetadata as $propertyKey => $pMetadata) {
172
173 26
                $isExclude = false;
174 26
                $isExpose = $pMetadata instanceof VirtualPropertyMetadata
175 26
                    || $pMetadata instanceof ExpressionPropertyMetadata;
176
177 26
                $pElem = $propertiesNodes[$propertyKey];
178 26
                if (!empty($pElem)) {
179
180 26
                    if (null !== $exclude = $pElem->attributes()->exclude) {
181 2
                        $isExclude = 'true' === strtolower((string)$exclude);
182
                    }
183
184 26
                    if ($isExclude) {
185 2
                        continue;
186
                    }
187
188 24
                    if (null !== $expose = $pElem->attributes()->expose) {
189 2
                        $isExpose = 'true' === strtolower((string)$expose);
190
                    }
191
192 24
                    if (null !== $excludeIf = $pElem->attributes()->{'exclude-if'}) {
193 1
                        $pMetadata->excludeIf = (string)$excludeIf;
194
                    }
195
196 24
                    if (null !== $skip = $pElem->attributes()->{'skip-when-empty'}) {
197 1
                        $pMetadata->skipWhenEmpty = 'true' === strtolower((string)$skip);
198
                    }
199
200 24
                    if (null !== $excludeIf = $pElem->attributes()->{'expose-if'}) {
201 1
                        $pMetadata->excludeIf = "!(" . (string)$excludeIf . ")";
202 1
                        $isExpose = true;
203
                    }
204
205 24
                    if (null !== $version = $pElem->attributes()->{'since-version'}) {
206
                        $pMetadata->sinceVersion = (string)$version;
207
                    }
208
209 24
                    if (null !== $version = $pElem->attributes()->{'until-version'}) {
210
                        $pMetadata->untilVersion = (string)$version;
211
                    }
212
213 24
                    if (null !== $serializedName = $pElem->attributes()->{'serialized-name'}) {
214 6
                        $pMetadata->serializedName = (string)$serializedName;
215
                    }
216
217 24
                    if (null !== $type = $pElem->attributes()->type) {
218 15
                        $pMetadata->setType($this->typeParser->parse((string)$type));
219 12
                    } elseif (isset($pElem->type)) {
220 4
                        $pMetadata->setType($this->typeParser->parse((string)$pElem->type));
221
                    }
222
223 24
                    if (null !== $groups = $pElem->attributes()->groups) {
224 2
                        $pMetadata->groups = preg_split('/\s*,\s*/', trim((string)$groups));
225 23
                    } elseif (isset($pElem->groups)) {
226 1
                        $pMetadata->groups = (array)$pElem->groups->value;
227
                    }
228
229 24
                    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 24
                    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 24
                    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 24
                    if (isset($pElem->attributes()->{'xml-attribute'})) {
286 4
                        $pMetadata->xmlAttribute = 'true' === (string)$pElem->attributes()->{'xml-attribute'};
287
                    }
288
289 24
                    if (isset($pElem->attributes()->{'xml-attribute-map'})) {
290
                        $pMetadata->xmlAttributeMap = 'true' === (string)$pElem->attributes()->{'xml-attribute-map'};
291
                    }
292
293 24
                    if (isset($pElem->attributes()->{'xml-value'})) {
294 2
                        $pMetadata->xmlValue = 'true' === (string)$pElem->attributes()->{'xml-value'};
295
                    }
296
297 24
                    if (isset($pElem->attributes()->{'xml-key-value-pairs'})) {
298 1
                        $pMetadata->xmlKeyValuePairs = 'true' === (string)$pElem->attributes()->{'xml-key-value-pairs'};
299
                    }
300
301 24
                    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 24
                    if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
307 1
                        $pMetadata->readOnly = 'true' === strtolower((string)$readOnly);
308
                    } else {
309 23
                        $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
310
                    }
311
312 24
                    $getter = $pElem->attributes()->{'accessor-getter'};
313 24
                    $setter = $pElem->attributes()->{'accessor-setter'};
314 24
                    $pMetadata->setAccessor(
315 24
                        (string)($pElem->attributes()->{'access-type'} ?: $classAccessType),
316 24
                        $getter ? (string)$getter : null,
317 24
                        $setter ? (string)$setter : null
318
                    );
319
320 24
                    if (null !== $inline = $pElem->attributes()->inline) {
321 2
                        $pMetadata->inline = 'true' === strtolower((string)$inline);
322
                    }
323
                }
324
325 26
                if ($pMetadata->inline) {
326 2
                    $metadata->isList = $metadata->isList || PropertyMetadata::isCollectionList($pMetadata->type);
327 2
                    $metadata->isMap = $metadata->isMap || PropertyMetadata::isCollectionMap($pMetadata->type);
328
                }
329
330 26
                if (!$pMetadata->serializedName) {
331 26
                    $pMetadata->serializedName = $this->namingStrategy->translateName($pMetadata);
332
                }
333
334 26
                if (!empty($pElem) && null !== $name = $pElem->attributes()->name) {
335 21
                    $pMetadata->name = (string)$name;
336
                }
337
338 26
                if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)
339 26
                    || (ExclusionPolicy::ALL === (string)$exclusionPolicy && $isExpose)
340
                ) {
341
342
343 26
                    $metadata->addPropertyMetadata($pMetadata);
344
                }
345
            }
346
        }
347
348 30
        foreach ($elem->xpath('./callback-method') as $method) {
349
            if (!isset($method->attributes()->type)) {
350
                throw new RuntimeException('The type attribute must be set for all callback-method elements.');
351
            }
352
            if (!isset($method->attributes()->name)) {
353
                throw new RuntimeException('The name attribute must be set for all callback-method elements.');
354
            }
355
356
            switch ((string)$method->attributes()->type) {
357
                case 'pre-serialize':
358
                    $metadata->addPreSerializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
359
                    break;
360
361
                case 'post-serialize':
362
                    $metadata->addPostSerializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
363
                    break;
364
365
                case 'post-deserialize':
366
                    $metadata->addPostDeserializeMethod(new MethodMetadata($name, (string)$method->attributes()->name));
367
                    break;
368
369
                case 'handler':
370
                    if (!isset($method->attributes()->format)) {
371
                        throw new RuntimeException('The format attribute must be set for "handler" callback methods.');
372
                    }
373
                    if (!isset($method->attributes()->direction)) {
374
                        throw new RuntimeException('The direction attribute must be set for "handler" callback methods.');
375
                    }
376
377
                    break;
378
379
                default:
380
                    throw new RuntimeException(sprintf('The type "%s" is not supported.', $method->attributes()->name));
381
            }
382
        }
383
384 30
        return $metadata;
385
    }
386
387 30
    protected function getExtension():string
388
    {
389 30
        return 'xml';
390
    }
391
}
392