| Conditions | 11 |
| Paths | 48 |
| Total Lines | 74 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 114 | protected function setElements(array $elements): void |
||
| 115 | { |
||
| 116 | Assert::maxCount($elements, C::UNBOUNDED_LIMIT); |
||
| 117 | Assert::allIsInstanceOf($elements, SerializableElementInterface::class); |
||
| 118 | $namespace = $this->getElementNamespace(); |
||
| 119 | |||
| 120 | // Validate namespace value |
||
| 121 | if (!is_array($namespace)) { |
||
| 122 | // Must be one of the predefined values |
||
| 123 | Assert::oneOf($namespace, NS::cases()); |
||
| 124 | } else { |
||
| 125 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 126 | Assert::notEmpty($namespace); |
||
| 127 | Assert::allNotSame($namespace, NS::ANY); |
||
| 128 | Assert::allNotSame($namespace, NS::OTHER); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Get namespaces for all elements |
||
| 132 | $actual_namespaces = array_map( |
||
| 133 | /** |
||
| 134 | * @param \SimpleSAML\XML\SerializableElementInterface $elt |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | function (SerializableElementInterface $elt) { |
||
| 138 | return ($elt instanceof Chunk) ? $elt->getNamespaceURI() : $elt::getNamespaceURI(); |
||
| 139 | }, |
||
| 140 | $elements, |
||
| 141 | ); |
||
| 142 | |||
| 143 | if ($namespace === NS::LOCAL) { |
||
| 144 | // If ##local then all namespaces must be null |
||
| 145 | Assert::allNull($actual_namespaces); |
||
| 146 | } elseif (is_array($namespace)) { |
||
| 147 | // Make a local copy of the property that we can edit |
||
| 148 | $allowed_namespaces = $namespace; |
||
| 149 | |||
| 150 | // Replace the ##targetedNamespace with the actual namespace |
||
| 151 | if (($key = array_search(NS::TARGET, $allowed_namespaces)) !== false) { |
||
| 152 | $allowed_namespaces[$key] = static::NS; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Replace the ##local with null |
||
| 156 | if (($key = array_search(NS::LOCAL, $allowed_namespaces)) !== false) { |
||
| 157 | $allowed_namespaces[$key] = null; |
||
| 158 | } |
||
| 159 | |||
| 160 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
| 161 | Assert::isEmpty( |
||
| 162 | $diff, |
||
| 163 | sprintf( |
||
| 164 | 'Elements from namespaces [ %s ] are not allowed inside a %s element.', |
||
| 165 | rtrim(implode(', ', $diff)), |
||
| 166 | static::NS, |
||
| 167 | ), |
||
| 168 | ); |
||
| 169 | } elseif ($namespace === NS::OTHER) { |
||
| 170 | // Must be any namespace other than the parent element, excluding elements with no namespace |
||
| 171 | Assert::notInArray(null, $actual_namespaces); |
||
| 172 | Assert::allNotSame($actual_namespaces, static::NS); |
||
| 173 | } elseif ($namespace === NS::TARGET) { |
||
| 174 | // Must be the same namespace as the one of the parent element |
||
| 175 | Assert::allSame($actual_namespaces, static::NS); |
||
| 176 | } else { |
||
| 177 | // XS_ANY_NS_ANY |
||
| 178 | } |
||
| 179 | |||
| 180 | $exclusionList = static::getElementExclusions(); |
||
| 181 | foreach ($elements as $i => $elt) { |
||
| 182 | if (in_array([$elt->getNamespaceURI(), $elt->getLocalName()], $exclusionList, true)) { |
||
| 183 | unset($elements[$i]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->elements = $elements; |
||
| 188 | } |
||
| 232 |