Completed
Push — master ( c03dba...972cf5 )
by Asmir
16s queued 15s
created

SchemaReader::addKnownSchemaLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GoetasWebservices\XML\XSDReader;
6
7
use Closure;
8
use DOMDocument;
9
use DOMElement;
10
use DOMNode;
11
use GoetasWebservices\XML\XSDReader\Documentation\DocumentationReader;
12
use GoetasWebservices\XML\XSDReader\Documentation\StandardDocumentationReader;
13
use GoetasWebservices\XML\XSDReader\Exception\IOException;
14
use GoetasWebservices\XML\XSDReader\Exception\TypeException;
15
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Attribute;
16
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeContainer;
17
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeDef;
18
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
19
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group as AttributeGroup;
20
use GoetasWebservices\XML\XSDReader\Schema\Element\AbstractElementSingle;
21
use GoetasWebservices\XML\XSDReader\Schema\Element\Element;
22
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementContainer;
23
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementDef;
24
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementRef;
25
use GoetasWebservices\XML\XSDReader\Schema\Element\Group;
26
use GoetasWebservices\XML\XSDReader\Schema\Element\GroupRef;
27
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetDefault;
28
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetFixed;
29
use GoetasWebservices\XML\XSDReader\Schema\Element\InterfaceSetMinMax;
30
use GoetasWebservices\XML\XSDReader\Schema\Exception\TypeNotFoundException;
31
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Base;
32
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Extension;
33
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction;
34
use GoetasWebservices\XML\XSDReader\Schema\Item;
35
use GoetasWebservices\XML\XSDReader\Schema\Schema;
36
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem;
37
use GoetasWebservices\XML\XSDReader\Schema\Type\BaseComplexType;
38
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
39
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexTypeSimpleContent;
40
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
41
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
42
use GoetasWebservices\XML\XSDReader\Utils\UrlUtils;
43
44
class SchemaReader
45
{
46
    public const XSD_NS = 'http://www.w3.org/2001/XMLSchema';
47
48
    public const XML_NS = 'http://www.w3.org/XML/1998/namespace';
49
50
    /**
51
     * @var DocumentationReader
52
     */
53
    private $documentationReader;
54
55
    /**
56
     * @var Schema[]
57
     */
58
    private $loadedFiles = [];
59
60
    /**
61
     * @var Schema[][]
62
     */
63
    private $loadedSchemas = [];
64
65
    /**
66
     * @var string[]
67
     */
68
    protected $knownLocationSchemas = [
69
        'http://www.w3.org/2001/xml.xsd' => (
70
            __DIR__.'/Resources/xml.xsd'
71
        ),
72
        'http://www.w3.org/2001/XMLSchema.xsd' => (
73
            __DIR__.'/Resources/XMLSchema.xsd'
74
        ),
75
        'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' => (
76
            __DIR__.'/Resources/oasis-200401-wss-wssecurity-secext-1.0.xsd'
77
        ),
78
        'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' => (
79
            __DIR__.'/Resources/oasis-200401-wss-wssecurity-utility-1.0.xsd'
80
        ),
81
        'https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => (
82
            __DIR__.'/Resources/xmldsig-core-schema.xsd'
83
        ),
84
        'http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd' => (
85
            __DIR__.'/Resources/xmldsig-core-schema.xsd'
86
        ),
87
    ];
88
89
    /**
90
     * @var string[]
91
     */
92
    protected $knownNamespaceSchemaLocations = [
93
        'http://www.w3.org/2000/09/xmldsig#' => (
94
            __DIR__.'/Resources/xmldsig-core-schema.xsd'
95
        ),
96
    ];
97
98
    /**
99
     * @var string[]
100 2
     */
101
    protected static $globalSchemaInfo = [
102 2
        self::XML_NS => 'http://www.w3.org/2001/xml.xsd',
103
        self::XSD_NS => 'http://www.w3.org/2001/XMLSchema.xsd',
104 2
    ];
105 1
106
    private function extractErrorMessage(): \Exception
107 2
    {
108 2
        $errors = [];
109
110 2
        foreach (libxml_get_errors() as $error) {
111
            $errors[] = sprintf("Error[%s] code %s: %s in '%s' at position %s:%s", $error->level, $error->code, trim($error->message), $error->file, $error->line, $error->column);
112
        }
113 85
        $e = new \Exception(implode('; ', $errors));
114
        libxml_use_internal_errors(false);
115 85
116 85
        return $e;
117
    }
118 85
119 85
    public function __construct(DocumentationReader $documentationReader = null)
120
    {
121
        if (null === $documentationReader) {
122
            $documentationReader = new StandardDocumentationReader();
123
        }
124
        $this->documentationReader = $documentationReader;
125
    }
126
127 1
    /**
128
     * Override remote location with a local file.
129 1
     *
130 1
     * @param string $remote remote schema URL
131
     * @param string $local  local file path
132
     */
133
    public function addKnownSchemaLocation(string $remote, string $local): void
134
    {
135
        $this->knownLocationSchemas[$remote] = $local;
136
    }
137
138
    /**
139 1
     * Specify schema location by namespace.
140
     * This can be used for schemas which import namespaces but do not specify schemaLocation attributes.
141 1
     *
142 1
     * @param string $namespace namespace
143
     * @param string $location  schema URL
144 74
     */
145
    public function addKnownNamespaceSchemaLocation(string $namespace, string $location): void
146
    {
147
        $this->knownNamespaceSchemaLocations[$namespace] = $location;
148 74
    }
149 74
150 74
    private function loadAttributeGroup(
151
        Schema $schema,
152
        DOMElement $node
153 74
    ): Closure {
154 74
        $attGroup = new AttributeGroup($schema, $node->getAttribute('name'));
155
        $attGroup->setDoc($this->getDocumentation($node));
156
        $schema->addAttributeGroup($attGroup);
157
158
        return function () use ($schema, $node, $attGroup): void {
159 74
            SchemaReader::againstDOMNodeList(
160 74
                $node,
161
                function (
162 74
                    DOMElement $node,
163 74
                    DOMElement $childNode
164 74
                ) use (
165 74
                    $schema,
166 74
                    $attGroup
167 74
                ): void {
168
                    switch ($childNode->localName) {
169 74
                        case 'attribute':
170 74
                            $attribute = $this->getAttributeFromAttributeOrRef(
171 74
                                $childNode,
172 1
                                $schema,
173 1
                                $node
174 1
                            );
175 1
                            $attGroup->addAttribute($attribute);
176 1
                            break;
177
                        case 'attributeGroup':
178 1
                            $this->findSomethingLikeAttributeGroup(
179
                                $schema,
180 74
                                $node,
181
                                $childNode,
182 74
                                $attGroup
183
                            );
184
                            break;
185 74
                    }
186
                }
187
            );
188
        };
189
    }
