Conditions | 10 |
Paths | 48 |
Total Lines | 80 |
Code Lines | 42 |
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 |
||
173 | protected function setAttributesNS(array $attributes): void |
||
174 | { |
||
175 | Assert::maxCount($attributes, C::UNBOUNDED_LIMIT); |
||
176 | Assert::allIsInstanceOf( |
||
177 | $attributes, |
||
178 | Attribute::class, |
||
179 | 'Arbitrary XML attributes can only be an instance of Attribute.', |
||
180 | ); |
||
181 | $namespace = $this->getAttributeNamespace(); |
||
182 | |||
183 | // Validate namespace value |
||
184 | if (!is_array($namespace)) { |
||
185 | // Must be one of the predefined values |
||
186 | Assert::oneOf($namespace, NamespaceEnum::cases()); |
||
187 | } else { |
||
188 | // Array must be non-empty and cannot contain ##any or ##other |
||
189 | Assert::notEmpty($namespace); |
||
190 | Assert::allNotSame($namespace, NamespaceEnum::Any); |
||
191 | Assert::allNotSame($namespace, NamespaceEnum::Other); |
||
192 | } |
||
193 | |||
194 | // Get namespaces for all attributes |
||
195 | $actual_namespaces = array_map( |
||
196 | /** |
||
197 | * @param \SimpleSAML\XML\Attribute $elt |
||
198 | * @return string|null |
||
199 | */ |
||
200 | function (Attribute $attr) { |
||
201 | return $attr->getNamespaceURI(); |
||
202 | }, |
||
203 | $attributes, |
||
204 | ); |
||
205 | |||
206 | if ($namespace === NamespaceEnum::Local) { |
||
207 | // If ##local then all namespaces must be null |
||
208 | Assert::allNull($actual_namespaces); |
||
209 | } elseif (is_array($namespace)) { |
||
210 | // Make a local copy of the property that we can edit |
||
211 | $allowed_namespaces = $namespace; |
||
212 | |||
213 | // Replace the ##targetedNamespace with the actual namespace |
||
214 | if (($key = array_search(NamespaceEnum::TargetNamespace, $allowed_namespaces)) !== false) { |
||
215 | $allowed_namespaces[$key] = self::NS; |
||
216 | } |
||
217 | |||
218 | // Replace the ##local with null |
||
219 | if (($key = array_search(NamespaceEnum::Local, $allowed_namespaces)) !== false) { |
||
220 | $allowed_namespaces[$key] = null; |
||
221 | } |
||
222 | |||
223 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
224 | Assert::isEmpty( |
||
225 | $diff, |
||
226 | sprintf( |
||
227 | 'Attributes from namespaces [ %s ] are not allowed inside a %s element.', |
||
228 | rtrim(implode(', ', $diff)), |
||
229 | self::NS, |
||
230 | ), |
||
231 | ); |
||
232 | } else { |
||
233 | if ($namespace === NamespaceEnum::Other) { |
||
234 | // All attributes must be namespaced, ergo non-null |
||
235 | Assert::allNotNull($actual_namespaces); |
||
236 | |||
237 | // Must be any namespace other than the parent element |
||
238 | Assert::allNotSame($actual_namespaces, self::NS); |
||
239 | } elseif ($namespace === NamespaceEnum::TargetNamespace) { |
||
240 | // Must be the same namespace as the one of the parent element |
||
241 | Assert::allSame($actual_namespaces, self::NS); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | $exclusionList = self::getAttributeExclusions(); |
||
246 | foreach ($attributes as $i => $attr) { |
||
247 | if (in_array([$attr->getNamespaceURI(), $attr->getAttrName()], $exclusionList, true)) { |
||
248 | unset($attributes[$i]); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | $this->namespacedAttributes = $attributes; |
||
253 | } |
||
290 |