AbstractElement::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 56
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 18
dl 0
loc 56
rs 9.7333
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSchema\XML;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XML\Constants as C;
10
use SimpleSAML\XMLSchema\Exception\ProtocolViolationException;
11
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12
use SimpleSAML\XMLSchema\Type\BooleanValue;
13
use SimpleSAML\XMLSchema\Type\IDValue;
14
use SimpleSAML\XMLSchema\Type\NCNameValue;
15
use SimpleSAML\XMLSchema\Type\QNameValue;
16
use SimpleSAML\XMLSchema\Type\Schema\BlockSetValue;
17
use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue;
18
use SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue;
19
use SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue;
20
use SimpleSAML\XMLSchema\Type\Schema\MinOccursValue;
21
use SimpleSAML\XMLSchema\Type\StringValue;
22
use SimpleSAML\XMLSchema\XML\Interface\IdentityConstraintInterface;
23
use SimpleSAML\XMLSchema\XML\Trait\DefRefTrait;
24
use SimpleSAML\XMLSchema\XML\Trait\OccursTrait;
25
26
use function strval;
27
28
/**
29
 * Abstract class representing the element-type.
30
 *
31
 * @package simplesamlphp/xml-common
32
 */
33
abstract class AbstractElement extends AbstractAnnotated
34
{
35
    use DefRefTrait;
36
    use OccursTrait;
37
38
39
    /**
40
     * Element constructor
41
     *
42
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $name
43
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $reference
44
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|\SimpleSAML\XMLSchema\XML\LocalComplexType|null $localType
45
     * @param array<\SimpleSAML\XMLSchema\XML\Interface\IdentityConstraintInterface> $identityConstraint
46
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $type
47
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $substitutionGroup
48
     * @param \SimpleSAML\XMLSchema\Type\Schema\MinOccursValue|null $minOccurs
49
     * @param \SimpleSAML\XMLSchema\Type\Schema\MaxOccursValue|null $maxOccurs
50
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $default
51
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $fixed
52
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $nillable
53
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $abstract
54
     * @param \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null $final
55
     * @param \SimpleSAML\XMLSchema\Type\Schema\BlockSetValue|null $block
56
     * @param \SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue|null $form
57
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
58
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
59
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
60
     */
61
    public function __construct(
62
        ?NCNameValue $name = null,
63
        ?QNameValue $reference = null,
64
        protected LocalSimpleType|LocalComplexType|null $localType = null,
65
        protected array $identityConstraint = [],
66
        protected ?QNameValue $type = null,
67
        protected ?QNameValue $substitutionGroup = null,
68
        ?MinOccursValue $minOccurs = null,
69
        ?MaxOccursValue $maxOccurs = null,
70
        protected ?StringValue $default = null,
71
        protected ?StringValue $fixed = null,
72
        protected ?BooleanValue $nillable = null,
73
        protected ?BooleanValue $abstract = null,
74
        protected ?DerivationSetValue $final = null,
75
        protected ?BlockSetValue $block = null,
76
        protected ?FormChoiceValue $form = null,
77
        ?Annotation $annotation = null,
78
        ?IDValue $id = null,
79
        array $namespacedAttributes = [],
80
    ) {
81
        Assert::maxCount($identityConstraint, C::UNBOUNDED_LIMIT);
82
        Assert::allIsInstanceOf(
83
            $identityConstraint,
84
            IdentityConstraintInterface::class,
85
            SchemaViolationException::class,
86
        );
87
88
        /**
89
         * An element is declared by either: a name and a type (either nested or referenced via the type attribute)
90
         * or a ref to an existing element declaration
91
         *
92
         * type and ref are mutually exclusive.
93
         * name and ref are mutually exclusive, one is required
94
         */
95
        Assert::oneOf(null, [$type, $reference], ProtocolViolationException::class);
96
        Assert::oneOf(null, [$name, $reference], ProtocolViolationException::class);
97
        Assert::false(is_null($name) && is_null($reference), ProtocolViolationException::class);
98
99
        /**
100
         * default and fixed are mutually exclusive
101
         */
102
        Assert::oneOf(null, [$default, $fixed], ProtocolViolationException::class);
103
104
        /**
105
         * simpleType or complexType only if no type|ref attribute
106
         */
107
        if ($localType !== null) {
108
            Assert::true(is_null($type) || is_null($reference), ProtocolViolationException::class);
109
        }
110
111
        parent::__construct($annotation, $id, $namespacedAttributes);
112
113
        $this->setName($name);
114
        $this->setReference($reference);
115
        $this->setMinOccurs($minOccurs);
116
        $this->setMaxOccurs($maxOccurs);
117
    }
118
119
120
    /**
121
     * Collect the value of the localType-property
122
     *
123
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|\SimpleSAML\XMLSchema\XML\LocalComplexType|null
124
     */
125
    public function getLocalType(): LocalSimpleType|LocalComplexType|null
126
    {
127
        return $this->localType;
128
    }
129
130
131
    /**
132
     * Collect the value of the identityConstraint-property
133
     *
134
     * @return array<\SimpleSAML\XMLSchema\XML\Interface\IdentityConstraintInterface>
135
     */
136
    public function getIdentityConstraint(): array
137
    {
138
        return $this->identityConstraint;
139
    }
140
141
142
    /**
143
     * Collect the value of the type-property
144
     *
145
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
146
     */
147
    public function getType(): ?QNameValue
148
    {
149
        return $this->type;
150
    }
151
152
153
    /**
154
     * Collect the value of the substitutionGroup-property
155
     *
156
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
157
     */
158
    public function getSubstitutionGroup(): ?QNameValue
159
    {
160
        return $this->substitutionGroup;
161
    }
162
163
164
    /**
165
     * Collect the value of the default-property
166
     *
167
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
168
     */
169
    public function getDefault(): ?StringValue
170
    {
171
        return $this->default;
172
    }
173
174
175
    /**
176
     * Collect the value of the fixed-property
177
     *
178
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
179
     */
180
    public function getFixed(): ?StringValue
181
    {
182
        return $this->fixed;
183
    }
184
185
186
    /**
187
     * Collect the value of the nillable-property
188
     *
189
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
190
     */
191
    public function getNillable(): ?BooleanValue
192
    {
193
        return $this->nillable;
194
    }
195
196
197
    /**
198
     * Collect the value of the abstract-property
199
     *
200
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
201
     */
202
    public function getAbstract(): ?BooleanValue
203
    {
204
        return $this->abstract;
205
    }
206
207
208
    /**
209
     * Collect the value of the final-property
210
     *
211
     * @return \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null
212
     */
213
    public function getFinal(): ?DerivationSetValue
214
    {
215
        return $this->final;
216
    }
217
218
219
    /**
220
     * Collect the value of the block-property
221
     *
222
     * @return \SimpleSAML\XMLSchema\Type\Schema\BlockSetValue|null
223
     */
224
    public function getBlock(): ?BlockSetValue
225
    {
226
        return $this->block;
227
    }
228
229
230
    /**
231
     * Collect the value of the form-property
232
     *
233
     * @return \SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue|null
234
     */
235
    public function getForm(): ?FormChoiceValue
236
    {
237
        return $this->form;
238
    }
239
240
241
    /**
242
     * Test if an object, at the state it's in, would produce an empty XML-element
243
     *
244
     * @return bool
245
     */
246
    public function isEmptyElement(): bool
247
    {
248
        return parent::isEmptyElement() &&
249
            empty($this->getName()) &&
250
            empty($this->getReference()) &&
251
            empty($this->getLocalType()) &&
252
            empty($this->getIdentityConstraint()) &&
253
            empty($this->getType()) &&
254
            empty($this->getSubstitutionGroup()) &&
255
            empty($this->getMinOccurs()) &&
256
            empty($this->getMaxOccurs()) &&
257
            empty($this->getDefault()) &&
258
            empty($this->getFixed()) &&
259
            empty($this->getNillable()) &&
260
            empty($this->getAbstract()) &&
261
            empty($this->getFinal()) &&
262
            empty($this->getBlock()) &&
263
            empty($this->getForm());
264
    }
265
266
267
    /**
268
     * Add this Annotated to an XML element.
269
     *
270
     * @param \DOMElement|null $parent The element we should append this Annotated to.
271
     * @return \DOMElement
272
     */
273
    public function toXML(?DOMElement $parent = null): DOMElement
274
    {
275
        $e = parent::toXML($parent);
276
277
        if ($this->getName() !== null) {
278
            $e->setAttribute('name', strval($this->getName()));
279
        }
280
281
        if ($this->getReference() !== null) {
282
            $e->setAttribute('ref', strval($this->getReference()));
283
        }
284
285
        if ($this->getType() !== null) {
286
            $e->setAttribute('type', strval($this->getType()));
287
        }
288
289
        if ($this->getSubstitutionGroup() !== null) {
290
            $e->setAttribute('substitutionGroup', strval($this->getSubstitutionGroup()));
291
        }
292
293
        if ($this->getMinOccurs() !== null) {
294
            $e->setAttribute('minOccurs', strval($this->getMinOccurs()));
295
        }
296
297
        if ($this->getMaxOccurs() !== null) {
298
            $e->setAttribute('maxOccurs', strval($this->getMaxOccurs()));
299
        }
300
301
        if ($this->getDefault() !== null) {
302
            $e->setAttribute('default', strval($this->getDefault()));
303
        }
304
305
        if ($this->getFixed() !== null) {
306
            $e->setAttribute('fixed', strval($this->getFixed()));
307
        }
308
309
        if ($this->getNillable() !== null) {
310
            $e->setAttribute('nillable', strval($this->getNillable()));
311
        }
312
313
        if ($this->getAbstract() !== null) {
314
            $e->setAttribute('abstract', strval($this->getAbstract()));
315
        }
316
317
        if ($this->getFinal() !== null) {
318
            $e->setAttribute('final', strval($this->getFinal()));
319
        }
320
321
        if ($this->getBlock() !== null) {
322
            $e->setAttribute('block', strval($this->getBlock()));
323
        }
324
325
        if ($this->getForm() !== null) {
326
            $e->setAttribute('form', strval($this->getForm()));
327
        }
328
329
        $this->getLocalType()?->toXML($e);
330
331
        foreach ($this->getIdentityConstraint() as $identityConstraint) {
332
            $identityConstraint->toXML($e);
333
        }
334
335
        return $e;
336
    }
337
}
338