Conditions | 8 |
Paths | 16 |
Total Lines | 74 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
159 | protected function setAttributesNS(array $attributes): void |
||
160 | { |
||
161 | Assert::maxCount($attributes, C::UNBOUNDED_LIMIT); |
||
162 | Assert::allIsInstanceOf( |
||
163 | $attributes, |
||
164 | Attribute::class, |
||
165 | 'Arbitrary XML attributes can only be an instance of Attribute.', |
||
166 | ); |
||
167 | $namespace = $this->getAttributeNamespace(); |
||
168 | |||
169 | // Validate namespace value |
||
170 | if (!is_array($namespace)) { |
||
171 | // Must be one of the predefined values |
||
172 | Assert::oneOf($namespace, C::XS_ANY_NS); |
||
173 | } else { |
||
174 | // Array must be non-empty and cannot contain ##any or ##other |
||
175 | Assert::notEmpty($namespace); |
||
176 | Assert::allStringNotEmpty($namespace); |
||
177 | Assert::allNotSame($namespace, C::XS_ANY_NS_ANY); |
||
178 | Assert::allNotSame($namespace, C::XS_ANY_NS_OTHER); |
||
179 | } |
||
180 | |||
181 | // Get namespaces for all attributes |
||
182 | $actual_namespaces = array_map( |
||
183 | /** |
||
184 | * @param \SimpleSAML\XML\Attribute $elt |
||
185 | * @return string|null |
||
186 | */ |
||
187 | function (Attribute $attr) { |
||
188 | return $attr->getNamespaceURI(); |
||
189 | }, |
||
190 | $attributes, |
||
191 | ); |
||
192 | |||
193 | if ($namespace === C::XS_ANY_NS_LOCAL) { |
||
194 | // If ##local then all namespaces must be null |
||
195 | Assert::allNull($actual_namespaces); |
||
196 | } elseif (is_array($namespace)) { |
||
197 | // Make a local copy of the property that we can edit |
||
198 | $allowed_namespaces = $namespace; |
||
199 | |||
200 | // Replace the ##targetedNamespace with the actual namespace |
||
201 | if (($key = array_search(C::XS_ANY_NS_TARGET, $allowed_namespaces)) !== false) { |
||
202 | $allowed_namespaces[$key] = static::NS; |
||
1 ignored issue
–
show
|
|||
203 | } |
||
204 | |||
205 | // Replace the ##local with null |
||
206 | if (($key = array_search(C::XS_ANY_NS_LOCAL, $allowed_namespaces)) !== false) { |
||
207 | $allowed_namespaces[$key] = null; |
||
208 | } |
||
209 | |||
210 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
211 | Assert::isEmpty( |
||
212 | $diff, |
||
213 | sprintf( |
||
214 | 'Attributes from namespaces [ %s ] are not allowed inside a %s element.', |
||
215 | rtrim(implode(', ', $diff)), |
||
216 | static::NS, |
||
217 | ), |
||
218 | ); |
||
219 | } else { |
||
220 | // All attributes must be namespaced, ergo non-null |
||
221 | Assert::allNotNull($actual_namespaces); |
||
222 | |||
223 | if ($namespace === C::XS_ANY_NS_OTHER) { |
||
224 | // Must be any namespace other than the parent element |
||
225 | Assert::allNotSame($actual_namespaces, static::NS); |
||
226 | } elseif ($namespace === C::XS_ANY_NS_TARGET) { |
||
227 | // Must be the same namespace as the one of the parent element |
||
228 | Assert::allSame($actual_namespaces, static::NS); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | $this->namespacedAttributes = $attributes; |
||
233 | } |
||
252 |