Passed
Push — master ( 39d85d...e43fd4 )
by Tim
13:56
created

TypedTextContentTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setContent() 0 4 1
A __construct() 0 4 1
A getContent() 0 3 1
A fromString() 0 5 1
A toXML() 0 16 3
A getTextContentType() 0 10 2
A fromXML() 0 14 2
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\XMLSchema\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSchema\Exception\InvalidValueTypeException;
11
use SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface;
12
use SimpleSAML\XMLSchema\Type\QNameValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\QNameValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SimpleSAML\XMLSchema\Type\StringValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\StringValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
use function defined;
16
use function strval;
17
18
/**
19
 * Trait for elements that hold a typed textContent value.
20
 *
21
 * @package simplesaml/xml-common
22
 * @phpstan-ignore trait.unused
23
 */
24
trait TypedTextContentTrait
25
{
26
    /**
27
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $content
28
     */
29
    public function __construct(
30
        protected ValueTypeInterface $content,
31
    ) {
32
        $this->setContent($content);
33
    }
34
35
36
    /**
37
     * Set the content of the element.
38
     *
39
     * @param \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface $content  The value to go in the XML textContent
40
     */
41
    protected function setContent(ValueTypeInterface $content): void
42
    {
43
        Assert::isAOf($content, self::getTextContentType(), InvalidValueTypeException::class);
44
        $this->content = $content;
45
    }
46
47
48
    /**
49
     * Get the typed content of the element
50
     *
51
     * @return \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
52
     */
53
    public function getContent(): ValueTypeInterface
54
    {
55
        return $this->content;
56
    }
57
58
59
    /**
60
     * Create a class from string
61
     */
62
    public static function fromString(string $text): static
63
    {
64
        $type = self::getTextContentType();
65
66
        return new static($type::fromString($text));
67
    }
68
69
70
    /**
71
     * Create a class from XML
72
     *
73
     * @param \DOMElement $xml
74
     */
75
    public static function fromXML(DOMElement $xml): static
76
    {
77
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
79
80
        $type = self::getTextContentType();
81
        if ($type === QNameValue::class) {
82
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
83
            $text = $qName->getRawValue();
84
        } else {
85
            $text = $xml->textContent;
86
        }
87
88
        return new static($type::fromString($text));
89
    }
90
91
92
    /**
93
     * Create XML from this class
94
     *
95
     * @param \DOMElement|null $parent
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = $this->instantiateParentElement($parent);
100
101
        if ($this->getTextContentType() === QNameValue::class) {
102
            if (!$e->lookupPrefix($this->getContent()->getNamespaceURI()->getValue())) {
0 ignored issues
show
Bug introduced by
The method getNamespaceURI() does not exist on SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface. ( Ignorable by Annotation )

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

102
            if (!$e->lookupPrefix($this->getContent()->/** @scrutinizer ignore-call */ getNamespaceURI()->getValue())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
                $e->setAttributeNS(
104
                    'http://www.w3.org/2000/xmlns/',
105
                    'xmlns:' . $this->getContent()->getNamespacePrefix()->getValue(),
0 ignored issues
show
Bug introduced by
The method getNamespacePrefix() does not exist on SimpleSAML\XMLSchema\Typ...face\ValueTypeInterface. ( Ignorable by Annotation )

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

105
                    'xmlns:' . $this->getContent()->/** @scrutinizer ignore-call */ getNamespacePrefix()->getValue(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
                    $this->getContent()->getNamespaceURI()->getValue(),
107
                );
108
            }
109
        }
110
111
        $e->textContent = strval($this->getContent());
112
        return $e;
113
    }
114
115
116
    /**
117
     * Get the type the element's textContent value.
118
     *
119
     * @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...
120
     */
121
    public static function getTextContentType(): string
122
    {
123
        if (defined('self::TEXTCONTENT_TYPE')) {
124
            $type = self::TEXTCONTENT_TYPE;
125
        } else {
126
            $type = StringValue::class;
127
        }
128
129
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
130
        return $type;
131
    }
132
133
134
    /**
135
     * Create a document structure for this element
136
     *
137
     * @param \DOMElement|null $parent The element we should append to.
138
     */
139
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
140
}
141