190 74
191 74
    private function getAttributeFromAttributeOrRef(
192
        DOMElement $childNode,
193
        Schema $schema,
194
        DOMElement $node
195
    ): AttributeItem {
196 74
        if ($childNode->hasAttribute('ref')) {
197
            $attribute = $this->findAttributeItem($schema, $node, $childNode->getAttribute('ref'));
198
        } else {
199 74
            /**
200
             * @var Attribute
201
             */
202 74
            $attribute = $this->loadAttribute($schema, $childNode);
203
        }
204
205
        return $attribute;
206 74
    }
207 74
208 74
    private function loadAttribute(Schema $schema, DOMElement $node): Attribute
209
    {
210 74
        $attribute = new Attribute($schema, $node->getAttribute('name'));
211 1
        $attribute->setDoc($this->getDocumentation($node));
212
        $this->fillItem($attribute, $node);
213 74
        $this->fillAttribute($attribute, $node);
214 1
215
        return $attribute;
216 74
    }
217 74
218
    private function fillAttribute(Attribute $attribute, DOMElement $node): void
219
    {
220 74
        if ($node->hasAttribute('nillable')) {
221
            $attribute->setNil($node->getAttribute('nillable') == 'true');
222
        }
223 74
        if ($node->hasAttribute('form')) {
224
            $attribute->setQualified($node->getAttribute('form') == 'qualified');
225
        }
226
        if ($node->hasAttribute('use')) {
227
            $attribute->setUse($node->getAttribute('use'));
228 74
        }
229 74
    }
230 74
231 74
    private function loadAttributeOrElementDef(
232
        Schema $schema,
233 74
        DOMElement $node,
234 74
        bool $attributeDef
235 74
    ): Closure {
236
        $name = $node->getAttribute('name');
237
        if ($attributeDef) {
238
            $attribute = new AttributeDef($schema, $name);
239 74
            if ($node->hasAttribute('fixed')) {
240 74
                $attribute->setFixed($node->getAttribute('fixed'));
241
            }
242
            if ($node->hasAttribute('default')) {
243 74
                $attribute->setDefault($node->getAttribute('default'));
244
            }
245 74
            $schema->addAttribute($attribute);
246
        } else {
247
            $attribute = new ElementDef($schema, $name);
248 74
            $attribute->setDoc($this->getDocumentation($node));
249
            $this->fillElement($attribute, $node);
250 74
            $schema->addElement($attribute);
251
        }
252
253
        return function () use ($attribute, $node): void {
254
            $this->fillItem($attribute, $node);
255
        };
256 74
    }
257
258 74
    private function loadAttributeDef(Schema $schema, DOMElement $node): Closure
259 74
    {
260
        return $this->loadAttributeOrElementDef($schema, $node, true);
261 74
    }
262 74
263
    private function getDocumentation(DOMElement $node): string
264
    {
265
        return $this->documentationReader->get($node);
266
    }
267 74
268 74
    /**
269
     * @return Closure[]
270 74
     */
271
    private function schemaNode(Schema $schema, DOMElement $node, Schema $parent = null): array
272 74
    {
273 74
        $this->setSchemaThingsFromNode($schema, $node, $parent);
274 74
        $functions = [];
275 74
276 74
        self::againstDOMNodeList(
277 74
            $node,
278 74
            function (
279 74
                DOMElement $node,
280 74
                DOMElement $childNode
281 74
            ) use (
282 74
                $schema,
283 74
                &$functions
284 74
            ): void {
285 74
                $callback = null;
286 74
287 74
                switch ($childNode->localName) {
288 74
                    case 'attributeGroup':
289 74
                        $callback = $this->loadAttributeGroup($schema, $childNode);
290 74
                        break;
291 74
                    case 'include':
292 74
                    case 'import':
293 74
                        $callback = $this->loadImport($schema, $childNode);
294 74
                        break;
295
                    case 'element':
296
                        $callback = $this->loadElementDef($schema, $childNode);
297 74
                        break;
298 74
                    case 'attribute':
299
                        $callback = $this->loadAttributeDef($schema, $childNode);
300 74
                        break;
301
                    case 'group':
302
                        $callback = $this->loadGroup($schema, $childNode);
303 74
                        break;
304
                    case 'complexType':
305
                        $callback = $this->loadComplexType($schema, $childNode);
306 74
                        break;
307
                    case 'simpleType':
308 74
                        $callback = $this->loadSimpleType($schema, $childNode);
309 74
                        break;
310
                }
311 74
312 74
                if ($callback instanceof Closure) {
313
                    $functions[] = $callback;
314 74
                }
315
            }
316
        );
317 74
318
        return $functions;
319 74
    }
320 74
321
    private function loadGroupRef(Group $referenced, DOMElement $node): GroupRef
322 74
    {
323
        $ref = new GroupRef($referenced);
324 74
        $ref->setDoc($this->getDocumentation($node));
325
326 74
        self::maybeSetMax($ref, $node);
327 74
        self::maybeSetMin($ref, $node);
328 74
329 7
        return $ref;
330
    }
331
332 74
    private static function maybeSetMax(InterfaceSetMinMax $ref, DOMElement $node): void
333
    {
334 74
        if ($node->hasAttribute('maxOccurs')) {
335
            $ref->setMax($node->getAttribute('maxOccurs') == 'unbounded' ? -1 : (int) $node->getAttribute('maxOccurs'));
336 74
        }
337 1
    }
338
339 74
    private static function maybeSetMin(InterfaceSetMinMax $ref, DOMElement $node): void
340
    {
341 74
        if ($node->hasAttribute('minOccurs')) {
342
            $ref->setMin((int) $node->getAttribute('minOccurs'));
343
            if ($ref->getMin() > $ref->getMax() && $ref->getMax() !== -1) {
344
                $ref->setMax($ref->getMin());
345 74
            }
346 74
        }
347 74
    }
348
349 74
    private static function maybeSetFixed(InterfaceSetFixed $ref, DOMElement $node): void
350 74
    {
351
        if ($node->hasAttribute('fixed')) {
352
            $ref->setFixed($node->getAttribute('fixed'));
353 74
        }
354 74
    }
355
356 74
    private static function maybeSetDefault(InterfaceSetDefault $ref, DOMElement $node): void
