Passed
Pull Request — master (#317)
by Tim
12:30
created

AbstractRoleDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 10
dl 0
loc 31
rs 9.7998
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\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
12
use function implode;
13
14
/**
15
 * Class representing SAML 2 RoleDescriptor element.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
abstract class AbstractRoleDescriptor extends AbstractMetadataDocument
20
{
21
    /**
22
     * Initialize a RoleDescriptor.
23
     *
24
     * @param string[] $protocolSupportEnumeration A set of URI specifying the protocols supported.
25
     * @param string|null $ID The ID for this document. Defaults to null.
26
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
27
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
28
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
29
     * @param string|null $errorURL An URI where to redirect users for support. Defaults to null.
30
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
31
     *   An array of KeyDescriptor elements. Defaults to an empty array.
32
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
33
     *   The organization running this entity. Defaults to null.
34
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contactPerson
35
     *   An array of contacts for this entity. Defaults to an empty array.
36
     * @param \DOMAttr[] $namespacedAttributes
37
     */
38
    public function __construct(
39
        protected array $protocolSupportEnumeration,
40
        ?string $ID = null,
41
        ?int $validUntil = null,
42
        ?string $cacheDuration = null,
43
        ?Extensions $extensions = null,
44
        protected ?string $errorURL = null,
45
        protected array $keyDescriptor = [],
46
        protected ?Organization $organization = null,
47
        protected array $contactPerson = [],
48
        array $namespacedAttributes = [],
49
    ) {
50
        Assert::nullOrValidURI($errorURL, SchemaViolationException::class); // Covers the empty string
51
        Assert::minCount(
52
            $protocolSupportEnumeration,
53
            1,
54
            'At least one protocol must be supported by this md:' . static::getLocalName() . '.',
55
        );
56
        Assert::allValidURI($protocolSupportEnumeration, SchemaViolationException::class);
57
        Assert::allIsInstanceOf(
58
            $contactPerson,
59
            ContactPerson::class,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\ContactPerson was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
            'All contacts must be an instance of md:ContactPerson',
61
        );
62
        Assert::allIsInstanceOf(
63
            $keyDescriptor,
64
            KeyDescriptor::class,
65
            'All key descriptors must be an instance of md:KeyDescriptor',
66
        );
67
68
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
69
    }
70
71
72
    /**
73
     * Collect the value of the errorURL property.
74
     *
75
     * @return string|null
76
     */
77
    public function getErrorURL()
78
    {
79
        return $this->errorURL;
80
    }
81
82
83
    /**
84
     * Collect the value of the protocolSupportEnumeration property.
85
     *
86
     * @return string[]
87
     */
88
    public function getProtocolSupportEnumeration()
89
    {
90
        return $this->protocolSupportEnumeration;
91
    }
92
93
94
    /**
95
     * Collect the value of the Organization property.
96
     *
97
     * @return \SimpleSAML\SAML2\XML\md\Organization|null
98
     */
99
    public function getOrganization()
100
    {
101
        return $this->organization;
102
    }
103
104
105
    /**
106
     * Collect the value of the ContactPersons property.
107
     *
108
     * @return \SimpleSAML\SAML2\XML\md\ContactPerson[]
109
     */
110
    public function getContactPerson()
111
    {
112
        return $this->contactPerson;
113
    }
114
115
116
    /**
117
     * Collect the value of the KeyDescriptors property.
118
     *
119
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
120
     */
121
    public function getKeyDescriptor()
122
    {
123
        return $this->keyDescriptor;
124
    }
125
126
127
    /**
128
     * Add this RoleDescriptor to an EntityDescriptor.
129
     *
130
     * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to.
131
     * @return \DOMElement
132
     */
133
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
134
    {
135
        $e = parent::toUnsignedXML($parent);
136
137
        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));
138
139
        if ($this->getErrorURL() !== null) {
140
            $e->setAttribute('errorURL', $this->getErrorURL());
141
        }
142
143
        foreach ($this->getKeyDescriptor() as $kd) {
144
            $kd->toXML($e);
145
        }
146
147
        $this->getOrganization()?->toXML($e);
148
149
        foreach ($this->getContactPerson() as $cp) {
150
            $cp->toXML($e);
151
        }
152
153
        return $e;
154
    }
155
}
156