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

LocalComplexType::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

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