357 74
    {
358
        if ($node->hasAttribute('default')) {
359 74
            $ref->setDefault($node->getAttribute('default'));
360 74
        }
361
    }
362
363
    private function loadSequence(ElementContainer $elementContainer, DOMElement $node, int $max = null, int $min = null): void
364
    {
365 74
        $max =
366 74
            (
367 74
                (is_int($max) && (bool) $max) ||
368
                $node->getAttribute('maxOccurs') == 'unbounded' ||
369 74
                $node->getAttribute('maxOccurs') > 1
370 74
            )
371 74
                ? 2
372 74
                : null;
373 74
        $min =
374 74
            (
375
                $min === null &&
376 74
                !$node->hasAttribute('minOccurs')
377
            )
378 74
                ? null
379
                : (int) max((int) $min, $node->getAttribute('minOccurs'));
380 74
381
        self::againstDOMNodeList(
382
            $node,
383
            function (
384
                DOMElement $node,
385
                DOMElement $childNode
386
            ) use (
387 74
                $elementContainer,
388 74
                $max,
389 74
                $min
390 74
            ): void {
391 74
                $this->loadSequenceChildNode(
392 74
                    $elementContainer,
393 74
                    $node,
394 74
                    $childNode,
395 74
                    $max,
396
                    $min
397 74
                );
398 74
            }
399 74
        );
400 74
    }
401 74
402 74
    private function loadSequenceChildNode(
403 74
        ElementContainer $elementContainer,
404 74
        DOMElement $node,
405
        DOMElement $childNode,
406 74
        ?int $max,
407 74
        ?int $min = null
408 74
    ): void {
409 74
        switch ($childNode->localName) {
410 74
            case 'sequence':
411 74
            case 'choice':
412 74
            case 'all':
413
                $this->loadSequence(
414 74
                    $elementContainer,
415
                    $childNode,
416 74
                    $max,
417
                    $min
418 74
                );
419
                break;
420
            case 'element':
421
                $this->loadSequenceChildNodeLoadElement(
422
                    $elementContainer,
423
                    $node,
424
                    $childNode,
425 74
                    $max,
426 74
                    $min
427 74
                );
428 74
                break;
429
            case 'group':
430 74
                $this->addGroupAsElement(
431 74
                    $elementContainer->getSchema(),
432
                    $node,
433 74
                    $childNode,
434 74
                    $elementContainer
435
                );
436 74
                break;
437 74
        }
438
    }
439
440 74
    private function loadSequenceChildNodeLoadElement(
441
        ElementContainer $elementContainer,
442
        DOMElement $node,
443 74
        DOMElement $childNode,
444 74
        ?int $max,
445
        ?int $min
446
    ): void {
447 74
        if ($childNode->hasAttribute('ref')) {
448 74
            $elementDef = $this->findElement($elementContainer->getSchema(), $node, $childNode->getAttribute('ref'));
449 74
            $element = new ElementRef($elementDef);
450
            $element->setDoc($this->getDocumentation($childNode));
451
            $this->fillElement($element, $childNode);
452
        } else {
453 74
            $element = $this->loadElement($elementContainer->getSchema(), $childNode);
454 74
        }
455
456
        if ($min !== null) {
457 74
            $element->setMin($min);
458
        }
459
460
        if ($max > 1) {
461
            /*
462
            * although one might think the typecast is not needed with $max being `? int $max` after passing > 1,
463 74
            * phpstan@a4f89fa still thinks it's possibly null.
464
            * see https://github.com/phpstan/phpstan/issues/577 for related issue
465 74
            */
466 74
            $element->setMax((int) $max);
467
        }
468 74
        $elementContainer->addElement($element);
469
    }
470
471
    private function addGroupAsElement(
472
        Schema $schema,
473
        DOMElement $node,
474 74
        DOMElement $childNode,
475 74
        ElementContainer $elementContainer
476 74
    ): void {
477 74
        $referencedGroup = $this->findGroup(
478
            $schema,
479
            $node,
480 74
            $childNode->getAttribute('ref')
481 74
        );
482 74
483
        $group = $this->loadGroupRef($referencedGroup, $childNode);
484 74
        $elementContainer->addElement($group);
485
    }
486 74
487 74
    private function loadGroup(Schema $schema, DOMElement $node): Closure
488 74
    {
489
        $group = new Group($schema, $node->getAttribute('name'));
490 74
        $group->setDoc($this->getDocumentation($node));
491 1
        $groupOriginal = $group;
492
493 1
        if ($node->hasAttribute('maxOccurs') || $node->hasAttribute('maxOccurs')) {
494 1
            $group = new GroupRef($group);
495
496 1
            if ($node->hasAttribute('maxOccurs')) {
497 1
                self::maybeSetMax($group, $node);
498
            }
499
            if ($node->hasAttribute('minOccurs')) {
500
                self::maybeSetMin($group, $node);
501 74
            }
502
        }
503
504 74
        $schema->addGroup($group);
505 74
506
        return function () use ($groupOriginal, $node): void {
507 74
            static::againstDOMNodeList(
508 74
                $node,
509 74
                function (DOMelement $node, DOMElement $childNode) use ($groupOriginal): void {
510 74
                    switch ($childNode->localName) {
511 74
                        case 'sequence':
512 74
                        case 'choice':
513
                        case 'all':
514 74
                            $this->loadSequence($groupOriginal, $childNode);
515
                            break;
516 74
                    }
517
                }
518
            );
519 74
        };
520
    }
521
522
    private function loadComplexType(Schema $schema, DOMElement $node, Closure $callback = null): Closure
523
    {
524 74
        /**
525
         * @var bool
526 74
         */
527 74
        $isSimple = false;
528
529
        self::againstDOMNodeList(
530
            $node,
531
            function (
532 74
                DOMElement $node,
533
                DOMElement $childNode
534 74
            ) use (
535 1
                &$isSimple
536
            ): void {
537 74
                if ($isSimple) {
538 2
                    return;
539
                }
540 74
                if ($childNode->localName === 'simpleContent') {
541
                    $isSimple = true;
542
                }
543 74
            }
544
        );
545 74
546 74
        $type = $isSimple ? new ComplexTypeSimpleContent($schema, $node->getAttribute('name')) : new ComplexType($schema, $node->getAttribute('name'));
547 74
548
        $type->setDoc($this->getDocumentation($node));
549
        if ($node->getAttribute('name')) {
550
            $schema->addType($type);
551 74
        }
552
553 74
        return function () use ($type, $node, $schema, $callback): void {
554 74
            $this->fillTypeNode($type, $node, true);
555
556
            self::againstDOMNodeList(
557
                $node,
558
                function (
559 74
                    DOMElement $node,
560 74
                    DOMElement $childNode
561
                ) use (
562 74
                    $schema,
563 74
                    $type
564 74
                ): void {
565 74
                    $this->loadComplexTypeFromChildNode(
566 74
                        $type,
567
                        $node,
568 74
                        $childNode,
569
                        $schema
570
                    );
571 74
                }
572 74
            );
573
574 74
            if ($callback instanceof Closure) {
575
                call_user_func($callback, $type);
576
            }
577 74
        };
578
    }
