| Conditions | 9 |
| Paths | 16 |
| Total Lines | 69 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 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 |
||
| 39 | protected function setElements(array $elements): void |
||
| 40 | { |
||
| 41 | Assert::allIsInstanceOf($elements, ElementInterface::class); |
||
| 42 | $namespace = $this->getElementNamespace(); |
||
| 43 | |||
| 44 | // Validate namespace value |
||
| 45 | if (!is_array($namespace)) { |
||
| 46 | // Must be one of the predefined values |
||
| 47 | Assert::oneOf($namespace, C::XS_ANY_NS); |
||
| 48 | } else { |
||
| 49 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 50 | Assert::notEmpty($namespace); |
||
| 51 | Assert::allNotSame($namespace, C::XS_ANY_NS_ANY); |
||
| 52 | Assert::allNotSame($namespace, C::XS_ANY_NS_OTHER); |
||
| 53 | } |
||
| 54 | |||
| 55 | // Get namespaces for all elements |
||
| 56 | $actual_namespaces = array_map( |
||
| 57 | /** |
||
| 58 | * @param \SimpleSAML\XML\ElementInterface $elt |
||
| 59 | * @return string|null |
||
| 60 | */ |
||
| 61 | function (ElementInterface $elt) { |
||
| 62 | /** @psalm-var \SimpleSAML\XML\Chunk|\SimpleSAML\XML\AbstractElement $elt */ |
||
| 63 | return ($elt instanceof Chunk) ? $elt->getNamespaceURI() : $elt::getNamespaceURI(); |
||
|
|
|||
| 64 | }, |
||
| 65 | $elements |
||
| 66 | ); |
||
| 67 | |||
| 68 | if ($namespace === C::XS_ANY_NS_LOCAL) { |
||
| 69 | // If ##local then all namespaces must be null |
||
| 70 | Assert::allNull($actual_namespaces); |
||
| 71 | } elseif (is_array($namespace)) { |
||
| 72 | // Make a local copy of the property that we can edit |
||
| 73 | $allowed_namespaces = $namespace; |
||
| 74 | |||
| 75 | // Replace the ##targetedNamespace with the actual namespace |
||
| 76 | if (($key = array_search(C::XS_ANY_NS_TARGET, $allowed_namespaces)) !== false) { |
||
| 77 | $allowed_namespaces[$key] = static::NS; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Replace the ##local with null |
||
| 81 | if (($key = array_search(C::XS_ANY_NS_LOCAL, $allowed_namespaces)) !== false) { |
||
| 82 | $allowed_namespaces[$key] = null; |
||
| 83 | } |
||
| 84 | |||
| 85 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
| 86 | Assert::isEmpty( |
||
| 87 | $diff, |
||
| 88 | sprintf( |
||
| 89 | 'Elements from namespaces [ %s ] are not allowed inside a %s element.', |
||
| 90 | rtrim(implode(', ', $diff)), |
||
| 91 | static::NS, |
||
| 92 | ), |
||
| 93 | ); |
||
| 94 | } else { |
||
| 95 | // All elements must be namespaced, ergo non-null |
||
| 96 | Assert::allNotNull($actual_namespaces); |
||
| 97 | |||
| 98 | if ($namespace === C::XS_ANY_NS_OTHER) { |
||
| 99 | // Must be any namespace other than the parent element |
||
| 100 | Assert::allNotSame($actual_namespaces, static::NS); |
||
| 101 | } elseif ($namespace === C::XS_ANY_NS_TARGET) { |
||
| 102 | // Must be the same namespace as the one of the parent element |
||
| 103 | Assert::allSame($actual_namespaces, static::NS); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->elements = $elements; |
||
| 108 | } |
||
| 137 |