Passed
Pull Request — master (#2)
by Jaime Pérez
02:47
created

AbstractSignedXMLElement::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\AbstractXMLElement;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\XML\ds\Signature;
13
14
/**
15
 * Abstract class to be implemented by all signed classes
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
abstract class AbstractSignedXMLElement extends AbstractXMLElement implements SignedElementInterface
20
{
21
    use SignedElementTrait;
22
23
    /**
24
     * The signed DOM structure.
25
     *
26
     * @var \DOMElement
27
     */
28
    protected DOMElement $structure;
29
30
    /**
31
     * The unsigned elelement.
32
     *
33
     * @var \SimpleSAML\XMLSecurity\XML\SignableElementInterface
34
     */
35
    protected SignableElementInterface $element;
36
37
38
    /**
39
     * Create/parse an alg:SigningMethod element.
40
     *
41
     * @param \DOMElement $xml
42
     * @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature
43
     */
44
    public function __construct(DOMElement $xml, Signature $signature)
45
    {
46
        $this->setStructure($xml);
47
        $this->setSignature($signature);
48
    }
49
50
51
    /**
52
     * Output the class as an XML-formatted string
53
     *
54
     * @return string
55
     */
56
    public function __toString(): string
57
    {
58
        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

58
        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...
59
    }
60
61
62
    /**
63
     * Set the value of the structure-property
64
     *
65
     * @param \DOMElement $structure
66
     */
67
    private function setStructure(DOMElement $structure): void
68
    {
69
        $this->structure = $structure;
70
    }
71
72
73
    /**
74
     * Create XML from this class
75
     *
76
     * @param \DOMElement|null $parent
77
     * @return \DOMElement
78
     */
79
    public function toXML(DOMElement $parent = null): DOMElement
80
    {
81
        return $this->structure;
82
    }
83
84
85
    /**
86
     * Create a class from XML
87
     *
88
     * @param \DOMElement $xml
89
     * @return self
90
     */
91
    public static function fromXML(DOMElement $xml): object
92
    {
93
        $original = $xml->ownerDocument->cloneNode(true);
0 ignored issues
show
Bug introduced by
The method cloneNode() 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

93
        /** @scrutinizer ignore-call */ 
94
        $original = $xml->ownerDocument->cloneNode(true);

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...
94
95
        $signature = Signature::getChildrenOfClass($xml);
96
        Assert::minCount($signature, 1, MissingElementException::class);
97
        Assert::maxCount($signature, 1, TooManyElementsException::class);
98
99
        return new static($original->documentElement, array_pop($signature));
100
    }
101
}
102