TypedTextContentTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 16 3
A __construct() 0 3 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
        protected readonly ValueTypeInterface $content,
31
    ) {
32
    }
33
34
35
    /**
36
     * Get the typed content of the element
37
     *
38
     * @return \SimpleSAML\XMLSchema\Type\Interface\ValueTypeInterface
39
     */
40
    public function getContent(): ValueTypeInterface
41
    {
42
        return $this->content;
43
    }
44
45
46
    /**
47
     * Create a class from XML
48
     *
49
     * @param \DOMElement $xml
50
     */
51
    public static function fromXML(DOMElement $xml): static
52
    {
53
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
54
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
55
56
        $type = self::getTextContentType();
57
        if ($type === QNameValue::class) {
58
            $qName = QNameValue::fromDocument($xml->textContent, $xml);
59
            $text = $qName->getRawValue();
60
        } else {
61
            $text = $xml->textContent;
62
        }
63
64
        return new static($type::fromString($text));
65
    }
66
67
68
    /**
69
     * Create XML from this class
70
     *
71
     * @param \DOMElement|null $parent
72
     */
73
    public function toXML(?DOMElement $parent = null): DOMElement
74
    {
75
        $e = $this->instantiateParentElement($parent);
76
77
        if ($this->getTextContentType() === QNameValue::class) {
78
            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

78
            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...
79
                $e->setAttributeNS(
80
                    'http://www.w3.org/2000/xmlns/',
81
                    '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

81
                    '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...
82
                    $this->getContent()->getNamespaceURI()->getValue(),
83
                );
84
            }
85
        }
86
87
        $e->textContent = strval($this->getContent());
88
        return $e;
89
    }
90
91
92
    /**
93
     * Get the type the element's textContent value.
94
     *
95
     * @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...
96
     */
97
    public static function getTextContentType(): string
98
    {
99
        if (defined('self::TEXTCONTENT_TYPE')) {
100
            $type = self::TEXTCONTENT_TYPE;
101
        } else {
102
            $type = StringValue::class;
103
        }
104
105
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
106
        return $type;
107
    }
108
109
110
    /**
111
     * Create a document structure for this element
112
     *
113
     * @param \DOMElement|null $parent The element we should append to.
114
     */
115
    abstract public function instantiateParentElement(?DOMElement $parent = null): DOMElement;
116
}
117