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