Passed
Branch feature/php8.3 (4d3b0a)
by Tim
17:15
created

TopLevelElement::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nc 1
nop 1
dl 0
loc 53
rs 9.328
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSchema\Type\BooleanValue;
15
use SimpleSAML\XMLSchema\Type\IDValue;
16
use SimpleSAML\XMLSchema\Type\NCNameValue;
17
use SimpleSAML\XMLSchema\Type\QNameValue;
18
use SimpleSAML\XMLSchema\Type\Schema\BlockSetValue;
19
use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue;
20
use SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue;
21
use SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue;
22
use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue;
23
use SimpleSAML\XMLSchema\Type\StringValue;
24
25
/**
26
 * Class representing the topLevelElement-type.
27
 *
28
 * @package simplesamlphp/xml-common
29
 */
30
final class TopLevelElement extends AbstractTopLevelElement implements SchemaValidatableElementInterface
31
{
32
    use SchemaValidatableElementTrait;
33
34
35
    public const string LOCALNAME = 'element';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 35 at column 24
Loading history...
36
37
38
    /**
39
     * Create an instance of this object from its XML representation.
40
     *
41
     * @param \DOMElement $xml
42
     * @return static
43
     *
44
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
45
     *   if the qualified name of the supplied element is wrong
46
     */
47
    public static function fromXML(DOMElement $xml): static
48
    {
49
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
50
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
51
52
        // Prohibited attributes
53
        $ref = self::getOptionalAttribute($xml, 'ref', QNameValue::class, null);
54
        Assert::null($ref, SchemaViolationException::class);
55
56
        $form = self::getOptionalAttribute($xml, 'form', FormChoiceValue::class, null);
57
        Assert::null($form, SchemaViolationException::class);
58
59
        $minCount = self::getOptionalAttribute($xml, 'minCount', MinOccursValue::class, null);
60
        Assert::null($minCount, SchemaViolationException::class);
61
62
        $maxCount = self::getOptionalAttribute($xml, 'maxCount', MaxOccursValue::class, null);
63
        Assert::null($maxCount, SchemaViolationException::class);
64
65
        // The annotation
66
        $annotation = Annotation::getChildrenOfClass($xml);
67
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
68
69
        // The local type
70
        $localSimpleType = LocalSimpleType::getChildrenOfClass($xml);
71
        Assert::maxCount($localSimpleType, 1, TooManyElementsException::class);
72
73
        $localComplexType = LocalComplexType::getChildrenOfClass($xml);
74
        Assert::maxCount($localComplexType, 1, TooManyElementsException::class);
75
76
        $localType = array_merge($localSimpleType, $localComplexType);
77
        Assert::maxCount($localType, 1, TooManyElementsException::class);
78
79
        // The identity constraint
80
        $key = Key::getChildrenOfClass($xml);
81
        $keyref = Keyref::getChildrenOfClass($xml);
82
        $unique = Unique::getChildrenOfClass($xml);
83
        $identityConstraint = array_merge($key, $keyref, $unique);
84
85
        return new static(
86
            self::getAttribute($xml, 'name', NCNameValue::class),
87
            array_pop($localType),
88
            $identityConstraint,
89
            self::getOptionalAttribute($xml, 'type', QNameValue::class, null),
90
            self::getOptionalAttribute($xml, 'substitutionGroup', QNameValue::class, null),
91
            self::getOptionalAttribute($xml, 'default', StringValue::class, null),
92
            self::getOptionalAttribute($xml, 'fixed', StringValue::class, null),
93
            self::getOptionalAttribute($xml, 'nillable', BooleanValue::class, null),
94
            self::getOptionalAttribute($xml, 'abstract', BooleanValue::class, null),
95
            self::getOptionalAttribute($xml, 'final', DerivationSetValue::class, null),
96
            self::getOptionalAttribute($xml, 'block', BlockSetValue::class, null),
97
            array_pop($annotation),
98
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
99
            self::getAttributesNSFromXML($xml),
100
        );
101
    }
102
}
103