| Conditions | 18 |
| Paths | 70 |
| Total Lines | 43 |
| Code Lines | 33 |
| 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 declare(strict_types=1); |
||
| 73 | private function addChildElement($parentNode, $key, $value, $allowAttribute=true) { |
||
| 74 | if (\is_bool($value)) { |
||
| 75 | if ($this->boolAsInt) { |
||
| 76 | $value = $value ? '1' : '0'; |
||
| 77 | } else { |
||
| 78 | $value = $value ? 'true' : 'false'; |
||
| 79 | } |
||
| 80 | } elseif (\is_numeric($value)) { |
||
| 81 | $value = (string)$value; |
||
| 82 | } elseif ($value === null && $this->nullAsEmpty) { |
||
| 83 | $value = ''; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (\is_string($value)) { |
||
| 87 | if ($key == $this->textNodeKey) { |
||
| 88 | $parentNode->appendChild($this->doc->createTextNode($value)); |
||
| 89 | } elseif ($allowAttribute && $this->keyMayDefineAttribute($key)) { |
||
| 90 | $parentNode->setAttribute($key, $value); |
||
| 91 | } else { |
||
| 92 | $child = $this->doc->createElement($key); |
||
| 93 | $child->appendChild($this->doc->createTextNode($value)); |
||
| 94 | $parentNode->appendChild($child); |
||
| 95 | } |
||
| 96 | } elseif (\is_array($value)) { |
||
| 97 | if (self::arrayIsIndexed($value)) { |
||
| 98 | foreach ($value as $child) { |
||
| 99 | $this->addChildElement($parentNode, $key, $child, /*allowAttribute=*/false); |
||
| 100 | } |
||
| 101 | } else { // associative array |
||
| 102 | $element = $this->doc->createElement($key); |
||
| 103 | $parentNode->appendChild($element); |
||
| 104 | foreach ($value as $childKey => $childValue) { |
||
| 105 | $this->addChildElement($element, $childKey, $childValue); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } elseif ($value instanceof \stdClass) { |
||
| 109 | // empty element |
||
| 110 | $element = $this->doc->createElement($key); |
||
| 111 | $parentNode->appendChild($element); |
||
| 112 | } elseif ($value === null) { |
||
| 113 | // skip |
||
| 114 | } else { |
||
| 115 | throw new \Exception("Unexpected value type for key $key"); |
||
| 116 | } |
||
| 137 |