Passed
Push — master ( 4cdecf...eb2eb4 )
by Tim
01:25
created

TypedTextContentTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 16 3
A setContent() 0 4 1
A __construct() 0 4 1
A getTextContentType() 0 10 2
A getContent() 0 3 1
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
        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;
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 XML
61
     *
62
     * @param \DOMElement $xml
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
67
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68
69
        $type = self::getTextContentType();
70
        if ($type === QNameValue::class) {
71
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
72
            $text = $qName->getRawValue();
73
        } else {
74
            $text = $xml->textContent;
75
        }
76
77
        return new static($type::fromString($text));
78
    }
79
80
81
    /**
82
     * Create XML from this class
83
     *
84
     * @param \DOMElement|null $parent
85
     */
86
    public function toXML(?DOMElement $parent = null): DOMElement
87
    {
88
        $e = $this->instantiateParentElement($parent);
89
90
        if ($this->getTextContentType() === QNameValue::class) {
91
            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

91
            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...
92
                $e->setAttributeNS(
93
                    'http://www.w3.org/2000/xmlns/',
94
                    '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

94
                    '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...
95
                    $this->getContent()->getNamespaceURI()->getValue(),
96
                );
97
            }
98
        }
99
100
        $e->textContent = strval($this->getContent());
101
        return $e;
102
    }
103
104
105
    /**
106
     * Get the type the element's textContent value.
107
     *
108
     * @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...
109
     */
110
    public static function getTextContentType(): string
111
    {
112
        if (defined('self::TEXTCONTENT_TYPE')) {
113
            $type = self::TEXTCONTENT_TYPE;
114
        } else {
115
            $type = StringValue::class;
116
        }
117
118
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
119
        return $type;
120
    }
121
122
123
    /**
124
     * Create a document structure for this element
125
     *
126
     * @param \DOMElement|null $parent The element we should append to.
127
     */
128
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
129
}
130