Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

SimpleRestriction::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 55
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

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