Passed
Push — master ( a7523b...62109e )
by Tim
06:52
created

XMLStringElementTrait::validateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Constants;
10
11
/**
12
 * Trait grouping common functionality for simple elements with just some textContent
13
 *
14
 * @package simplesamlphp/xml-common
15
 */
16
trait XMLStringElementTrait
17
{
18
    /** @var string */
19
    protected string $content;
20
21
22
    /**
23
     * @param string $content
24
     */
25
    public function __construct(string $content)
26
    {
27
        $this->setContent($content);
28
    }
29
30
31
    /**
32
     * Set the content of the element.
33
     *
34
     * @param string $content  The value to go in the XML textContent
35
     */
36
    protected function setContent(string $content): void
37
    {
38
        $this->validateContent($content);
39
        $this->content = $content;
40
    }
41
42
43
    /**
44
     * Get the content of the element.
45
     *
46
     * @return string
47
     */
48
    public function getContent(): string
49
    {
50
        return $this->content;
51
    }
52
53
54
    /**
55
     * Validate the content of the element.
56
     *
57
     * @param string $content  The value to go in the XML textContent
58
     * @throws \Exception on failure
59
     * @return void
60
     */
61
    private function validateContent(string $content): void
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed. ( Ignorable by Annotation )

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

61
    private function validateContent(/** @scrutinizer ignore-unused */ string $content): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        /**
64
         * Perform no validation by default.
65
         * Override this method on the implementing class to perform content validation.
66
         */
67
    }
68
69
70
    /**
71
     * Convert this element to XML.
72
     *
73
     * @param \DOMElement|null $parent The element we should append this element to.
74
     * @return \DOMElement
75
     */
76
    public function toXML(DOMElement $parent = null): DOMElement
77
    {
78
        $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

78
        /** @scrutinizer ignore-call */ 
79
        $e = $this->instantiateParentElement($parent);
Loading history...
79
        $e->textContent = $this->content;
80
81
        return $e;
82
    }
83
}
84