Passed
Push — master ( d67029...9c04a6 )
by Tim
01:46
created

QNameStringElementTrait::toXML()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
13
use function preg_split;
14
15
/**
16
 * Trait grouping common functionality for simple elements with QName textContent
17
 *
18
 * @package simplesamlphp/xml-soap
19
 */
20
trait QNameStringElementTrait
21
{
22
    /** @var string */
23
    protected string $content;
24
25
    /** @var string */
26
    protected string $namespaceUri;
27
28
29
    /**
30
     * Set the content of the element.
31
     *
32
     * @param string $content  The value to go in the XML textContent
33
     */
34
    protected function setContent(string $content): void
35
    {
36
        Assert::validQName($content, SchemaViolationException::class);
37
        $this->content = $content;
38
    }
39
40
41
    /**
42
     * Get the content of the element.
43
     *
44
     * @return string
45
     */
46
    public function getContent(): string
47
    {
48
        return $this->content;
49
    }
50
51
52
    /**
53
     * Set the namespaceUri.
54
     *
55
     * @param string $namespaceUri
56
     */
57
    protected function setContentNamespaceUri(string $namespaceUri): void
58
    {
59
        Assert::validURI($namespaceUri, SchemaViolationException::class);
60
        $this->namespaceUri = $namespaceUri;
61
    }
62
63
64
    /**
65
     * Get the namespace URI.
66
     *
67
     * @return string
68
     */
69
    public function getContentNamespaceUri(): string
70
    {
71
        return $this->namespaceUri;
72
    }
73
74
75
    /**
76
     * Splits a QName into an array holding the prefix (or null if no prefix is available) and the localName
77
     *
78
     * @param string $qName  The qualified name
79
     * @return string[]
80
     */
81
    private function parseQName(string $qName): array
82
    {
83
        Assert::validQName($content);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $content seems to be never defined.
Loading history...
84
85
        @list($prefix, $localName) = preg_split('/:/', $qName, 2);
86
        if ($localName === null) {
87
            $prefix = null;
88
            $localName = $qName;
89
        }
90
91
        Assert::nullOrValidNCName($prefix);
92
        Assert::validNCName($localName);
93
94
        return [$prefix, $localName];
95
    }
96
97
98
    /**
99
     * Convert XML into a class instance
100
     *
101
     * @param \DOMElement $xml The XML element we should load
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
105
     *   If the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\XML\QNameStringElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
111
112
        list($prefix, $localName) = $this->parseQName($this->textContent);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
113
        if ($localName === null) {
0 ignored issues
show
introduced by
The condition $localName === null is always false.
Loading history...
114
            // We don't have a prefixed value here; use target namespace
115
            $namespace = $xml->lookupNamespaceUri(null);
116
        } else {
117
            $namespace = $xml->lookupNamespaceUri($prefix);
118
        }
119
120
        return new static($xml->textContent, $namespace);
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\XML\QNameStri...entTrait::__construct() has too many arguments starting with $xml->textContent. ( Ignorable by Annotation )

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

120
        return /** @scrutinizer ignore-call */ new static($xml->textContent, $namespace);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
121
    }
122
123
124
    /**
125
     * Convert this element to XML.
126
     *
127
     * @param \DOMElement|null $parent The element we should append this element to.
128
     * @return \DOMElement
129
     */
130
    public function toXML(DOMElement $parent = null): DOMElement
131
    {
132
        $e = $this->instantiateParentElement($parent);
133
134
        list($prefix, $localName) = $this->parseQName($this->content);
135
        if ($e->lookupNamespaceUri($this->namespaceUri) === null && $e->lookupPrefix($prefix)) {
136
            // The namespace is not yet available in the document - insert it
137
            $e->setAttribute('xmlns:' . $prefix, $this->namespaceUri);
138
        }
139
140
        $e->textContent = ($prefix === null) ? $localName : ($prefix . ':' . $localName);
141
142
        return $e;
143
    }
144
145
146
    /** @return string */
147
    abstract public static function getLocalName(): string;
148
149
150
    /**
151
     * Create a document structure for this element
152
     *
153
     * @param \DOMElement|null $parent The element we should append to.
154
     * @return \DOMElement
155
     */
156
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
157
}
158