579
580
    private function loadComplexTypeFromChildNode(
581
        BaseComplexType $type,
582
        DOMElement $node,
583 74
        DOMElement $childNode,
584 74
        Schema $schema
585 74
    ): void {
586 74
        switch ($childNode->localName) {
587 74
            case 'sequence':
588 74
            case 'choice':
589 74
            case 'all':
590 74
                if ($type instanceof ElementContainer) {
591
                    $this->loadSequence(
592
                        $type,
593 74
                        $childNode
594 74
                    );
595 74
                }
596 74
                break;
597 74
            case 'attribute':
598 74
                $this->addAttributeFromAttributeOrRef(
599 74
                    $type,
600
                    $childNode,
601 74
                    $schema,
602 74
                    $node
603 2
                );
604 2
                break;
605 2
            case 'attributeGroup':
606 2
                $this->findSomethingLikeAttributeGroup(
607 2
                    $schema,
608
                    $node,
609 2
                    $childNode,
610 74
                    $type
611
                );
612 1
                break;
613
            case 'group':
614 1
                if (
615 1
                    $type instanceof ComplexType
616 1
                ) {
617 1
                    $this->addGroupAsElement(
618 1
                        $schema,
619
                        $node,
620
                        $childNode,
621 1
                        $type
622
                    );
623 74
                }
624
                break;
625 74
        }
626
    }
627 74
628 74
    private function loadSimpleType(Schema $schema, DOMElement $node, Closure $callback = null): Closure
629 74
    {
630 74
        $type = new SimpleType($schema, $node->getAttribute('name'));
631
        $type->setDoc($this->getDocumentation($node));
632
        if ($node->getAttribute('name')) {
633
            $schema->addType($type);
634 74
        }
635
636 74
        return function () use ($type, $node, $callback): void {
637 74
            $this->fillTypeNode($type, $node, true);
638
639 74
            self::againstDOMNodeList(
640 74
                $node,
641 74
                function (DOMElement $node, DOMElement $childNode) use ($type): void {
642 74
                    switch ($childNode->localName) {
643 74
                        case 'union':
644 74
                            $this->loadUnion($type, $childNode);
645 74
                            break;
646
                        case 'list':
647 74
                            $this->loadList($type, $childNode);
648
                            break;
649
                    }
650 74
                }
651 74
            );
652
653 74
            if ($callback instanceof Closure) {
654
                call_user_func($callback, $type);
655
            }
656 74
        };
657
    }
658 74
659
    private function loadList(SimpleType $type, DOMElement $node): void
660
    {
661
        if ($node->hasAttribute('itemType')) {
662 74
            /**
663 74
             * @var SimpleType
664
             */
665 74
            $listType = $this->findSomeType($type, $node, 'itemType');
666 74
            $type->setList($listType);
667
        } else {
668
            self::againstDOMNodeList(
669
                $node,
670
                function (
671 74
                    DOMElement $node,
672
                    DOMElement $childNode
673 74
                ) use (
674 74
                    $type
675 74
                ): void {
676
                    $this->loadTypeWithCallback(
677 74
                        $type->getSchema(),
678 74
                        $childNode,
679
                        function (SimpleType $list) use ($type): void {
680 74
                            $type->setList($list);
681
                        }
682
                    );
683 74
                }
684
            );
685 74
        }
686
    }
687
688
    private function findSomeType(
689
        SchemaItem $fromThis,
690 74
        DOMElement $node,
691 74
        string $attributeName
692 74
    ): SchemaItem {
693 74
        return $this->findSomeTypeFromAttribute(
694
            $fromThis,
695
            $node,
696
            $node->getAttribute($attributeName)
697 74
        );
698
    }
699
700
    private function findSomeTypeFromAttribute(
701
        SchemaItem $fromThis,
702 74
        DOMElement $node,
703 74
        string $attributeName
704 74
    ): SchemaItem {
705 74
        $out = $this->findType(
706
            $fromThis->getSchema(),
707
            $node,
708 74
            $attributeName
709
        );
710
711 74
        return $out;
712
    }
713 74
714 74
    private function loadUnion(SimpleType $type, DOMElement $node): void
715 74
    {
716
        if ($node->hasAttribute('memberTypes')) {
717
            $types = preg_split('/\s+/', $node->getAttribute('memberTypes'));
718
            foreach ($types as $typeName) {
719 74
                /**
720 74
                 * @var SimpleType
721 74
                 */
722 74
                $unionType = $this->findSomeTypeFromAttribute(
723
                    $type,
724 74
                    $node,
725
                    $typeName
726
                );
727 74
                $type->addUnion($unionType);
728 74
            }
729
        }
730
        self::againstDOMNodeList(
731
            $node,
732
            function (
733 74
                DOMElement $node,
734
                DOMElement $childNode
735 74
            ) use (
736 74
                $type
737 74
            ): void {
738
                $this->loadTypeWithCallback(
739 74
                    $type->getSchema(),
740 74
                    $childNode,
741
                    function (SimpleType $unType) use ($type): void {
742 74
                        $type->addUnion($unType);
743
                    }
744 74
                );
745
            }
746 74
        );
747
    }
748 74
749 74
    private function fillTypeNode(Type $type, DOMElement $node, bool $checkAbstract = false): void
750
    {
751
        if ($checkAbstract) {
752 74
            $type->setAbstract($node->getAttribute('abstract') === 'true' || $node->getAttribute('abstract') === '1');
753 74
        }
754
755 74
        self::againstDOMNodeList(
756 74
            $node,
757 74
            function (DOMElement $node, DOMElement $childNode) use ($type): void {
758 74
                switch ($childNode->localName) {
759 74
                    case 'restriction':
760 74
                        $this->loadRestriction($type, $childNode);
761 74
                        break;
762
                    case 'extension':
763 74
                        if ($type instanceof BaseComplexType) {
764 74
                            $this->loadExtension($type, $childNode);
765 74
                        }
766 74
                        break;
767 74
                    case 'simpleContent':
768
                    case 'complexContent':
769 74
                        $this->fillTypeNode($type, $childNode);
770
                        break;
771 74
                }
772
            }
773 74
        );
774
    }
