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