Passed
Push — master ( 1a8f47...5223c5 )
by Tim
01:46
created

XMLBase64ElementTrait::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
12
use function str_replace;
13
14
/**
15
 * Trait grouping common functionality for simple elements with base64 textContent
16
 *
17
 * @package simplesamlphp/xml-common
18
 */
19
trait XMLBase64ElementTrait
20
{
21
    /** @var string */
22
    protected string $content;
23
24
25
    /**
26
     * Set the content of the element.
27
     *
28
     * @param string $content  The value to go in the XML textContent
29
     */
30
    protected function setContent(string $content): void
31
    {
32
        $this->validateContent($content);
33
        $this->content = $content;
34
    }
35
36
37
    /**
38
     * Get the content of the element.
39
     *
40
     * @return string
41
     */
42
    public function getContent(): string
43
    {
44
        return $this->sanitizeContent($this->getRawContent());
45
    }
46
47
48
    /**
49
     * Get the raw and unsanitized content of the element.
50
     *
51
     * @return string
52
     */
53
    public function getRawContent(): string
54
    {
55
        return $this->content;
56
    }
57
58
59
    /**
60
     * Sanitize the content of the element.
61
     *
62
     * @param string $content  The unsanitized textContent
63
     * @throws \Exception on failure
64
     * @return string
65
     */
66
    protected function sanitizeContent(string $content): string
67
    {
68
        return str_replace(["\r", "\n", "\t", ' '], '', $content);
69
    }
70
71
72
    /**
73
     * Validate the content of the element.
74
     *
75
     * @param string $content  The value to go in the XML textContent
76
     * @throws \Exception on failure
77
     * @return void
78
     */
79
    protected function validateContent(string $content): void
80
    {
81
        // Note: content must already be sanitized before validating
82
        Assert::stringPlausibleBase64($this->sanitizeContent($content));
83
    }
84
85
86
    /**
87
     * Convert XML into a class instance
88
     *
89
     * @param \DOMElement $xml The XML element we should load
90
     * @return self
91
     *
92
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
93
     *   If the qualified name of the supplied element is wrong
94
     */
95
    public static function fromXML(DOMElement $xml): object
96
    {
97
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\XML\XMLBase64ElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
100
        return new static($xml->textContent);
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\XML\XMLBase64...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

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

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...
101
    }
102
103
104
    /**
105
     * Convert this element to XML.
106
     *
107
     * @param \DOMElement|null $parent The element we should append this element to.
108
     * @return \DOMElement
109
     */
110
    public function toXML(DOMElement $parent = null): DOMElement
111
    {
112
        $e = $this->instantiateParentElement($parent);
113
        $e->textContent = $this->getRawContent();
114
115
        return $e;
116
    }
117
118
119
    /** @return string */
120
    abstract public static function getLocalName(): string;
121
122
123
    /**
124
     * Create a document structure for this element
125
     *
126
     * @param \DOMElement|null $parent The element we should append to.
127
     * @return \DOMElement
128
     */
129
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
130
}
131