Completed
Push — master ( f72cfb...4ef12f )
by Tim
21s queued 18s
created

AbstractElement::__construct()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 48
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 1
nop 18
dl 0
loc 48
rs 9.5222
c 1
b 0
f 0

How to fix   Many Parameters   

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