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); |
||
90 | private function addChildElement(\DOMElement $parentElem, string $key, /*mixed*/ $value, bool $allowAttribute=true) : void { |
||
91 | if (\is_bool($value)) { |
||
92 | if ($this->boolAsInt) { |
||
93 | $value = $value ? '1' : '0'; |
||
94 | } else { |
||
95 | $value = $value ? 'true' : 'false'; |
||
96 | } |
||
97 | } elseif (\is_numeric($value)) { |
||
98 | $value = (string)$value; |
||
99 | } elseif ($value === null && $this->nullAsEmpty) { |
||
100 | $value = ''; |
||
101 | } |
||
102 | |||
103 | if (\is_string($value)) { |
||
104 | if ($key == $this->textNodeKey) { |
||
105 | $parentElem->appendChild($this->doc->createTextNode($value)); |
||
106 | } elseif ($allowAttribute && $this->keyMayDefineAttribute($key)) { |
||
107 | $parentElem->setAttribute($key, $value); |
||
108 | } else { |
||
109 | $child = $this->doc->createElement($key); |
||
110 | $child->appendChild($this->doc->createTextNode($value)); |
||
111 | $parentElem->appendChild($child); |
||
112 | } |
||
113 | } elseif (\is_array($value)) { |
||
114 | if (self::arrayIsIndexed($value)) { |
||
115 | foreach ($value as $child) { |
||
116 | $this->addChildElement($parentElem, $key, $child, /*allowAttribute=*/false); |
||
117 | } |
||
118 | } else { // associative array |
||
119 | $element = $this->doc->createElement($key); |
||
120 | $parentElem->appendChild($element); |
||
121 | foreach ($value as $childKey => $childValue) { |
||
122 | $this->addChildElement($element, (string)$childKey, $childValue); |
||
123 | } |
||
124 | } |
||
125 | } elseif ($value instanceof \stdClass) { |
||
126 | // empty element |
||
127 | $element = $this->doc->createElement($key); |
||
128 | $parentElem->appendChild($element); |
||
129 | } elseif ($value === null) { |
||
|
|||
130 | // skip |
||
131 | } else { |
||
132 | throw new \Exception("Unexpected value type for key $key"); |
||
133 | } |
||
153 |