| Conditions | 16 |
| Paths | 12 |
| Total Lines | 63 |
| Code Lines | 36 |
| 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 |
||
| 91 | private static function &convert($node) |
||
| 92 | { |
||
| 93 | $output = []; |
||
| 94 | |||
| 95 | switch ($node->nodeType) { |
||
| 96 | case XML_CDATA_SECTION_NODE: |
||
| 97 | $output['@cdata'] = trim($node->textContent); |
||
| 98 | break; |
||
| 99 | |||
| 100 | case XML_TEXT_NODE: |
||
| 101 | $output = trim($node->textContent); |
||
| 102 | break; |
||
| 103 | |||
| 104 | case XML_ELEMENT_NODE: |
||
| 105 | // for each child node, call the covert function recursively |
||
| 106 | for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { |
||
| 107 | $child = $node->childNodes->item($i); |
||
| 108 | $v = self::convert($child); |
||
| 109 | if (isset($child->tagName)) { |
||
| 110 | $t = $child->tagName; |
||
| 111 | |||
| 112 | // assume more nodes of same kind are coming |
||
| 113 | if (!isset($output[$t])) { |
||
| 114 | $output[$t] = []; |
||
| 115 | } |
||
| 116 | $output[$t][] = $v; |
||
| 117 | } else { |
||
| 118 | //check if it is not an empty text node |
||
| 119 | if ($v !== '') { |
||
| 120 | $output = $v; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if (is_array($output)) { |
||
| 126 | // if only one node of its kind, assign it directly instead if array($value); |
||
| 127 | foreach ($output as $t => $v) { |
||
| 128 | if (is_array($v) && count($v) == 1) { |
||
| 129 | $output[$t] = $v[0]; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | if (empty($output)) { |
||
| 133 | //for empty nodes |
||
| 134 | $output = ''; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // loop through the attributes and collect them |
||
| 139 | if ($node->attributes->length) { |
||
| 140 | $a = []; |
||
| 141 | foreach ($node->attributes as $attrName => $attrNode) { |
||
| 142 | $a[$attrName] = (string)$attrNode->value; |
||
| 143 | } |
||
| 144 | // if its an leaf node, store the value in @value instead of directly storing it. |
||
| 145 | if (!is_array($output)) { |
||
| 146 | $output = ['@value' => $output]; |
||
| 147 | } |
||
| 148 | $output['@attributes'] = $a; |
||
| 149 | } |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $output; |
||
| 154 | } |
||
| 168 |