AbstractRestrictionType::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 9
dl 0
loc 29
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\SchemaViolationException;
10
use SimpleSAML\XMLSchema\Type\IDValue;
11
use SimpleSAML\XMLSchema\Type\QNameValue;
12
use SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface;
13
use SimpleSAML\XMLSchema\XML\Trait\AttrDeclsTrait;
14
use SimpleSAML\XMLSchema\XML\Trait\SimpleRestrictionModelTrait;
15
use SimpleSAML\XMLSchema\XML\Trait\TypeDefParticleTrait;
16
17
use function strval;
18
19
/**
20
 * Abstract class representing the restrictionType-type.
21
 *
22
 * @package simplesamlphp/xml-common
23
 */
24
abstract class AbstractRestrictionType extends AbstractAnnotated
25
{
26
    use AttrDeclsTrait;
27
    use SimpleRestrictionModelTrait;
28
    use TypeDefParticleTrait;
29
30
31
    /**
32
     * AbstractRestrictionType constructor
33
     *
34
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $base
35
     * @param \SimpleSAML\XMLSchema\XML\Interface\TypeDefParticleInterface|null $particle
36
     * @param \SimpleSAML\XMLSchema\XML\LocalSimpleType|null $localSimpleType
37
     * @param array<\SimpleSAML\XMLSchema\XML\Interface\FacetInterface> $facets
38
     * @param (
39
     *     \SimpleSAML\XMLSchema\XML\LocalAttribute|
40
     *     \SimpleSAML\XMLSchema\XML\ReferencedAttributeGroup
41
     * )[] $attributes
42
     * @param \SimpleSAML\XMLSchema\XML\AnyAttribute|null $anyAttribute
43
     * @param \SimpleSAML\XMLSchema\XML\Annotation|null $annotation
44
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id
45
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
46
     */
47
    public function __construct(
48
        protected QNameValue $base,
49
        // xs:typeDefParticle
50
        ?TypeDefParticleInterface $particle = null,
51
        // xs:simpleRestrictionModel
52
        protected ?LocalSimpleType $localSimpleType = null,
53
        array $facets = [],
54
        // xs:attrDecls
55
        array $attributes = [],
56
        ?AnyAttribute $anyAttribute = null,
57
        // parent defined
58
        ?Annotation $annotation = null,
59
        ?IDValue $id = null,
60
        array $namespacedAttributes = [],
61
    ) {
62
        // The xs:typeDefParticle and xs:simpleRestrictionModel groups are mutually exclusive
63
        if ($particle !== null) {
64
            Assert::null($localSimpleType, SchemaViolationException::class);
65
            Assert::isEmpty($facets, SchemaViolationException::class);
66
        } elseif ($localSimpleType !== null || $facets !== []) {
67
            $this->setSimpleType($localSimpleType);
68
            $this->setFacets($facets);
69
        }
70
71
        parent::__construct($annotation, $id, $namespacedAttributes);
72
73
        $this->setAttributes($attributes);
74
        $this->setAnyAttribute($anyAttribute);
75
        $this->setParticle($particle);
76
    }
77
78
79
    /**
80
     * Collect the value of the localSimpleType-property
81
     *
82
     * @return \SimpleSAML\XMLSchema\XML\LocalSimpleType|null
83
     */
84
    public function getLocalSimpleType(): ?LocalSimpleType
85
    {
86
        return $this->localSimpleType;
87
    }
88
89
90
    /**
91
     * Collect the value of the base-property
92
     *
93
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
94
     */
95
    public function getBase(): ?QNameValue
96
    {
97
        return $this->base;
98
    }
99
100
101
    /**
102
     * Add this RestrictionType to an XML element.
103
     *
104
     * @param \DOMElement|null $parent The element we should append this RestrictionType to.
105
     * @return \DOMElement
106
     */
107
    public function toXML(?DOMElement $parent = null): DOMElement
108
    {
109
        $e = parent::toXML($parent);
110
111
        if ($this->getParticle() !== null) {
112
            $this->getParticle()->toXML($e);
113
        } elseif ($this->getLocalSimpleType() !== null || $this->getFacets() !== []) {
114
            $this->getLocalSimpleType()?->toXML($e);
115
116
            foreach ($this->getFacets() as $facet) {
117
                $facet->toXML($e);
118
            }
119
        }
120
121
        foreach ($this->getAttributes() as $attr) {
122
            $attr->toXML($e);
123
        }
124
125
        $this->getAnyAttribute()?->toXML($e);
126
127
        if ($this->getBase() !== null) {
128
            $e->setAttribute('base', strval($this->getBase()));
129
        }
130
131
        return $e;
132
    }
133
}
134