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

AbstractSignedXMLElement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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
use function array_pop;
15
16
/**
17
 * Abstract class to be implemented by all signed classes
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractSignedXMLElement extends AbstractXMLElement implements SignedElementInterface
22
{
23
    use SignedElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XMLSecurity\XML\SignedElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\X...bstractSignedXMLElement: $ownerDocument, $documentElement
Loading history...
24
25
    /**
26
     * The signed DOM structure.
27
     *
28
     * @var \DOMElement
29
     */
30
    protected DOMElement $structure;
31
32
    /**
33
     * The unsigned elelement.
34
     *
35
     * @var \SimpleSAML\XMLSecurity\XML\SignableElementInterface
36
     */
37
    protected SignableElementInterface $element;
38
39
40
    /**
41
     * Create/parse an alg:SigningMethod element.
42
     *
43
     * @param \DOMElement $xml
44
     * @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature
45
     */
46
    public function __construct(DOMElement $xml, Signature $signature)
47
    {
48
        $this->setStructure($xml);
49
        $this->setSignature($signature);
50
    }
51
52
53
    /**
54
     * Output the class as an XML-formatted string
55
     *
56
     * @return string
57
     */
58
    public function __toString(): string
59
    {
60
        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

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

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