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