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

AbstractAttribute::__construct()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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