775 74
776 74
    private function loadExtension(BaseComplexType $type, DOMElement $node): void
777
    {
778 74
        $extension = new Extension();
779 74
        $type->setExtension($extension);
780 74
781 74
        if ($node->hasAttribute('base')) {
782 74
            $this->findAndSetSomeBase($type, $extension, $node);
783
        }
784
        $this->loadExtensionChildNodes($type, $node);
785 74
    }
786 74
787
    private function findAndSetSomeBase(
788 74
        Type $type,
789
        Base $setBaseOnThis,
790
        DOMElement $node
791
    ): void {
792
        /**
793
         * @var Type
794
         */
795
        $parent = $this->findSomeType($type, $node, 'base');
796 74
        $setBaseOnThis->setBase($parent);
797 74
    }
798 74
799
    private function loadExtensionChildNodes(
800 74
        BaseComplexType $type,
801
        DOMElement $node
802
    ): void {
803
        self::againstDOMNodeList(
804 74
            $node,
805 74
            function (
806
                DOMElement $node,
807
                DOMElement $childNode
808
            ) use (
809
                $type
810 74
            ): void {
811
                switch ($childNode->localName) {
812 74
                    case 'sequence':
813 74
                    case 'choice':
814 74
                    case 'all':
815 74
                        if ($type instanceof ElementContainer) {
816 74
                            $this->loadSequence($type, $childNode);
817 74
                        }
818 74
                        break;
819 74
                }
820
                $this->loadChildAttributesAndAttributeGroups($type, $node, $childNode);
821
            }
822 74
        );
823 74
    }
824 74
825 74
    private function loadChildAttributesAndAttributeGroups(
826 74
        BaseComplexType $type,
827 74
        DOMElement $node,
828 74
        DOMElement $childNode
829
    ): void {
830 74
        switch ($childNode->localName) {
831 74
            case 'attribute':
832 74
                $this->addAttributeFromAttributeOrRef(
833 74
                    $type,
834 74
                    $childNode,
835 74
                    $type->getSchema(),
836 74
                    $node
837
                );
838 74
                break;
839
            case 'attributeGroup':
840 74
                $this->findSomethingLikeAttributeGroup(
841
                    $type->getSchema(),
842 74
                    $node,
843
                    $childNode,
844 74
                    $type
845
                );
846 74
                break;
847 74
        }
848 74
    }
849 74
850
    private function loadRestriction(Type $type, DOMElement $node): void
851 74
    {
852 74
        $restriction = new Restriction();
853
        $type->setRestriction($restriction);
854
        if ($node->hasAttribute('base')) {
855
            $this->findAndSetSomeBase($type, $restriction, $node);
856
        } else {
857 74
            self::againstDOMNodeList(
858 74
                $node,
859
                function (
860 74
                    DOMElement $node,
861 74
                    DOMElement $childNode
862 74
                ) use (
863
                    $type,
864 74
                    $restriction
865 74
                ): void {
866
                    $this->loadTypeWithCallback(
867 74
                        $type->getSchema(),
868
                        $childNode,
869
                        function (Type $restType) use ($restriction): void {
870 74
                            $restriction->setBase($restType);
871 74
                        }
872
                    );
873
                }
874
            );
875
        }
876 74
        $this->loadRestrictionChildNodes($type, $restriction, $node);
877
    }
878
879 74
    private function loadRestrictionChildNodes(
880 74
        Type $type,
881
        Restriction $restriction,
882 74
        DOMElement $node
883
    ): void {
884
        self::againstDOMNodeList(
885
            $node,
886
            function (
887
                DOMElement $node,
888
                DOMElement $childNode
889
            ) use (
890
                $type,
891
                $restriction
892
            ): void {
893
                if ($type instanceof BaseComplexType) {
894
                    $this->loadChildAttributesAndAttributeGroups($type, $node, $childNode);
895 74
                }
896
                if (
897
                in_array(
898 74
                    $childNode->localName,
899 74
                    [
900
                        'enumeration',
901 74
                        'pattern',
902 74
                        'length',
903
                        'minLength',
904
                        'maxLength',
905
                        'minInclusive',
906 74
                        'maxInclusive',
907
                        'minExclusive',
908 74
                        'maxExclusive',
909
                        'fractionDigits',
910
                        'totalDigits',
911
                        'whiteSpace',
912
                    ],
913 74
                    true
914
                )
915 74
                ) {
916 74
                    $restriction->addCheck(
917 74
                        $childNode->localName,
918 74
                        [
919
                            'value' => $childNode->getAttribute('value'),
920
                            'doc' => $this->getDocumentation($childNode),
921
                        ]
922
                    );
923
                }
924 74
            }
925
        );
926
    }
927 74
928 74
    /**
929 74
     * @return mixed[]
930
     */
931
    private static function splitParts(DOMElement $node, string $typeName): array
932
    {
933 74
        $prefix = null;
934
        $name = $typeName;
935 74
        if (strpos($typeName, ':') !== false) {
936
            [$prefix, $name] = explode(':', $typeName);
937
        }
938
939
        /**
940 74
         * @psalm-suppress PossiblyNullArgument
941
         */
942
        $namespace = $node->lookupNamespaceUri($prefix);
943
944
        return [
945
            $name,
946 74
            $namespace,
947
            $prefix,
948 74
        ];
949
    }
950
951
    private function findAttributeItem(Schema $schema, DOMElement $node, string $typeName): AttributeItem
952
    {
953
        [$name, $namespace] = static::splitParts($node, $typeName);
954 74
955
        /**
956 74
         * @var string|null $namespace
957
         */
958
        $namespace = $namespace ?: $schema->getTargetNamespace();
959
960
        try {
961 74
            /**
962
             * @var AttributeItem $out
963
             */
964
            $out = $schema->findAttribute((string) $name, $namespace);
965
966
            return $out;
967 74
        } catch (TypeNotFoundException $e) {
968
            throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'attribute', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e);
969 74
        }
970
    }
971
972
    private function findAttributeGroup(Schema $schema, DOMElement $node, string $typeName): AttributeGroup
973
    {
974
        [$name, $namespace] = static::splitParts($node, $typeName);
975 74
976
        /**
977 74
         * @var string|null $namespace
978
         */
979
        $namespace = $namespace ?: $schema->getTargetNamespace();
980
981
        try {
982 74
            /**
983
             * @var AttributeGroup $out
984
             */
985 74
            $out = $schema->findAttributeGroup((string) $name, $namespace);
986
987
            return $out;
988
        } catch (TypeNotFoundException $e) {
989
            throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'attributegroup', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e);
990
        }
