| Conditions | 16 |
| Paths | 12 |
| Total Lines | 64 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 67 | private static function convert($node) |
||
| 68 | { |
||
| 69 | $output = []; |
||
| 70 | |||
| 71 | switch ($node->nodeType) { |
||
| 72 | case XML_CDATA_SECTION_NODE: |
||
| 73 | $output[self::$prefixAttributes . 'cdata'] = trim($node->textContent); |
||
| 74 | break; |
||
| 75 | |||
| 76 | case XML_TEXT_NODE: |
||
| 77 | $output = trim($node->textContent); |
||
| 78 | break; |
||
| 79 | |||
| 80 | case XML_ELEMENT_NODE: |
||
| 81 | // for each child node, call the covert function recursively |
||
| 82 | for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { |
||
| 83 | $child = $node->childNodes->item($i); |
||
| 84 | $v = self::convert($child); |
||
| 85 | if (isset($child->tagName)) { |
||
| 86 | $t = $child->tagName; |
||
| 87 | |||
| 88 | // assume more nodes of same kind are coming |
||
| 89 | if (!array_key_exists($t, $output)) { |
||
| 90 | $output[$t] = []; |
||
| 91 | } |
||
| 92 | $output[$t][] = $v; |
||
| 93 | } else { |
||
| 94 | //check if it is not an empty text node |
||
| 95 | if ($v !== '') { |
||
| 96 | $output = $v; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if (is_array($output)) { |
||
| 102 | // if only one node of its kind, assign it directly instead if array($value); |
||
| 103 | foreach ($output as $t => $v) { |
||
| 104 | if (is_array($v) && count($v) === 1) { |
||
| 105 | $output[$t] = $v[0]; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | if (count($output) === 0) { |
||
| 109 | //for empty nodes |
||
| 110 | $output = ''; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | // loop through the attributes and collect them |
||
| 115 | if ($node->attributes->length) { |
||
| 116 | $a = []; |
||
| 117 | foreach ($node->attributes as $attrName => $attrNode) { |
||
| 118 | $a[$attrName] = (string)$attrNode->value; |
||
| 119 | } |
||
| 120 | // if its an leaf node, store the value in @value instead of directly storing it. |
||
| 121 | if (!is_array($output)) { |
||
| 122 | $output = [self::$prefixAttributes . 'value' => $output]; |
||
| 123 | } |
||
| 124 | $output[self::$prefixAttributes . 'attributes'] = $a; |
||
| 125 | } |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | |||
| 129 | return $output; |
||
| 130 | } |
||
| 131 | } |