Passed
Push — master ( 8413b2...00f101 )
by Tim
02:19
created

AbstractAttribute::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 32
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 11
dl 0
loc 32
rs 9.9332
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\Type\{IDValue, NCNameValue, QNameValue, StringValue};
11
use SimpleSAML\XMLSchema\Type\Schema\{FormChoiceValue, UseValue};
12
use SimpleSAML\XMLSchema\XML\Trait\{DefRefTrait, FormChoiceTrait};
13
14
use function strval;
15
16
/**
17
 * Abstract class representing the attribute-type.
18
 *
19
 * @package simplesamlphp/xml-common
20
 */
21
abstract class AbstractAttribute extends AbstractAnnotated
22
{
23
    use DefRefTrait;
24
    use FormChoiceTrait;
25
26
27
    /**
28
     * Attribute constructor
29
     *
30
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $type
31
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $name
32
     * @param \SimpleSAML\XMLSchema\Type\QNameValue|null $reference
33
     * @param \SimpleSAML\XMLSchema\Type\Schema\UseValue|null $use
34
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $default
35
     * @param \SimpleSAML\XMLSchema\Type\StringValue|null $fixed
36
     * @param \SimpleSAML\XMLSchema\Type\Schema\FormChoiceValue|null $formChoice
37
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $simpleType
38
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
39
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
40
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
41
     */
42
    public function __construct(
43
        protected ?QNameValue $type = null,
44
        ?NCNameValue $name = null,
45
        ?QNameValue $reference = null,
46
        protected ?UseValue $use = null,
47
        protected ?StringValue $default = null,
48
        protected ?StringValue $fixed = null,
49
        ?FormChoiceValue $formChoice = null,
50
        protected ?LocalSimpleType $simpleType = null,
51
        ?Annotation $annotation = null,
52
        ?IDValue $id = null,
53
        array $namespacedAttributes = [],
54
    ) {
55
        Assert::oneOf(null, [$type, $reference], ProtocolViolationException::class);
56
        Assert::oneOf(null, [$name, $reference], ProtocolViolationException::class);
57
        Assert::false(is_null($name) && is_null($reference), ProtocolViolationException::class);
58
59
        /**
60
         * default and fixed are mutually exclusive
61
         */
62
        Assert::oneOf(null, [$default, $fixed], ProtocolViolationException::class);
63
64
        // simpleType only if no type|ref attribute
65
        if ($simpleType !== null) {
66
            Assert::true(is_null($type) && is_null($reference), ProtocolViolationException::class);
67
        }
68
69
        parent::__construct($annotation, $id, $namespacedAttributes);
70
71
        $this->setName($name);
72
        $this->setReference($reference);
73
        $this->setFormChoice($formChoice);
74
    }
75
76
77
    /**
78
     * Collect the value of the simpleType-property
79
     *
80
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|null
81
     */
82
    public function getSimpleType(): ?LocalSimpleType
83
    {
84
        return $this->simpleType;
85
    }
86
87
88
    /**
89
     * Collect the value of the type-property
90
     *
91
     * @return \SimpleSAML\XMLSchema\Type\QNameValue|null
92
     */
93
    public function getType(): ?QNameValue
94
    {
95
        return $this->type;
96
    }
97
98
99
    /**
100
     * Collect the value of the use-property
101
     *
102
     * @return \SimpleSAML\XMLSchema\Type\Schema\UseValue|null
103
     */
104
    public function getUse(): ?UseValue
105
    {
106
        return $this->use;
107
    }
108
109
110
    /**
111
     * Collect the value of the default-property
112
     *
113
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
114
     */
115
    public function getDefault(): ?StringValue
116
    {
117
        return $this->default;
118
    }
119
120
121
    /**
122
     * Collect the value of the fixed-property
123
     *
124
     * @return \SimpleSAML\XMLSchema\Type\StringValue|null
125
     */
126
    public function getFixed(): ?StringValue
127
    {
128
        return $this->fixed;
129
    }
130
131
132
    /**
133
     * Add this Attribute to an XML element.
134
     *
135
     * @param \DOMElement|null $parent The element we should append this facet to.
136
     * @return \DOMElement
137
     */
138
    public function toXML(?DOMElement $parent = null): DOMElement
139
    {
140
        $e = parent::toXML($parent);
141
142
        if ($this->getName() !== null) {
143
            $e->setAttribute('name', strval($this->getName()));
144
        }
145
146
        if ($this->getReference() !== null) {
147
            $e->setAttribute('ref', strval($this->getReference()));
148
        }
149
150
        if ($this->getType() !== null) {
151
            $e->setAttribute('type', strval($this->getType()));
152
        }
153
154
        if ($this->getUse() !== null) {
155
            $e->setAttribute('use', strval($this->getUse()));
156
        }
157
158
        if ($this->getDefault() !== null) {
159
            $e->setAttribute('default', strval($this->getDefault()));
160
        }
161
162
        if ($this->getFixed() !== null) {
163
            $e->setAttribute('fixed', strval($this->getFixed()));
164
        }
165
166
        if ($this->getFormChoice() !== null) {
167
            $e->setAttribute('form', strval($this->getFormChoice()));
168
        }
169
170
        $this->getSimpleType()?->toXML($e);
171
172
        return $e;
173
    }
174
}
175