991 74
    }
992
993 74
    private function findElement(Schema $schema, DOMElement $node, string $typeName): ElementDef
994
    {
995
        [$name, $namespace] = static::splitParts($node, $typeName);
996
997
        /**
998 74
         * @var string|null $namespace
999
         */
1000
        $namespace = $namespace ?: $schema->getTargetNamespace();
1001
1002
        try {
1003
            return $schema->findElement((string) $name, $namespace);
1004 74
        } catch (TypeNotFoundException $e) {
1005
            throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'element', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e);
1006 74
        }
1007
    }
1008
1009
    private function findGroup(Schema $schema, DOMElement $node, string $typeName): Group
1010
    {
1011
        [$name, $namespace] = static::splitParts($node, $typeName);
1012 74
1013
        /**
1014 74
         * @var string|null $namespace
1015
         */
1016
        $namespace = $namespace ?: $schema->getTargetNamespace();
1017
1018
        try {
1019 74
            /**
1020
             * @var Group $out
1021
             */
1022
            $out = $schema->findGroup((string) $name, $namespace);
1023 74
1024 1
            return $out;
1025 1
        } catch (TypeNotFoundException $e) {
1026
            throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'group', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI), 0, $e);
1027 74
        }
1028
    }
1029 74
1030 74
    private function findType(Schema $schema, DOMElement $node, string $typeName): SchemaItem
1031 74
    {
1032 74
        [$name, $namespace] = static::splitParts($node, $typeName);
1033
1034
        /**
1035
         * @var string|null $namespace
1036
         */
1037
        $namespace = $namespace ?: $schema->getTargetNamespace();
1038
1039 74
        $tryFindType = static function (Schema $schema, string $name, ?string $namespace): ?SchemaItem {
1040
            try {
1041 74
                return $schema->findType($name, $namespace);
1042
            } catch (TypeNotFoundException $e) {
1043
                return null;
1044 74
            }
1045
        };
1046
1047
        $interestingSchemas = array_merge([$schema], $this->loadedSchemas[$namespace] ?? []);
1048
        foreach ($interestingSchemas as $interestingSchema) {
1049 74
            if ($result = $tryFindType($interestingSchema, $name, $namespace)) {
1050 74
                return $result;
1051 74
            }
1052
        }
1053
1054
        throw new TypeException(sprintf("Can't find %s named {%s}#%s, at line %d in %s ", 'type', $namespace, $name, $node->getLineNo(), $node->ownerDocument->documentURI));
1055
    }
1056 74
1057 74
    private function loadElementDef(Schema $schema, DOMElement $node): Closure
1058
    {
1059
        return $this->loadAttributeOrElementDef($schema, $node, false);
1060 74
    }
1061 74
1062 74
    private function fillItem(Item $element, DOMElement $node): void
1063
    {
1064 74
        /**
1065
         * @var bool
1066
         */
1067 74
        $skip = false;
1068
        self::againstDOMNodeList(
1069
            $node,
1070 74
            function (
1071 74
                DOMElement $node,
1072 74
                DOMElement $childNode
1073
            ) use (
1074 74
                $element,
1075 74
                &$skip
1076
            ): void {
1077 74
                if (
1078
                    !$skip &&
1079 74
                    in_array(
1080
                        $childNode->localName,
1081 74
                        [
1082 74
                            'complexType',
1083
                            'simpleType',
1084 74
                        ],
1085 74
                        true
1086
                    )
1087 74
                ) {
1088
                    $this->loadTypeWithCallback(
1089 74
                        $element->getSchema(),
1090
                        $childNode,
1091
                        function (Type $type) use ($element): void {
1092
                            $element->setType($type);
1093 74
                        }
1094
                    );
1095
                    $skip = true;
1096
                }
1097
            }
1098 74
        );
1099 74
        if ($skip) {
1100 74
            return;
1101 74
        }
1102
        $this->fillItemNonLocalType($element, $node);
1103
    }
1104
1105 74
    private function fillItemNonLocalType(Item $element, DOMElement $node): void
1106 74
    {
1107
        if ($node->getAttribute('type')) {
1108 74
            /**
1109
             * @var Type
1110
             */
1111
            $type = $this->findSomeType($element, $node, 'type');
1112 74
        } else {
1113 74
            /**
1114 74
             * @var Type
1115 1
             */
1116
            $type = $this->findSomeTypeFromAttribute(
1117
                $element,
1118
                $node,
1119 74
                ($node->lookupPrefix(self::XSD_NS).':anyType')
1120
            );
1121 3
        }
1122 3
1123 3
        $element->setType($type);
1124
    }
1125
1126 3
    private function loadImport(
1127 74
        Schema $schema,
1128 1
        DOMElement $node
1129 1
    ): Closure {
1130
        $namespace = $node->getAttribute('namespace');
1131
        $schemaLocation = $node->getAttribute('schemaLocation');
1132
        if (!$schemaLocation && isset($this->knownNamespaceSchemaLocations[$namespace])) {
1133 74
            $schemaLocation = $this->knownNamespaceSchemaLocations[$namespace];
1134 74
        }
1135
1136 74
        // postpone schema loading
1137 74
        if ($namespace && !$schemaLocation && !isset(self::$globalSchemaInfo[$namespace])) {
1138
            return function () use ($schema, $namespace): void {
1139
                if (!empty($this->loadedSchemas[$namespace])) {
1140 74
                    foreach ($this->loadedSchemas[$namespace] as $s) {
1141
                        $schema->addSchema($s, $namespace);
1142
                    }
1143 3
                }
1144
            };
1145
        } elseif ($namespace && !$schemaLocation && !empty($this->loadedSchemas[$namespace])) {
1146 3
            foreach ($this->loadedSchemas[$namespace] as $s) {
1147
                $schema->addSchema($s, $namespace);
1148
            }
1149
        }
1150 3
1151 2
        $base = urldecode($node->ownerDocument->documentURI);
0 ignored issues
show
Bug introduced by
It seems like $node->ownerDocument->documentURI can also be of type null; however, parameter $string of urldecode() does only seem to accept string, 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

1151
        $base = urldecode(/** @scrutinizer ignore-type */ $node->ownerDocument->documentURI);
Loading history...
1152 2
        $file = UrlUtils::resolveRelativeUrl($base, $schemaLocation);
1153 2
1154
        if (isset($this->loadedFiles[$file])) {
1155 1
            $schema->addSchema($this->loadedFiles[$file]);
1156
1157
            return function (): void {
1158 3
            };
1159
        }
1160
1161 3
        return $this->loadImportFresh($namespace, $schema, $file);
1162
    }
