AbstractComplexType::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 12
dl 0
loc 33
rs 9.8333
c 0
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;
6
7
use DOMElement;
8
use SimpleSAML\XML\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\ProtocolViolationException;
10
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
11
use SimpleSAML\XMLSchema\Type\BooleanValue;
12
use SimpleSAML\XMLSchema\Type\IDValue;
13
use SimpleSAML\XMLSchema\Type\NCNameValue;
14
use SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue;
15
use SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface;
16
use SimpleSAML\XMLSchema\XML\Trait\ComplexTypeModelTrait;
17
18
use function strval;
19
20
/**
21
 * Abstract class representing the complexType-type.
22
 *
23
 * @package simplesamlphp/xml-common
24
 */
25
abstract class AbstractComplexType extends AbstractAnnotated
26
{
27
    use ComplexTypeModelTrait;
28
29
30
    /**
31
     * ComplexType constructor
32
     *
33
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $name
34
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $mixed
35
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $abstract
36
     * @param \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null $final
37
     * @param \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null $block
38
     * @param \SimpleSAML\XMLSchema\XML\SimpleContent|\SimpleSAML\XMLSchema\XML\ComplexContent|null $content
39
     * @param \SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface|null $particle
40
     * @param (
41
     *   \SimpleSAML\XMLSchema\XML\LocalAttribute|
42
     *   \SimpleSAML\XMLSchema\XML\ReferencedAttributeGroup
43
     * )[] $attributes
44
     * @param \SimpleSAML\XMLSchema\XML\AnyAttribute|null $anyAttribute
45
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
46
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
47
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
48
     */
49
    public function __construct(
50
        protected ?NCNameValue $name = null,
51
        protected ?BooleanValue $mixed = null,
52
        protected ?BooleanValue $abstract = null,
53
        protected ?DerivationSetValue $final = null,
54
        protected ?DerivationSetValue $block = null,
55
        SimpleContent|ComplexContent|null $content = null,
56
        ?TypeDefParticleInterface $particle = null,
57
        array $attributes = [],
58
        ?AnyAttribute $anyAttribute = null,
59
        ?Annotation $annotation = null,
60
        ?IDValue $id = null,
61
        array $namespacedAttributes = [],
62
    ) {
63
        if ($content !== null) {
64
            Assert::null($particle, SchemaViolationException::class);
65
            Assert::isEmpty($attributes, SchemaViolationException::class);
66
            Assert::null($anyAttribute, SchemaViolationException::class);
67
68
            $this->setContent($content);
69
70
            if ($content instanceof SimpleContent) {
71
                Assert::null($mixed, ProtocolViolationException::class, 'mixed is disallowed if simpleContent');
72
            }
73
        } else {
74
            Assert::null($content, SchemaViolationException::class);
75
76
            $this->setParticle($particle);
77
            $this->setAttributes($attributes);
78
            $this->setAnyAttribute($anyAttribute);
79
        }
80
81
        parent::__construct($annotation, $id, $namespacedAttributes);
82
    }
83
84
85
    /**
86
     * Collect the value of the name-property
87
     *
88
     * @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
89
     */
90
    public function getName(): ?NCNameValue
91
    {
92
        return $this->name;
93
    }
94
95
96
    /**
97
     * Collect the value of the mixed-property
98
     *
99
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
100
     */
101
    public function getMixed(): ?BooleanValue
102
    {
103
        return $this->mixed;
104
    }
105
106
107
    /**
108
     * Collect the value of the abstract-property
109
     *
110
     * @return \SimpleSAML\XMLSchema\Type\BooleanValue|null
111
     */
112
    public function getAbstract(): ?BooleanValue
113
    {
114
        return $this->abstract;
115
    }
116
117
118
    /**
119
     * Collect the value of the final-property
120
     *
121
     * @return \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null
122
     */
123
    public function getFinal(): ?DerivationSetValue
124
    {
125
        return $this->final;
126
    }
127
128
129
    /**
130
     * Collect the value of the block-property
131
     *
132
     * @return \SimpleSAML\XMLSchema\Type\Schema\DerivationSetValue|null
133
     */
134
    public function getBlock(): ?DerivationSetValue
135
    {
136
        return $this->block;
137
    }
138
139
140
    /**
141
     * Test if an object, at the state it's in, would produce an empty XML-element
142
     *
143
     * @return bool
144
     */
145
    public function isEmptyElement(): bool
146
    {
147
        return parent::isEmptyElement() &&
148
            empty($this->getName()) &&
149
            empty($this->getMixed()) &&
150
            empty($this->getAbstract()) &&
151
            empty($this->getFinal()) &&
152
            empty($this->getBlock()) &&
153
            empty($this->getAttributes()) &&
154
            empty($this->getAnyAttribute()) &&
155
            empty($this->getParticle()) &&
156
            empty($this->getContent());
157
    }
158
159
160
    /**
161
     * Add this ComplexType to an XML element.
162
     *
163
     * @param \DOMElement|null $parent The element we should append this ComplexType to.
164
     * @return \DOMElement
165
     */
166
    public function toXML(?DOMElement $parent = null): DOMElement
167
    {
168
        $e = parent::toXML($parent);
169
170
        if ($this->getName() !== null) {
171
            $e->setAttribute('name', strval($this->getName()));
172
        }
173
174
        if ($this->getMixed() !== null) {
175
            $e->setAttribute('mixed', strval($this->getMixed()));
176
        }
177
178
        if ($this->getAbstract() !== null) {
179
            $e->setAttribute('abstract', strval($this->getAbstract()));
180
        }
181
182
        if ($this->getFinal() !== null) {
183
            $e->setAttribute('final', strval($this->getFinal()));
184
        }
185
186
        if ($this->getBlock() !== null) {
187
            $e->setAttribute('block', strval($this->getBlock()));
188
        }
189
190
        if ($this->getContent() !== null) {
191
            $this->getContent()->toXML($e);
192
        } else {
193
            $this->getParticle()?->toXML($e);
194
195
            foreach ($this->getAttributes() as $attr) {
196
                $attr->toXML($e);
197
            }
198
199
            $this->getAnyAttribute()?->toXML($e);
200
        }
201
202
        return $e;
203
    }
204
}
205