Passed
Push — master ( c726f3...c686fd )
by Tim
10:07
created

Envelope::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP11\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
14
/**
15
 * Class representing a env:Envelope element.
16
 *
17
 * @package simplesaml/xml-soap
18
 */
19
final class Envelope extends AbstractSoapElement
20
{
21
    use ExtendableAttributesTrait;
22
23
    /**
24
     * The Header element
25
     *
26
     * @var \SimpleSAML\SOAP\XML\env\Header|null
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env\Header 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...
27
     */
28
    protected ?Header $header;
29
30
    /**
31
     * The Body element
32
     *
33
     * @var \SimpleSAML\SOAP\XML\env\Body
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env\Body 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...
34
     */
35
    protected Body $body;
36
37
38
    /**
39
     * Initialize a env:Envelope
40
     *
41
     * @param \SimpleSAML\SOAP\XML\env\Body $body
42
     * @param \SimpleSAML\SOAP\XML\env\Header|null $header
43
     * @param \DOMAttr[] $namespacedAttributes
44
     */
45
    public function __construct(Body $body, ?Header $header = null, array $namespacedAttributes = [])
46
    {
47
        $this->setBody($body);
48
        $this->setHeader($header);
49
        $this->setAttributesNS($namespacedAttributes);
50
    }
51
52
53
    /**
54
     * @return \SimpleSAML\SOAP\XML\env\Body
55
     */
56
    public function getBody(): Body
57
    {
58
        return $this->body;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->body returns the type SimpleSAML\SOAP11\XML\env\Body which is incompatible with the documented return type SimpleSAML\SOAP\XML\env\Body.
Loading history...
59
    }
60
61
62
    /**
63
     * @param \SimpleSAML\SOAP\XML\env\Body $body
64
     */
65
    protected function setBody(Body $body): void
66
    {
67
        $this->body = $body;
0 ignored issues
show
Documentation Bug introduced by
It seems like $body of type SimpleSAML\SOAP11\XML\env\Body is incompatible with the declared type SimpleSAML\SOAP\XML\env\Body of property $body.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
    }
69
70
71
    /**
72
     * @return \SimpleSAML\SOAP\XML\env\Header|null
73
     */
74
    public function getHeader(): ?Header
75
    {
76
        return $this->header;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->header also could return the type SimpleSAML\SOAP11\XML\env\Header which is incompatible with the documented return type SimpleSAML\SOAP\XML\env\Header|null.
Loading history...
77
    }
78
79
80
    /**
81
     * @param \SimpleSAML\SOAP\XML\env\Header|null $header
82
     */
83
    protected function setHeader(?Header $header): void
84
    {
85
        $this->header = $header;
0 ignored issues
show
Documentation Bug introduced by
It seems like $header can also be of type SimpleSAML\SOAP11\XML\env\Header. However, the property $header is declared as type SimpleSAML\SOAP\XML\env\Header|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
86
    }
87
88
89
    /**
90
     * Convert XML into an Envelope element
91
     *
92
     * @param \DOMElement $xml The XML element we should load
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
96
     *   If the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        Assert::same($xml->localName, 'Envelope', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, Envelope::NS, InvalidDOMElementException::class);
102
103
        $body = Body::getChildrenOfClass($xml);
104
        Assert::count($body, 1, 'Must contain exactly one Body', MissingElementException::class);
105
106
        $header = Header::getChildrenOfClass($xml);
107
        Assert::maxCount($header, 1, 'Cannot process more than one Header element.', TooManyElementsException::class);
108
109
        return new static(
110
            array_pop($body),
111
            empty($header) ? null : array_pop($header),
112
            self::getAttributesNSFromXML($xml)
113
        );
114
    }
115
116
117
    /**
118
     * Convert this Envelope to XML.
119
     *
120
     * @param \DOMElement|null $parent The element we should add this envelope to.
121
     * @return \DOMElement This Envelope-element.
122
     */
123
    public function toXML(DOMElement $parent = null): DOMElement
124
    {
125
        $e = $this->instantiateParentElement($parent);
126
127
        foreach ($this->getAttributesNS() as $attr) {
128
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
129
        }
130
131
        if ($this->getHeader() !== null && !$this->getHeader()->isEmptyElement()) {
132
            $this->getHeader()->toXML($e);
133
        }
134
135
        $this->getBody()->toXML($e);
136
137
        return $e;
138
    }
139
}
140