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