Conditions | 16 |
Paths | 12 |
Total Lines | 65 |
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 |
||
93 | private static function convert(DOMNode $node) |
||
94 | { |
||
95 | $output = []; |
||
96 | |||
97 | switch ($node->nodeType) { |
||
98 | case XML_CDATA_SECTION_NODE: |
||
99 | $output['@cdata'] = trim($node->textContent); |
||
100 | break; |
||
101 | |||
102 | case XML_TEXT_NODE: |
||
103 | $output = trim($node->textContent); |
||
104 | break; |
||
105 | |||
106 | case XML_ELEMENT_NODE: |
||
107 | |||
108 | // for each child node, call the covert function recursively |
||
109 | for ($i = 0, $m = $node->childNodes->length; $i < $m; ++$i) { |
||
110 | $child = $node->childNodes->item($i); |
||
111 | $v = self::convert($child); |
||
112 | if (isset($child->tagName)) { |
||
113 | $t = $child->tagName; |
||
114 | |||
115 | // assume more nodes of same kind are coming |
||
116 | if (!array_key_exists($t, $output)) { |
||
117 | $output[$t] = []; |
||
118 | } |
||
119 | $output[$t][] = $v; |
||
120 | } else { |
||
121 | //check if it is not an empty node |
||
122 | if (!empty($v)) { |
||
123 | $output = $v; |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if (is_array($output)) { |
||
129 | // if only one node of its kind, assign it directly instead if array($value); |
||
130 | foreach ($output as $t => $v) { |
||
131 | if (is_array($v) && count($v) == 1) { |
||
132 | $output[$t] = $v[0]; |
||
133 | } |
||
134 | } |
||
135 | if (empty($output)) { |
||
136 | //for empty nodes |
||
137 | $output = ''; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // loop through the attributes and collect them |
||
142 | if ($node->attributes->length) { |
||
143 | $a = []; |
||
144 | foreach ($node->attributes as $attrName => $attrNode) { |
||
145 | $a[$attrName] = $attrNode->value; |
||
146 | } |
||
147 | // if its an leaf node, store the value in @value instead of directly storing it. |
||
148 | if (!is_array($output)) { |
||
149 | $output = ['@value' => $output]; |
||
150 | } |
||
151 | $output['@attributes'] = $a; |
||
152 | } |
||
153 | break; |
||
154 | } |
||
155 | |||
156 | return $output; |
||
157 | } |
||
158 | |||
173 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.