Passed
Pull Request — master (#70)
by Axel
16:50 queued 06:52
created

SchemaReader::loadAttributeOrElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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

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

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