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

SimpleRestriction::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nc 1
nop 1
dl 0
loc 55
rs 9.2
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\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
11
use SimpleSAML\XMLSchema\Type\IDValue;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
13
14
use function array_merge;
15
use function array_pop;
16
17
/**
18
 * Class representing the simple version of the xs:restriction.
19
 *
20
 * @package simplesamlphp/xml-common
21
 */
22
final class SimpleRestriction extends AbstractSimpleRestrictionType
23
{
24
    public const string LOCALNAME = 'restriction';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 24 at column 24
Loading history...
25
26
27
    /**
28
     * Create an instance of this object from its XML representation.
29
     *
30
     * @param \DOMElement $xml
31
     * @return static
32
     *
33
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
34
     *   if the qualified name of the supplied element is wrong
35
     */
36
    public static function fromXML(DOMElement $xml): static
37
    {
38
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
39
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
40
41
        $annotation = Annotation::getChildrenOfClass($xml);
42
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
43
44
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
45
        $attributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
46
        $attributes = array_merge($localAttribute, $attributeGroup);
47
48
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
49
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
50
51
        $localSimpleType = LocalSimpleType::getChildrenOfClass($xml);
52
        Assert::maxCount($localSimpleType, 1, TooManyElementsException::class);
53
54
        $minExclusive = MinExclusive::getChildrenOfClass($xml);
55
        $minInclusive = MinInclusive::getChildrenOfClass($xml);
56
        $maxExclusive = MaxExclusive::getChildrenOfClass($xml);
57
        $maxInclusive = MaxInclusive::getChildrenOfClass($xml);
58
        $totalDigits = TotalDigits::getChildrenOfClass($xml);
59
        $fractionDigits = FractionDigits::getChildrenOfClass($xml);
60
        $length = Length::getChildrenOfClass($xml);
61
        $minLength = MinLength::getChildrenOfClass($xml);
62
        $maxLength = MaxLength::getChildrenOfClass($xml);
63
        $enumeration = Enumeration::getChildrenOfClass($xml);
64
        $whiteSpace = WhiteSpace::getChildrenOfClass($xml);
65
        $pattern = Pattern::getChildrenOfClass($xml);
66
67
        $facets = array_merge(
68
            $maxExclusive,
69
            $maxInclusive,
70
            $minExclusive,
71
            $minInclusive,
72
            $totalDigits,
73
            $fractionDigits,
74
            $length,
75
            $maxLength,
76
            $minLength,
77
            $enumeration,
78
            $whiteSpace,
79
            $pattern,
80
        );
81
82
        return new static(
83
            self::getAttribute($xml, 'base', QNameValue::class),
84
            array_pop($localSimpleType),
85
            $facets,
86
            $attributes,
87
            array_pop($anyAttribute),
88
            array_pop($annotation),
89
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
90
            self::getAttributesNSFromXML($xml),
91
        );
92
    }
93
}
94