AbstractSerElementsType::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\sp_200702;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function sprintf;
16
17
/**
18
 * Class representing WS security policy SetElementsType.
19
 *
20
 * @package simplesamlphp/ws-security
21
 */
22
abstract class AbstractSerElementsType extends AbstractSpElement
23
{
24
    use ExtendableAttributesTrait;
25
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractSerElementsType: $namespaceURI, $localName, $childNodes
Loading history...
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29
30
    /** The namespace-attribute for the xs:anyAttribute element */
31
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
32
33
    /** The exclusions for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_EXCLUSIONS = [
35
        ['http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702', 'XPathVersion'],
36
    ];
37
38
39
    /**
40
     * AbstractSerElementsType constructor.
41
     *
42
     * @param list<\SimpleSAML\WSSecurity\XML\sp_200702\XPath> $xpath
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\sp_200702\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     * @param string|null $xpathVersion
44
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $elts
45
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
46
     */
47
    final public function __construct(
48
        protected array $xpath,
49
        protected ?string $xpathVersion = null,
50
        array $elts = [],
51
        array $namespacedAttributes = [],
52
    ) {
53
        Assert::minCount($xpath, 1, SchemaViolationException::class);
0 ignored issues
show
Bug introduced by
The method minCount() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        Assert::/** @scrutinizer ignore-call */ 
54
                minCount($xpath, 1, SchemaViolationException::class);
Loading history...
54
        Assert::nullOrValidURI($xpathVersion);
0 ignored issues
show
Bug introduced by
The method nullOrValidURI() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        Assert::/** @scrutinizer ignore-call */ 
55
                nullOrValidURI($xpathVersion);
Loading history...
55
56
        $this->setElements($elts);
57
        $this->setAttributesNS($namespacedAttributes);
58
    }
59
60
61
    /**
62
     * Collect the value of the XPath property.
63
     *
64
     * @return list<\SimpleSAML\WSSecurity\XML\sp_200702\XPath>
65
     */
66
    public function getXPath(): array
67
    {
68
        return $this->xpath;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xpath returns the type array which is incompatible with the documented return type SimpleSAML\WSSecurity\XML\sp_200702\list.
Loading history...
69
    }
70
71
72
    /**
73
     * Collect the value of the XPathVersion property.
74
     *
75
     * @return string|null
76
     */
77
    public function getXPathVersion(): ?string
78
    {
79
        return $this->xpathVersion;
80
    }
81
82
83
    /**
84
     * Initialize an SerElementsType.
85
     *
86
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
87
     *
88
     * @param \DOMElement $xml The XML element we should load.
89
     * @return static
90
     *
91
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
92
     *   if the qualified name of the supplied element is wrong
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        $qualifiedName = static::getClassName(static::class);
97
        Assert::eq(
0 ignored issues
show
Bug introduced by
The method eq() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        Assert::/** @scrutinizer ignore-call */ 
98
                eq(
Loading history...
98
            $xml->localName,
99
            $qualifiedName,
100
            sprintf('Unexpected name for SerElementsType: %s. Expected: %s.', $xml->localName, $qualifiedName),
101
            InvalidDOMElementException::class,
102
        );
103
104
        return new static(
105
            XPath::getChildrenOfClass($xml),
106
            $xml->hasAttributeNS(self::NS, 'XPathVersion') ? $xml->getAttributeNS(self::NS, 'XPathVersion') : null,
107
            self::getChildElementsFromXML($xml),
108
            self::getAttributesNSFromXML($xml),
109
        );
110
    }
111
112
113
    /**
114
     * Convert this element to XML.
115
     *
116
     * @param \DOMElement|null $parent The element we should append this element to.
117
     * @return \DOMElement
118
     */
119
    public function toXML(?DOMElement $parent = null): DOMElement
120
    {
121
        $e = $this->instantiateParentElement($parent);
122
123
        if ($this->getXPathVersion() !== null) {
124
            $e->setAttributeNS(self::NS, 'sp:XPathVersion', $this->getXPathVersion());
125
        }
126
127
        foreach ($this->getXPath() as $xpath) {
128
            $xpath->toXML($e);
129
        }
130
131
        foreach ($this->getElements() as $elt) {
132
            /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
133
            $elt->toXML($e);
134
        }
135
136
        foreach ($this->getAttributesNS() as $attr) {
137
            $attr->toXML($e);
138
        }
139
140
        return $e;
141
    }
142
}
143