Passed
Pull Request — master (#14)
by Tim
03:18
created

Reference::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
11
/**
12
 * Class representing a ds:Reference element.
13
 *
14
 * @package simplesamlphp/xml-security
15
 */
16
final class Reference extends AbstractDsElement
17
{
18
    /** @var \SimpleSAML\XMLSecurity\Transforms|null */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Transforms 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...
19
    protected Transforms $transforms;
20
21
    /** @var \SimpleSAML\XMLSecurity\DigestMethod */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\DigestMethod 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...
22
    protected DigestMethod $digestMethod;
23
24
    /** @var \SimpleSAML\XMLSecurity\DigestValue */
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\DigestValue 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...
25
    protected DigestValue $digestValue;
26
27
    /** @var string|null $Id */
28
    protected ?string $Id;
29
30
    /** @var string|null $type */
31
    protected ?string $Type;
32
33
    /** @var string|null $URI */
34
    protected ?string $URI;
35
36
37
    /**
38
     * Initialize a ds:Reference
39
     *
40
     * @param \SimpleSAML\XMLSecurity\DigestMethod $digestMethod
41
     * @param \SimpleSAML\XMLSecurity\DigestValue $digestValue
42
     * @param \SimpleSAML\XMLSecurity\Transforms|null $transforms
43
     * @param string|null $Id
44
     * @param string|null $Type
45
     * @param string|null $URI
46
     */
47
    public function __construct(
48
        DigestMethod $digestMethod,
49
        DigestValue $digestValue,
50
        ?Transforms $transforms = null,
51
        ?string $Id = null,
52
        ?string $Type = null,
53
        ?string $URI = null
54
    ) {
55
        $this->setTransforms($transforms);
56
        $this->setDigestMethod($digestMethod);
57
        $this->setDigestValue($digestValue);
58
        $this->setId($Id);
59
        $this->setType($Type);
60
        $this->setURI($URI);
61
    }
62
63
64
    /**
65
     * @return \SimpleSAML\XMLSecurity\Transforms|null
66
     */
67
    public function getTransforms(): ?Transforms
68
    {
69
        return $this->transforms;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transforms returns the type SimpleSAML\XMLSecurity\XML\ds\Transforms which is incompatible with the documented return type SimpleSAML\XMLSecurity\Transforms|null.
Loading history...
70
    }
71
72
73
    /**
74
     * @param \SimpleSAML\XMLSecurity\Transforms|null
75
     */
76
    protected function setTransforms(?Transforms $transforms): void
77
    {
78
        $this->transforms = $transforms;
0 ignored issues
show
Documentation Bug introduced by
It seems like $transforms can also be of type SimpleSAML\XMLSecurity\XML\ds\Transforms. However, the property $transforms is declared as type SimpleSAML\XMLSecurity\Transforms|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...
79
    }
80
81
82
    /**
83
     * @return \SimpleSAML\XMLSecurity\DigestMethod
84
     */
85
    public function getDigestMethod(): DigestMethod
86
    {
87
        return $this->digestMethod;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->digestMethod returns the type SimpleSAML\XMLSecurity\XML\ds\DigestMethod which is incompatible with the documented return type SimpleSAML\XMLSecurity\DigestMethod.
Loading history...
88
    }
89
90
91
    /**
92
     * @param \SimpleSAML\XMLSecurity\DigestMethod $digestMethod
93
     */
94
    private function setDigestMethod(DigestMethod $digestMethod): void
95
    {
96
        $this->digestMethod = $digestMethod;
0 ignored issues
show
Documentation Bug introduced by
It seems like $digestMethod of type SimpleSAML\XMLSecurity\XML\ds\DigestMethod is incompatible with the declared type SimpleSAML\XMLSecurity\DigestMethod of property $digestMethod.

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...
97
    }
98
99
100
    /**
101
     * @return \SimpleSAML\XMLSecurity\DigestValue
102
     */
103
    public function getDigestValue(): DigestValue
104
    {
105
        return $this->digestValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->digestValue returns the type SimpleSAML\XMLSecurity\XML\ds\DigestValue which is incompatible with the documented return type SimpleSAML\XMLSecurity\DigestValue.
Loading history...
106
    }
107
108
109
    /**
110
     * @param \SimpleSAML\XMLSecurity\DigestValue $digestValue
111
     */
112
    private function setDigestValue(DigestValue $digestValue): void
113
    {
114
        $this->digestValue = $digestValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $digestValue of type SimpleSAML\XMLSecurity\XML\ds\DigestValue is incompatible with the declared type SimpleSAML\XMLSecurity\DigestValue of property $digestValue.

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...
115
    }
116
117
118
    /**
119
     * @return string
120
     */
121
    public function getId(): string
122
    {
123
        return $this->Id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->Id could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
124
    }
125
126
127
    /**
128
     * @param string|null $Id
129
     */
130
    private function setId(?string $Id): void
131
    {
132
        $this->Id = $Id;
133
    }
134
135
136
    /**
137
     * @return string
138
     */
139
    public function getType(): string
140
    {
141
        return $this->Type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->Type could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
142
    }
143
144
145
    /**
146
     * @param string|null $Type
147
     */
148
    private function setType(?string $Type): void
149
    {
150
        $this->Type = $Type;
151
    }
152
153
154
    /**
155
     * @return string
156
     */
157
    public function getURI(): string
158
    {
159
        return $this->URI;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->URI could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
160
    }
161
162
163
    /**
164
     * @param string|null $URI
165
     */
166
    private function setURI(?string $URI): void
167
    {
168
        $this->URI = $URI;
169
    }
170
171
172
   /**
173
     * Convert XML into a Reference element
174
     *
175
     * @param \DOMElement $xml The XML element we should load
176
     * @return self
177
     *
178
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
179
     *   If the qualified name of the supplied element is wrong
180
     */
181
    public static function fromXML(DOMElement $xml): object
182
    {
183
        Assert::same($xml->localName, 'Reference', InvalidDOMElementException::class);
184
        Assert::same($xml->namespaceURI, Reference::NS, InvalidDOMElementException::class);
185
186
        $Id = self::getAttribute($xml, 'Id', null);
187
        $Type = self::getAttribute($xml, 'Type', null);
188
        $URI = self::getAttribute($xml, 'URI', null);
189
190
        $transforms = Transforms::getChildrenOfClass($xml);
191
        Assert::maxCount($transforms, 1, 'A <ds:Reference> may contain just on <ds:Transforms>.');
192
193
        $digestMethod = DigestMethod::getChildrenOfClass($xml);
194
        Assert::count($digestMethod, 1, 'A <ds:Reference> must contain a <ds:DigestMethod>.');
195
196
        $digestValue = DigestValue::getChildrenOfClass($xml);
197
        Assert::count($digestValue, 1, 'A <ds:Reference> must contain a <ds:DigestValue>.');
198
199
        return new self(
200
            array_pop($digestMethod),
201
            array_pop($digestValue),
202
            empty($transforms) ? null : array_pop($transforms),
203
            $Id,
204
            $Type,
205
            $URI
206
        );
207
    }
208
209
210
    /**
211
     * Convert this Reference element to XML.
212
     *
213
     * @param \DOMElement|null $parent The element we should append this Reference element to.
214
     * @return \DOMElement
215
     */
216
    public function toXML(DOMElement $parent = null): DOMElement
217
    {
218
        $e = $this->instantiateParentElement($parent);
219
        $e->setAttribute('Id', $this->Id);
220
        $e->setAttribute('Type', $this->Type);
221
        $e->setAttribute('URI', $this->URI);
222
223
        if ($this->transforms !== null) {
224
            $this->transforms->toXML($e);
225
        }
226
227
        $this->digestMethod->toXML($e);
228
        $this->digestValue->toXML($e);
229
230
        return $e;
231
    }
232
}
233