| Conditions | 9 |
| Paths | 16 |
| Total Lines | 65 |
| Code Lines | 35 |
| 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 |
||
| 28 | protected function setElements(array $elements): void |
||
| 29 | { |
||
| 30 | Assert::allIsInstanceOf($elements, XMLElementInterface::class); |
||
| 31 | $namespace = $this->getNamespace(); |
||
| 32 | |||
| 33 | // Validate namespace value |
||
| 34 | Assert::true(is_array($namespace) || is_string($namespace)); |
||
| 35 | if (!is_array($namespace)) { |
||
| 36 | // Must be one of the predefined values |
||
| 37 | Assert::oneOf($namespace, Constants::XS_ANY_NS); |
||
| 38 | } else { |
||
| 39 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 40 | Assert::notEmpty($namespace); |
||
| 41 | Assert::allNotSame($namespace, Constants::XS_ANY_NS_ANY); |
||
| 42 | Assert::allNotSame($namespace, Constants::XS_ANY_NS_OTHER); |
||
| 43 | } |
||
| 44 | |||
| 45 | // Get namespaces for all elements |
||
| 46 | $actual_namespaces = array_map( |
||
| 47 | function ($elt) { |
||
| 48 | return $elt->getNamespaceURI(); |
||
| 49 | }, |
||
| 50 | $elements |
||
| 51 | ); |
||
| 52 | |||
| 53 | if ($namespace === Constants::XS_ANY_NS_LOCAL) { |
||
| 54 | // If ##local then all namespaces must be null |
||
| 55 | Assert::allNull($actual_namespaces); |
||
| 56 | } elseif (is_array($namespace)) { |
||
| 57 | // Make a local copy of the property that we can edit |
||
| 58 | $allowed_namespaces = $namespace; |
||
| 59 | |||
| 60 | // Replace the ##targetedNamespace with the actual namespace |
||
| 61 | if (($key = array_search(Constants::XS_ANY_NS_TARGET, $allowed_namespaces)) !== false) { |
||
| 62 | $allowed_namespaces[$key] = static::NS; |
||
|
1 ignored issue
–
show
|
|||
| 63 | } |
||
| 64 | |||
| 65 | // Replace the ##local with null |
||
| 66 | if (($key = array_search(Constants::XS_ANY_NS_LOCAL, $allowed_namespaces)) !== false) { |
||
| 67 | $allowed_namespaces[$key] = null; |
||
| 68 | } |
||
| 69 | |||
| 70 | $diff = array_diff($actual_namespaces, $allowed_namespaces); |
||
| 71 | Assert::isEmpty( |
||
| 72 | $diff, |
||
| 73 | sprintf( |
||
| 74 | 'Elements from namespaces [ %s ] are not allowed inside a %s element.', |
||
| 75 | rtrim(implode(', ', $diff)), |
||
| 76 | static::NS |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | } else { |
||
| 80 | // All elements must be namespaced, ergo non-null |
||
| 81 | Assert::allNotNull($actual_namespaces); |
||
| 82 | |||
| 83 | if ($namespace === Constants::XS_ANY_NS_OTHER) { |
||
| 84 | // Must be any namespace other than the parent element |
||
| 85 | Assert::allNotSame($actual_namespaces, static::NS); |
||
| 86 | } elseif ($namespace === Constants::XS_ANY_NS_TARGET) { |
||
| 87 | // Must be the same namespace as the one of the parent element |
||
| 88 | Assert::allSame($actual_namespaces, static::NS); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->elements = $elements; |
||
| 93 | } |
||
| 120 |