Passed
Pull Request — master (#317)
by Tim
12:30
created

ExtensionsTrait::getList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\ElementInterface;
12
13
use function in_array;
14
15
/**
16
 * Trait grouping common functionality for elements implementing ExtensionType.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
trait ExtensionsTrait
21
{
22
    /** @var \SimpleSAML\XML\ElementInterface[] */
23
    protected array $extensions = [];
24
25
26
    /**
27
     * Extensions constructor.
28
     *
29
     * @param \SimpleSAML\XML\ElementInterface[] $extensions
30
     */
31
    public function __construct(array $extensions)
32
    {
33
        Assert::allIsInstanceOf($extensions, ElementInterface::class);
34
35
        foreach ($extensions as $extension) {
36
            $namespace = $extension->getNamespaceURI();
0 ignored issues
show
Bug introduced by
The method getNamespaceURI() does not exist on SimpleSAML\XML\ElementInterface. It seems like you code against a sub-type of said class. However, the method does not exist in SimpleSAML\XMLSecurity\X...lizableElementInterface or SimpleSAML\XMLSecurity\XML\SignedElementInterface or SimpleSAML\XMLSecurity\X...ignableElementInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

36
            /** @scrutinizer ignore-call */ 
37
            $namespace = $extension->getNamespaceURI();
Loading history...
37
38
            Assert::notNull(
39
                $namespace,
40
                'Extensions MUST NOT include global (non-namespace-qualified) elements.',
41
                ProtocolViolationException::class,
42
            );
43
            Assert::true(
44
                !in_array($namespace, [C::NS_SAML, C::NS_SAMLP], true),
45
                'Extensions MUST NOT include any SAML-defined namespace elements.',
46
                ProtocolViolationException::class,
47
            );
48
        }
49
50
        /**
51
         * Set an array with all extensions present.
52
         */
53
        $this->extensions = $extensions;
54
    }
55
56
57
    /**
58
     * Get an array with all extensions present.
59
     *
60
     * @return \SimpleSAML\XML\ElementInterface[]
61
     */
62
    public function getList(): array
63
    {
64
        return $this->extensions;
65
    }
66
67
68
    /**
69
     * @return bool
70
     */
71
    public function isEmptyElement(): bool
72
    {
73
        if (empty($this->extensions)) {
74
            return true;
75
        }
76
77
        foreach ($this->extensions as $extension) {
78
            if ($extension->isEmptyElement() === false) {
79
                return false;
80
            }
81
        }
82
83
        return true;
84
    }
85
86
87
    /**
88
     * Convert this object into its md:Extensions XML representation.
89
     *
90
     * @param \DOMElement|null $parent The element we should add this Extensions element to.
91
     * @return \DOMElement The new md:Extensions XML element.
92
     */
93
    public function toXML(DOMElement $parent = null): DOMElement
94
    {
95
        $e = $this->instantiateParentElement($parent);
96
        foreach ($this->extensions as $extension) {
97
            if (!$extension->isEmptyElement()) {
98
                $extension->toXML($e);
99
            }
100
        }
101
        return $e;
102
    }
103
104
105
    /**
106
     * @param \DOMElement|null $parent
107
     * @return \DOMElement
108
     */
109
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
110
}
111