Passed
Push — master ( f4dd67...4250cc )
by Tim
02:02
created

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

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