Passed
Pull Request — master (#302)
by Tim
03:00 queued 29s
created

Action::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Utils;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\StringElementTrait;
12
13
/**
14
 * Class representing SAML2 Action
15
 *
16
 * @package simplesamlphp/saml2
17
 */
18
final class Action extends AbstractSamlElement
19
{
20
    use StringElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\StringElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\Action: $localName, $namespaceURI
Loading history...
21
22
23
    /**
24
     * NOTE: This attribute was marked REQUIRED in the 2012 SAML errata (E36)
25
     *
26
     * @var string
27
     */
28
    protected string $namespace;
29
30
31
    /**
32
     * Initialize an Action.
33
     *
34
     * @param string $namespace
35
     * @param string $content
36
     */
37
    public function __construct(
38
        string $namespace,
39
        string $content
40
    ) {
41
        $this->setNamespace($namespace);
42
        $this->setContent($content);
43
    }
44
45
46
    /**
47
     * Collect the value of the namespace-property
48
     *
49
     * @return string
50
     */
51
    public function getNamespace(): string
52
    {
53
        return $this->namespace;
54
    }
55
56
57
    /**
58
     * Set the value of the namespace-property
59
     *
60
     * @param string $namespace
61
     */
62
    private function setNamespace(string $namespace): void
63
    {
64
        Assert::validURI($namespace, SchemaViolationException::class); // Covers the empty string
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\SchemaViolationException 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...
65
        $this->namespace = $namespace;
66
    }
67
68
69
    /**
70
     * Convert XML into a Action
71
     *
72
     * @param \DOMElement $xml The XML element we should load
73
     * @return static
74
     *
75
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'Action', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, Action::NS, InvalidDOMElementException::class);
81
82
        return new self(
83
            self::getAttribute($xml, 'Namespace', null),
0 ignored issues
show
Bug introduced by
It seems like self::getAttribute($xml, 'Namespace', null) can also be of type null; however, parameter $namespace of SimpleSAML\SAML2\XML\saml\Action::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
            /** @scrutinizer ignore-type */ self::getAttribute($xml, 'Namespace', null),
Loading history...
84
            $xml->textContent,
85
        );
86
    }
87
88
89
    /**
90
     * Convert this Action to XML.
91
     *
92
     * @param \DOMElement|null $parent The element we should append this Action to.
93
     * @return \DOMElement
94
     */
95
    public function toXML(DOMElement $parent = null): DOMElement
96
    {
97
        $e = $this->instantiateParentElement($parent);
98
99
        $e->setAttribute('Namespace', $this->namespace);
100
        $e->textContent = $this->getContent();
101
102
        return $e;
103
    }
104
}
105