| Conditions | 16 |
| Paths | 12 |
| Total Lines | 57 |
| 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 |
||
| 69 | private static function convert(DOMNode $node) |
||
| 70 | { |
||
| 71 | $output = []; |
||
| 72 | switch ($node->nodeType) { |
||
| 73 | case XML_CDATA_SECTION_NODE: |
||
| 74 | $output['@cdata'] = trim($node->textContent); |
||
| 75 | break; |
||
| 76 | case XML_TEXT_NODE: |
||
| 77 | $output = trim($node->textContent); |
||
| 78 | break; |
||
| 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 | // assume more nodes of same kind are coming |
||
| 87 | if (!array_key_exists($t, $output)) { |
||
| 88 | $output[$t] = []; |
||
| 89 | } |
||
| 90 | $output[$t][] = $v; |
||
| 91 | } else { |
||
| 92 | //check if it is not an empty node |
||
| 93 | if (!empty($v)) { |
||
| 94 | $output = $v; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | if (is_array($output)) { |
||
| 99 | // if only one node of its kind, assign it directly instead if array($value); |
||
| 100 | foreach ($output as $t => $v) { |
||
| 101 | if (is_array($v) && count($v) == 1) { |
||
| 102 | $output[$t] = $v[0]; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | if (empty($output)) { |
||
| 106 | //for empty nodes |
||
| 107 | $output = ''; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | // loop through the attributes and collect them |
||
| 111 | if ($node->attributes->length) { |
||
| 112 | $a = []; |
||
| 113 | foreach ($node->attributes as $attrName => $attrNode) { |
||
| 114 | $a[$attrName] = $attrNode->value; |
||
| 115 | } |
||
| 116 | // if its an leaf node, store the value in @value instead of directly storing it. |
||
| 117 | if (!is_array($output)) { |
||
| 118 | $output = ['@value' => $output]; |
||
| 119 | } |
||
| 120 | $output['@attributes'] = $a; |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | return $output; |
||
| 125 | } |
||
| 126 | /** |
||
| 139 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.