Completed
Push — master ( 6c6948...3444f2 )
by Tim
19s queued 12s
created

LocalizedStringElementTrait::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 as C;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingAttributeException;
12
13
/**
14
 * Trait grouping common functionality for simple localized string elements
15
 *
16
 * @package simplesamlphp/xml-common
17
 */
18
trait LocalizedStringElementTrait
19
{
20
    use XMLStringElementTrait;
21
22
    /**
23
     * The language this string is on.
24
     *
25
     * @var string
26
     */
27
    protected string $language;
28
29
30
    /**
31
     * Validate the content of the element.
32
     *
33
     * @param string $content  The value to go in the XML textContent
34
     * @throws \Exception on failure
35
     * @return void
36
     */
37
    protected function validateContent(string $content): void
38
    {
39
        Assert::notEmpty($content);
40
    }
41
42
43
    /**
44
     * Get the language this string is localized in.
45
     *
46
     * @return string
47
     */
48
    public function getLanguage(): string
49
    {
50
        return $this->language;
51
    }
52
53
54
    /**
55
     * Set the language this string is localized in.
56
     *
57
     * @param string $language
58
     */
59
    protected function setLanguage(string $language): void
60
    {
61
        Assert::notEmpty($language, 'xml:lang cannot be empty.');
62
        $this->language = $language;
63
    }
64
65
66
    /**
67
     * Convert XML into a class instance
68
     *
69
     * @param \DOMElement $xml The XML element we should load
70
     * @return self
71
     *
72
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
73
     *   If the qualified name of the supplied element is wrong
74
     */
75
    public static function fromXML(DOMElement $xml): object
76
    {
77
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\LocalizedStringElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
        Assert::true(
80
            $xml->hasAttributeNS(C::NS_XML, 'lang'),
81
            'Missing xml:lang from ' . static::getLocalName(),
82
            MissingAttributeException::class,
83
        );
84
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
85
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
86
87
        return new static($xml->getAttributeNS(C::NS_XML, 'lang'), $xml->textContent);
1 ignored issue
show
Unused Code introduced by
The call to SimpleSAML\XML\Localized...entTrait::__construct() has too many arguments starting with $xml->getAttributeNS(Sim...stants::NS_XML, 'lang'). ( Ignorable by Annotation )

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

87
        return /** @scrutinizer ignore-call */ new static($xml->getAttributeNS(C::NS_XML, 'lang'), $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...
88
    }
89
90
91
    /**
92
     * @param \DOMElement|null $parent
93
     * @return \DOMElement
94
     */
95
    final public function toXML(DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
        $e->setAttributeNS(C::NS_XML, 'xml:lang', $this->language);
99
        $e->textContent = $this->getContent();
100
101
        return $e;
102
    }
103
}
104