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