Conditions | 11 |
Paths | 48 |
Total Lines | 74 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
120 | protected function setElements(array $elements): void |
||
121 | { |
||
122 | Assert::maxCount($elements, C::UNBOUNDED_LIMIT); |
||
123 | Assert::allIsInstanceOf($elements, SerializableElementInterface::class); |
||
124 | |||
125 | $namespace = $this->getElementNamespace(); |
||
126 | // Validate namespace value |
||
127 | if (!is_array($namespace)) { |
||
128 | // Must be one of the predefined values |
||
129 | Assert::oneOf($namespace, NamespaceEnum::cases()); |
||
130 | } else { |
||
131 | // Array must be non-empty and cannot contain ##any or ##other |
||
132 | Assert::notEmpty($namespace); |
||
133 | Assert::allNotSame($namespace, NamespaceEnum::Any); |
||
134 | Assert::allNotSame($namespace, NamespaceEnum::Other); |
||
135 | } |
||
136 | |||
137 | // Get namespaces for all elements |
||
138 | /** @var array<\SimpleSAML\XML\AbstractElement|\SimpleSAML\XML\Chunk> $elements */ |
||
139 | $actual_namespaces = array_map( |
||
140 | /** |
||
141 | * @return string|null |
||
142 | */ |
||
143 | function (AbstractElement|Chunk $elt): ?string { |
||
144 | return ($elt instanceof Chunk) ? $elt->getNamespaceURI() : $elt::getNamespaceURI(); |
||
145 | }, |
||
146 | $elements, |
||
147 | ); |
||
148 | |||
149 | if ($namespace === NamespaceEnum::Local) { |
||
150 | // If ##local then all namespaces must be null |
||
151 | Assert::allNull($actual_namespaces); |
||
152 | } elseif (is_array($namespace)) { |
||
153 | // Make a local copy of the property that we can edit |
||
154 | $allowed_namespaces = $namespace; |
||
155 | |||
156 | // Replace the ##targetedNamespace with the actual namespace |
||
157 | if (($key = array_search(NamespaceEnum::TargetNamespace, $allowed_namespaces)) !== false) { |
||
158 | $allowed_namespaces[$key] = self::NS; |
||
159 | } |
||
160 | |||
161 | // Replace the ##local with null |
||
162 | if (($key = array_search(NamespaceEnum::Local, $allowed_namespaces)) !== false) { |
||
163 | $allowed_namespaces[$key] = null; |
||
164 | } |
||
165 | |||
166 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
167 | Assert::isEmpty( |
||
168 | $diff, |
||
169 | sprintf( |
||
170 | 'Elements from namespaces [ %s ] are not allowed inside a %s element.', |
||
171 | rtrim(implode(', ', $diff)), |
||
172 | self::NS, |
||
173 | ), |
||
174 | ); |
||
175 | } elseif ($namespace === NamespaceEnum::Other) { |
||
176 | // Must be any namespace other than the parent element, excluding elements with no namespace |
||
177 | Assert::notInArray(null, $actual_namespaces); |
||
178 | Assert::allNotSame($actual_namespaces, self::NS); |
||
179 | } elseif ($namespace === NamespaceEnum::TargetNamespace) { |
||
180 | // Must be the same namespace as the one of the parent element |
||
181 | Assert::allSame($actual_namespaces, self::NS); |
||
182 | } else { |
||
183 | // XS_ANY_NS_ANY |
||
184 | } |
||
185 | |||
186 | $exclusionList = self::getElementExclusions(); |
||
187 | foreach ($elements as $i => $elt) { |
||
188 | if (in_array([$elt->getNamespaceURI(), $elt->getLocalName()], $exclusionList, true)) { |
||
189 | unset($elements[$i]); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $this->elements = $elements; |
||
194 | } |
||
241 |