1163
1164
    private function createOrUseSchemaForNs(
1165
        Schema $schema,
1166
        string $namespace
1167 3
    ): Schema {
1168 3
        if (('' !== trim($namespace))) {
1169 3
            $newSchema = new Schema();
1170
            $newSchema->addSchema($this->getGlobalSchema());
1171
            $schema->addSchema($newSchema);
1172 3
        } else {
1173
            $newSchema = $schema;
1174 3
        }
1175
1176 3
        return $newSchema;
1177
    }
1178 3
1179 3
    private function loadImportFresh(
1180
        string $namespace,
1181 3
        Schema $schema,
1182
        string $file
1183
    ): Closure {
1184
        return function () use ($namespace, $schema, $file): void {
1185
            $dom = $this->getDOM(
1186
                $this->knownLocationSchemas[$file]
1187
                    ?? $file
1188
            );
1189 74
1190
            $schemaNew = $this->createOrUseSchemaForNs($schema, $namespace);
1191 74
1192 74
            $this->setLoadedFile($file, $schemaNew);
1193 74
1194
            $callbacks = $this->schemaNode($schemaNew, $dom->documentElement, $schema);
1195
1196
            foreach ($callbacks as $callback) {
1197 74
                $callback();
1198 74
            }
1199 74
        };
1200 74
    }
1201
1202 74
    /**
1203 74
     * @var Schema|null
1204 74
     */
1205
    protected $globalSchema;
1206 74
1207 74
    public function getGlobalSchema(): Schema
1208
    {
1209
        if (!($this->globalSchema instanceof Schema)) {
1210 74
            $callbacks = [];
1211 74
            $globalSchemas = [];
1212
            /**
1213 74
             * @var string $namespace
1214 74
             */
1215 74
            foreach (self::$globalSchemaInfo as $namespace => $uri) {
1216
                $this->setLoadedFile(
1217 74
                    $uri,
1218 74
                    $globalSchemas[$namespace] = $schema = new Schema()
1219 74
                );
1220
                $this->setLoadedSchema($namespace, $schema);
1221
                if ($namespace === self::XSD_NS) {
1222
                    $this->globalSchema = $schema;
1223
                }
1224
                $xml = $this->getDOM($this->knownLocationSchemas[$uri]);
1225 74
                $callbacks = array_merge($callbacks, $this->schemaNode($schema, $xml->documentElement));
1226 74
            }
1227
1228
            $globalSchemas[(string) static::XSD_NS]->addType(new SimpleType($globalSchemas[(string) static::XSD_NS], 'anySimpleType'));
1229
            $globalSchemas[(string) static::XSD_NS]->addType(new SimpleType($globalSchemas[(string) static::XSD_NS], 'anyType'));
1230 74
1231
            $globalSchemas[(string) static::XML_NS]->addSchema(
1232
                $globalSchemas[(string) static::XSD_NS],
1233
                (string) static::XSD_NS
1234 74
            );
1235
            $globalSchemas[(string) static::XSD_NS]->addSchema(
1236
                $globalSchemas[(string) static::XML_NS],
1237
                (string) static::XML_NS
1238
            );
1239
1240 2
            /**
1241
             * @var Closure $callback
1242 2
             */
1243 2
            foreach ($callbacks as $callback) {
1244
                $callback();
1245 2
            }
1246 2
        }
1247
1248
        if (!($this->globalSchema instanceof Schema)) {
1249 2
            throw new TypeException('Global schema not discovered');
1250 2
        }
1251 2
1252 2
        return $this->globalSchema;
1253 2
    }
1254
1255 2
    /**
1256
     * @param DOMNode[] $nodes
1257 2
     */
1258
    public function readNodes(array $nodes, string $file = null): Schema
1259 2
    {
1260 2
        $rootSchema = new Schema();
1261
        $rootSchema->addSchema($this->getGlobalSchema());
1262
1263
        if ($file !== null) {
1264 2
            $this->setLoadedFile($file, $rootSchema);
1265 2
        }
1266
1267
        $all = [];
1268 2
        foreach ($nodes as $k => $node) {
1269
            if (($node instanceof \DOMElement) && $node->namespaceURI === self::XSD_NS && $node->localName == 'schema') {
1270
                $holderSchema = new Schema();
1271 72
                $holderSchema->addSchema($this->getGlobalSchema());
1272
1273 72
                $this->setLoadedSchemaFromElement($node, $holderSchema);
1274 72
1275
                $rootSchema->addSchema($holderSchema);
1276 72
1277 71
                $callbacks = $this->schemaNode($holderSchema, $node);
1278
                $all = array_merge($callbacks, $all);
1279
            }
1280 72
        }
1281
1282 72
        foreach ($all as $callback) {
1283
            call_user_func($callback);
1284 72
        }
1285 66
1286
        return $rootSchema;
1287
    }
1288 72
1289
    public function readNode(DOMElement $node, string $file = null): Schema
1290
    {
1291
        $rootSchema = new Schema();
1292
        $rootSchema->addSchema($this->getGlobalSchema());
1293
1294 68
        if ($file !== null) {
1295
            $this->setLoadedFile($file, $rootSchema);
1296 68
        }
1297 68
1298 68
        $this->setLoadedSchemaFromElement($node, $rootSchema);
1299 1
1300
        $callbacks = $this->schemaNode($rootSchema, $node);
1301 67
1302 67
        foreach ($callbacks as $callback) {
1303
            call_user_func($callback);
1304 67
        }
1305
1306
        return $rootSchema;
1307
    }
1308
1309
    /**
1310 5
     * @throws IOException
1311
     */
1312 5
    public function readString(string $content, string $file = 'schema.xsd'): Schema
1313
    {
1314 4
        $xml = new DOMDocument('1.0', 'UTF-8');
1315
        libxml_use_internal_errors(true);
1316
        if (!@$xml->loadXML($content)) {
1317
            throw new IOException("Can't load the schema", 0, $this->extractErrorMessage());
1318
        }
1319
        libxml_use_internal_errors(false);
1320 75
        $xml->documentURI = $file;
1321
1322 75
        return $this->readNode($xml->documentElement, $file);
1323 75
    }
1324 75
1325 1
    /**
1326 1
     * @throws IOException
1327
     */
1328 74
    public function readFile(string $file): Schema
1329
    {
1330 74
        $xml = $this->getDOM($file);
1331
1332
        return $this->readNode($xml->documentElement, $file);
1333 74
    }
