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