Completed
Push — master ( 3444f2...316fc7 )
by Tim
15s queued 12s
created

StringElementTrait::getContent()   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\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
12
/**
13
 * Trait grouping common functionality for simple elements with just some textContent
14
 *
15
 * @package simplesamlphp/xml-common
16
 */
17
trait StringElementTrait
18
{
19
    /** @var string */
20
    protected string $content;
21
22
23
    /**
24
     * Set the content of the element.
25
     *
26
     * @param string $content  The value to go in the XML textContent
27
     */
28
    protected function setContent(string $content): void
29
    {
30
        $this->validateContent($content);
31
        $this->content = $content;
32
    }
33
34
35
    /**
36
     * Get the content of the element.
37
     *
38
     * @return string
39
     */
40
    public function getContent(): string
41
    {
42
        return $this->content;
43
    }
44
45
46
    /**
47
     * Validate the content of the element.
48
     *
49
     * @param string $content  The value to go in the XML textContent
50
     * @throws \Exception on failure
51
     * @return void
52
     */
53
    protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
54
    {
55
        /**
56
         * Perform no validation by default.
57
         * Override this method on the implementing class to perform content validation.
58
         */
59
    }
60
61
62
    /**
63
     * Convert XML into a class instance
64
     *
65
     * @param \DOMElement $xml The XML element we should load
66
     * @return self
67
     *
68
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
69
     *   If the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): object
72
    {
73
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
74
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\StringElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
76
        return new static($xml->textContent);
1 ignored issue
show
Unused Code introduced by
The call to SimpleSAML\XML\StringElementTrait::__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

76
        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...
77
    }
78
79
80
    /**
81
     * Convert this element to XML.
82
     *
83
     * @param \DOMElement|null $parent The element we should append this element to.
84
     * @return \DOMElement
85
     */
86
    public function toXML(DOMElement $parent = null): DOMElement
87
    {
88
        $e = $this->instantiateParentElement($parent);
89
        $e->textContent = $this->getContent();
90
91
        return $e;
92
    }
93
94
95
    /** @return string */
96
    abstract public static function getLocalName(): string;
97
98
99
    /**
100
     * Create a document structure for this element
101
     *
102
     * @param \DOMElement|null $parent The element we should append to.
103
     * @return \DOMElement
104
     */
105
    abstract public function instantiateParentElement(DOMElement $parent = null): DOMElement;
106
}
107