1334
1335
    /**
1336
     * @throws IOException
1337 74
     */
1338 74
    private function getDOM(string $file): DOMDocument
1339
    {
1340
        $xml = new DOMDocument('1.0', 'UTF-8');
1341
        libxml_use_internal_errors(true);
1342 74
        if (!@$xml->load($file)) {
1343
            libxml_use_internal_errors(false);
1344 74
            throw new IOException("Can't load the file '$file'", 0, $this->extractErrorMessage());
1345 74
        }
1346 74
        libxml_use_internal_errors(false);
1347 74
1348
        return $xml;
1349
    }
1350
1351 74
    private static function againstDOMNodeList(
1352
        DOMElement $node,
1353 74
        Closure $againstNodeList
1354
    ): void {
1355
        $limit = $node->childNodes->length;
1356
        for ($i = 0; $i < $limit; ++$i) {
1357
            /**
1358
             * @var DOMNode
1359
             */
1360
            $childNode = $node->childNodes->item($i);
1361 74
1362
            if ($childNode instanceof DOMElement) {
1363 74
                $againstNodeList(
1364 74
                    $node,
1365 74
                    $childNode
1366 74
                );
1367 74
            }
1368 74
        }
1369 74
    }
1370
1371
    private function loadTypeWithCallback(
1372 74
        Schema $schema,
1373 74
        DOMElement $childNode,
1374
        Closure $callback
1375 74
    ): void {
1376
        /**
1377 74
         * @var Closure|null $func
1378
         */
1379
        $func = null;
1380
1381 74
        switch ($childNode->localName) {
1382 74
            case 'complexType':
1383
                $func = $this->loadComplexType($schema, $childNode, $callback);
1384 74
                break;
1385
            case 'simpleType':
1386 74
                $func = $this->loadSimpleType($schema, $childNode, $callback);
1387 74
                break;
1388 74
        }
1389
1390 74
        if ($func instanceof Closure) {
1391 74
            call_user_func($func);
1392
        }
1393 74
    }
1394 74
1395
    private function loadElement(Schema $schema, DOMElement $node): Element
1396
    {
1397 74
        $element = new Element($schema, $node->getAttribute('name'));
1398 4
        $element->setDoc($this->getDocumentation($node));
1399
        $this->fillItem($element, $node);
1400 74
        $this->fillElement($element, $node);
1401 5
1402
        return $element;
1403
    }
1404 74
1405
    private function fillElement(AbstractElementSingle $element, DOMElement $node): void
1406 74
    {
1407 74
        self::maybeSetMax($element, $node);
1408
        self::maybeSetMin($element, $node);
1409
        self::maybeSetFixed($element, $node);
1410 74
        self::maybeSetDefault($element, $node);
1411
1412
        $xp = new \DOMXPath($node->ownerDocument);
0 ignored issues
show
Bug introduced by
It seems like $node->ownerDocument can also be of type null; however, parameter $document of DOMXPath::__construct() does only seem to accept DOMDocument, 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

1412
        $xp = new \DOMXPath(/** @scrutinizer ignore-type */ $node->ownerDocument);
Loading history...
1413 74
        $xp->registerNamespace('xs', self::XSD_NS);
1414
1415
        if ($xp->query('ancestor::xs:choice', $node)->length) {
1416
            $element->setMin(0);
1417
        }
1418
1419 74
        if ($node->hasAttribute('nillable')) {
1420 74
            $element->setNil($node->getAttribute('nillable') == 'true');
1421 74
        }
1422 74
        if ($node->hasAttribute('form')) {
1423
            $element->setQualified($node->getAttribute('form') == 'qualified');
1424
        }
1425 74
1426 74
        $parentNode = $node->parentNode;
1427
        if ($parentNode->localName != 'schema' || $parentNode->namespaceURI != self::XSD_NS) {
1428 74
            $element->setLocal(true);
1429
        }
1430
    }
1431
1432
    private function addAttributeFromAttributeOrRef(
1433
        BaseComplexType $type,
1434 74
        DOMElement $childNode,
1435 74
        Schema $schema,
1436 74
        DOMElement $node
1437
    ): void {
1438 74
        $attribute = $this->getAttributeFromAttributeOrRef(
1439
            $childNode,
1440 74
            $schema,
1441 74
            $node
1442
        );
1443 74
1444
        $type->addAttribute($attribute);
1445 74
    }
1446 74
1447
    private function findSomethingLikeAttributeGroup(
1448 74
        Schema $schema,
1449
        DOMElement $node,
1450 74
        DOMElement $childNode,
1451
        AttributeContainer $addToThis
1452 74
    ): void {
1453 74
        $attribute = $this->findAttributeGroup($schema, $node, $childNode->getAttribute('ref'));
1454
        $addToThis->addAttribute($attribute);
1455 74
    }
1456 74
1457
    private function setLoadedFile(string $key, Schema $schema): void
1458 74
    {
1459
        $this->loadedFiles[$key] = $schema;
1460 74
    }
1461
1462
    private function setLoadedSchemaFromElement(DOMElement $node, Schema $schema): void
1463
    {
1464
        if ($node->hasAttribute('targetNamespace')) {
1465 74
            $this->setLoadedSchema($node->getAttribute('targetNamespace'), $schema);
1466
        }
1467 74
    }
1468 74
1469
    private function setLoadedSchema(string $namespace, Schema $schema): void
1470
    {
1471
        if (!isset($this->loadedSchemas[$namespace])) {
1472 74
            $this->loadedSchemas[$namespace] = [];
1473 74
        }
1474 74
        if (!in_array($schema, $this->loadedSchemas[$namespace], true)) {
1475 74
            $this->loadedSchemas[$namespace][] = $schema;
1476
        }
1477
    }
1478
1479
    private function setSchemaThingsFromNode(
1480
        Schema $schema,
1481
        DOMElement $node,
1482
        Schema $parent = null
1483
    ): void {
1484
        $schema->setDoc($this->getDocumentation($node));
1485
1486
        if ($node->hasAttribute('targetNamespace')) {
1487
            $schema->setTargetNamespace($node->getAttribute('targetNamespace'));
1488
        } elseif ($parent instanceof Schema) {
1489
            $schema->setTargetNamespace($parent->getTargetNamespace());
1490
        }
1491
        $schema->setElementsQualification($node->getAttribute('elementFormDefault') == 'qualified');
1492
        $schema->setAttributesQualification($node->getAttribute('attributeFormDefault') == 'qualified');
1493
        $schema->setDoc($this->getDocumentation($node));
1494
    }
1495
}
1496