Passed
Pull Request — master (#55)
by Tim
01:56
created

TypedTextContentTrait::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\Exception\{InvalidDOMElementException, InvalidValueTypeException};
10
use SimpleSAML\XML\Type\{QNameValue, StringValue, ValueTypeInterface};
11
12
use function defined;
13
use function strval;
14
15
/**
16
 * Trait for elements that hold a typed textContent value.
17
 *
18
 * @package simplesaml/xml-common
19
 */
20
trait TypedTextContentTrait
21
{
22
    /** @var \SimpleSAML\XML\Type\ValueTypeInterface */
23
    protected ValueTypeInterface $content;
24
25
26
    /**
27
     * Set the content of the element.
28
     *
29
     * @param \SimpleSAML\XML\Type\ValueTypeInterface $content  The value to go in the XML textContent
30
     */
31
    protected function setContent(ValueTypeInterface $content): void
32
    {
33
        Assert::isAOf($content, static::getTextContentType(), InvalidValueTypeException::class);
34
        $this->content = $content;
35
    }
36
37
38
    /**
39
     * Get the typed content of the element
40
     *
41
     * @return \SimpleSAML\XML\Type\ValueTypeInterface
42
     */
43
    public function getContent(): ValueTypeInterface
44
    {
45
        return $this->content;
46
    }
47
48
49
    /**
50
     * Create a class from XML
51
     *
52
     * @param \DOMElement $xml
53
     * @return static
54
     */
55
    public static function fromXML(DOMElement $xml): static
56
    {
57
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
58
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\TypedTextContentTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
60
        $type = self::getTextContentType();
61
        if ($type === QNameValue::class) {
62
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
63
            $text = $qName->getRawValue();
64
        } else {
65
            $text = $xml->textContent;
66
        }
67
68
        return new static($type::fromString($text));
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\XML\TypedText...entTrait::__construct() has too many arguments starting with $type::fromString($text). ( Ignorable by Annotation )

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

68
        return /** @scrutinizer ignore-call */ new static($type::fromString($text));

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...
69
    }
70
71
72
    /**
73
     * Create XML from this class
74
     *
75
     * @param \DOMElement|null $parent
76
     * @return \DOMElement
77
     */
78
    public function toXML(?DOMElement $parent = null): DOMElement
79
    {
80
        $e = $this->instantiateParentElement($parent);
81
82
        if ($this->getTextContentType() === QNameValue::class) {
83
            if (!$e->lookupPrefix($this->getContent()->getNamespaceURI()->getValue())) {
0 ignored issues
show
Bug introduced by
The method getNamespaceURI() does not exist on SimpleSAML\XML\Type\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XML\Type\ValueTypeInterface such as SimpleSAML\XML\Type\QNameValue. ( Ignorable by Annotation )

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

83
            if (!$e->lookupPrefix($this->getContent()->/** @scrutinizer ignore-call */ getNamespaceURI()->getValue())) {
Loading history...
84
                $e->setAttributeNS(
85
                    'http://www.w3.org/2000/xmlns/',
86
                    'xmlns:' . $this->getContent()->getNamespacePrefix()->getValue(),
0 ignored issues
show
Bug introduced by
The method getNamespacePrefix() does not exist on SimpleSAML\XML\Type\ValueTypeInterface. It seems like you code against a sub-type of SimpleSAML\XML\Type\ValueTypeInterface such as SimpleSAML\XML\Type\QNameValue. ( Ignorable by Annotation )

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

86
                    'xmlns:' . $this->getContent()->/** @scrutinizer ignore-call */ getNamespacePrefix()->getValue(),
Loading history...
87
                    $this->getContent()->getNamespaceURI()->getValue(),
88
                );
89
            }
90
        }
91
92
        $e->textContent = strval($this->getContent());
93
94
        return $e;
95
    }
96
97
98
    /**
99
     * Get the type the element's textContent value.
100
     *
101
     * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
102
     */
103
    public static function getTextContentType(): string
104
    {
105
        if (defined('static::TEXTCONTENT_TYPE')) {
106
            $type = static::TEXTCONTENT_TYPE;
1 ignored issue
show
Bug introduced by
The constant SimpleSAML\XML\TypedText...Trait::TEXTCONTENT_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
107
        } else {
108
            $type = StringValue::class;
109
        }
110
111
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
112
        return $type;
113
    }
114
115
116
    /**
117
     * Create a document structure for this element
118
     *
119
     * @param \DOMElement|null $parent The element we should append to.
120
     * @return \DOMElement
121
     */
122
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
123
}
124