Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

TopLevelComplexType::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 38
nc 1
nop 1
dl 0
loc 52
rs 9.312
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, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, TooManyElementsException};
11
use SimpleSAML\XMLSchema\Type\{BooleanValue, IDValue, NCNameValue};
12
use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue;
13
use SimpleSAML\XMLSchema\XML\Interface\RedefinableInterface;
14
15
use function array_merge;
16
use function array_pop;
17
18
/**
19
 * Class representing the xs:complexType element.
20
 *
21
 * @package simplesamlphp/xml-common
22
 */
23
final class TopLevelComplexType extends AbstractTopLevelComplexType implements
24
    RedefinableInterface,
25
    SchemaValidatableElementInterface
26
{
27
    use SchemaValidatableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSchema\XML\TopLevelComplexType: $message, $line
Loading history...
28
29
    /** @var string */
30
    public const LOCALNAME = 'complexType';
31
32
33
    /**
34
     * Create an instance of this object from its XML representation.
35
     *
36
     * @param \DOMElement $xml
37
     * @return static
38
     *
39
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
40
     *   if the qualified name of the supplied element is wrong
41
     */
42
    public static function fromXML(DOMElement $xml): static
43
    {
44
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
45
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
46
47
        $annotation = Annotation::getChildrenOfClass($xml);
48
        Assert::maxCount($annotation, 1, TooManyElementsException::class);
49
50
        $simpleContent = SimpleContent::getChildrenOfClass($xml);
51
        Assert::maxCount($simpleContent, 1, TooManyElementsException::class);
52
53
        $complexContent = ComplexContent::getChildrenOfClass($xml);
54
        Assert::maxCount($complexContent, 1, TooManyElementsException::class);
55
56
        $content = array_merge($simpleContent, $complexContent);
57
        Assert::maxCount($content, 1, TooManyElementsException::class);
58
59
        $referencedGroup = ReferencedGroup::getChildrenOfClass($xml);
60
        Assert::maxCount($referencedGroup, 1, TooManyElementsException::class);
61
62
        $all = All::getChildrenOfClass($xml);
63
        Assert::maxCount($all, 1, TooManyElementsException::class);
64
65
        $choice = Choice::getChildrenOfClass($xml);
66
        Assert::maxCount($choice, 1, TooManyElementsException::class);
67
68
        $sequence = Sequence::getChildrenOfClass($xml);
69
        Assert::maxCount($sequence, 1, TooManyElementsException::class);
70
71
        $particles = array_merge($referencedGroup, $all, $choice, $sequence);
72
        Assert::maxCount($particles, 1, TooManyElementsException::class);
73
74
        $localAttribute = LocalAttribute::getChildrenOfClass($xml);
75
        $referencedAttributeGroup = ReferencedAttributeGroup::getChildrenOfClass($xml);
76
        $attributes = array_merge($localAttribute, $referencedAttributeGroup);
77
78
        $anyAttribute = AnyAttribute::getChildrenOfClass($xml);
79
        Assert::maxCount($anyAttribute, 1, TooManyElementsException::class);
80
81
        return new static(
82
            self::getAttribute($xml, 'name', NCNameValue::class),
83
            self::getOptionalAttribute($xml, 'mixed', BooleanValue::class, null),
84
            self::getOptionalAttribute($xml, 'abstract', BooleanValue::class, null),
85
            self::getOptionalAttribute($xml, 'final', DerivationSetValue::class, null),
86
            self::getOptionalAttribute($xml, 'block', DerivationSetValue::class, null),
87
            array_pop($content),
88
            array_pop($particles),
89
            $attributes,
90
            array_pop($anyAttribute),
91
            array_pop($annotation),
92
            self::getOptionalAttribute($xml, 'id', IDValue::class, null),
93
            self::getAttributesNSFromXML($xml),
94
        );
95
    }
96
}
97