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

Code::setSubcode()   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\SOAP12\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SOAP\Constants as C;
10
use SimpleSAML\SOAP\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
15
/**
16
 * Class representing a env:Code element.
17
 *
18
 * @package simplesaml/xml-soap
19
 */
20
final class Code extends AbstractSoapElement
21
{
22
    /**
23
     * The Value element
24
     *
25
     * @var \SimpleSAML\SOAP\XML\env\Value
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env\Value 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...
26
     */
27
    protected Value $value;
28
29
    /**
30
     * The Subcode element
31
     *
32
     * @var \SimpleSAML\SOAP\XML\env\Subcode|null
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env\Subcode 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...
33
     */
34
    protected ?Subcode $subcode;
35
36
37
    /**
38
     * Initialize a soap:Code
39
     *
40
     * @param \SimpleSAML\SOAP\XML\env\Value $value
41
     * @param \SimpleSAML\SOAP\XML\env\Code|null $code
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env\Code 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...
42
     */
43
    public function __construct(Value $value, ?Subcode $subcode)
44
    {
45
        $this->setValue($value);
46
        $this->setSubcode($subcode);
47
    }
48
49
50
    /**
51
     * @return \SimpleSAML\SOAP\XML\env\Value
52
     */
53
    public function getValue(): Value
54
    {
55
        return $this->value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->value returns the type SimpleSAML\SOAP12\XML\env\Value which is incompatible with the documented return type SimpleSAML\SOAP\XML\env\Value.
Loading history...
56
    }
57
58
59
    /**
60
     * @param \SimpleSAML\SOAP\XML\env\Value $value
61
     */
62
    protected function setValue(Value $value): void
63
    {
64
        @list($prefix, $localName) = preg_split('/:/', $value->getContent(), 2);
65
        if ($localName === null) {
66
            // We don't have a prefixed value here
67
            $localName = $prefix;
68
        }
69
70
        Assert::oneOf(
71
            $localName,
72
            C::FAULT_CODES,
73
            'Invalid top-level Value',
74
            ProtocolViolationException::class
75
        );
76
        $this->value = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type SimpleSAML\SOAP12\XML\env\Value is incompatible with the declared type SimpleSAML\SOAP\XML\env\Value of property $value.

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...
77
    }
78
79
80
    /**
81
     * @return \SimpleSAML\SOAP\XML\env\Subcode|null
82
     */
83
    public function getSubcode(): ?Subcode
84
    {
85
        return $this->subcode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->subcode also could return the type SimpleSAML\SOAP12\XML\env\Subcode which is incompatible with the documented return type SimpleSAML\SOAP\XML\env\Subcode|null.
Loading history...
86
    }
87
88
89
    /**
90
     * @param \SimpleSAML\SOAP\XML\env\Subcode|null $subcode
91
     */
92
    protected function setSubcode(?Subcode $subcode): void
93
    {
94
        $this->subcode = $subcode;
0 ignored issues
show
Documentation Bug introduced by
It seems like $subcode can also be of type SimpleSAML\SOAP12\XML\env\Subcode. However, the property $subcode is declared as type SimpleSAML\SOAP\XML\env\Subcode|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...
95
    }
96
97
98
    /**
99
     * Convert XML into an Code element
100
     *
101
     * @param \DOMElement $xml The XML element we should load
102
     * @return static
103
     *
104
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
105
     *   If the qualified name of the supplied element is wrong
106
     */
107
    public static function fromXML(DOMElement $xml): static
108
    {
109
        Assert::same($xml->localName, 'Code', InvalidDOMElementException::class);
110
        Assert::same($xml->namespaceURI, Code::NS, InvalidDOMElementException::class);
111
112
        $value = Value::getChildrenOfClass($xml);
113
        Assert::count($value, 1, 'Must contain exactly one Value', MissingElementException::class);
114
115
        // Assert that the namespace of the value matches the SOAP-ENV namespace
116
        @list($prefix, $localName) = preg_split('/:/', $value[0]->getContent(), 2);
117
        $namespace = $xml->lookupNamespaceUri($prefix);
118
        Assert::same($namespace, C::NS_SOAP_ENV_12);
119
120
        $subcode = Subcode::getChildrenOfClass($xml);
121
        Assert::maxCount($subcode, 1, 'Cannot process more than one Subcode element.', TooManyElementsException::class);
122
123
        return new static(
124
            array_pop($value),
125
            empty($subcode) ? null : array_pop($subcode)
126
        );
127
    }
128
129
130
    /**
131
     * Convert this Code to XML.
132
     *
133
     * @param \DOMElement|null $parent The element we should add this code to.
134
     * @return \DOMElement This Code-element.
135
     */
136
    public function toXML(DOMElement $parent = null): DOMElement
137
    {
138
        $e = $this->instantiateParentElement($parent);
139
140
        $this->value->toXML($e);
141
        $this->subcode?->toXML($e);
142
143
        return $e;
144
    }
145
}
146