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