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

TypedTextContentTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 14
dl 0
loc 59
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 6 1
A __construct() 0 3 1
A getTextContentType() 0 10 2
A fromXML() 0 9 1
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\{ValueTypeInterface, StringValue};
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
    /**
23
     * @param \SimpleSAML\XML\Type\ValueTypeInterface $content
24
     */
25
    public function __construct(
26
        protected ValueTypeInterface $content,
27
    ) {
28
    }
29
30
31
    /**
32
     * Create a class from XML
33
     *
34
     * @param \DOMElement $xml
35
     * @return static
36
     */
37
    public static function fromXML(DOMElement $xml): static
38
    {
39
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
40
        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...
41
42
        $type = self::getTextContentType();
43
        $text = $type::fromString($xml->textContent);
44
45
        return new static($text);
46
    }
47
48
49
    /**
50
     * Create XML from this class
51
     *
52
     * @param \DOMElement|null $parent
53
     * @return \DOMElement
54
     */
55
    public function toXML(?DOMElement $parent = null): DOMElement
56
    {
57
        $e = $this->instantiateParentElement($parent);
0 ignored issues
show
Bug introduced by
It seems like instantiateParentElement() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $e = $this->instantiateParentElement($parent);
Loading history...
58
        $e->textContent = strval($this->getContent());
0 ignored issues
show
Bug introduced by
It seems like getContent() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

58
        $e->textContent = strval($this->/** @scrutinizer ignore-call */ getContent());
Loading history...
59
60
        return $e;
61
    }
62
63
64
    /**
65
     * Get the type the element's textContent value.
66
     *
67
     * @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...
68
     */
69
    public static function getTextContentType(): string
70
    {
71
        if (defined('static::TEXTCONTENT_TYPE')) {
72
            $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...
73
        } else {
74
            $type = StringValue::class;
75
        }
76
77
        Assert::isAOf($type, ValueTypeInterface::class, InvalidValueTypeException::class);
78
        return $type;
79
    }
80
}
81