Passed
Pull Request — master (#2)
by Tim
06:49
created

AbstractSignedXMLElement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 4 1
A fromXML() 0 7 1
A toXML() 0 3 1
A setStructure() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\MissingElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XMLSecurity\XML\ds\Signature;
12
13
/**
14
 * Abstract class to be implemented by all signed classes
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
abstract class AbstractSignedXMLElement implements SignedElementInterface
19
{
20
    use SignedElementTrait;
21
22
    /**
23
     * The signed DOM structure.
24
     *
25
     * @var \DOMElement
26
     */
27
    protected DOMElement $structure;
28
29
    /**
30
     * The unsigned elelement.
31
     *
32
     * @var \SimpleSAML\XMLSecurity\XML\SignableElementInterface
33
     */
34
    protected SignableElementInterface $element;
35
36
37
    /**
38
     * Create/parse an alg:SigningMethod element.
39
     *
40
     * @param \DOMElement $xml
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature
42
     */
43
    public function __construct(DOMElement $xml, Signature $signature)
44
    {
45
        $this->setStructure($xml);
46
        $this->setSignature($signature);
47
    }
48
49
50
    /**
51
     * Output the class as an XML-formatted string
52
     *
53
     * @return string
54
     */
55
    public function __toString(): string
56
    {
57
        return $this->structure->ownerDocument->saveXML();
0 ignored issues
show
Bug introduced by
The method saveXML() does not exist on null. ( Ignorable by Annotation )

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

57
        return $this->structure->ownerDocument->/** @scrutinizer ignore-call */ saveXML();

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...
58
    }
59
60
61
    /**
62
     * Set the value of the structure-property
63
     *
64
     * @param \DOMElement $structure
65
     */
66
    private function setStructure(DOMElement $structure): void
67
    {
68
        $this->structure = $structure;
69
    }
70
71
72
    /**
73
     * Create XML from this class
74
     *
75
     * @param \DOMElement|null $parent
76
     * @return \DOMElement
77
     */
78
    public function toXML(DOMElement $parent = null): DOMElement
0 ignored issues
show
Unused Code introduced by
The parameter $parent 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

78
    public function toXML(/** @scrutinizer ignore-unused */ DOMElement $parent = null): DOMElement

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...
79
    {
80
        return $this->structure;
81
    }
82
83
84
    /**
85
     * Create a class from XML
86
     *
87
     * @param \DOMElement $xml
88
     * @return self
89
     */
90
    public static function fromXML(DOMElement $xml): object
91
    {
92
        $signature = Signature::getChildrenOfClass($xml);
93
        Assert::minCount($signature, 1, MissingElementException::class);
94
        Assert::maxCount($signature, 1, TooManyElementsException::class);
95
96
        return new self($xml, array_pop($signature));
97
    }
98
}
99