| Conditions | 14 |
| Paths | 20 |
| Total Lines | 65 |
| Code Lines | 35 |
| 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 |
||
| 117 | private function &convert($node_name, $arr = array()) |
||
| 118 | { |
||
| 119 | |||
| 120 | //print_arr($node_name); |
||
| 121 | $xml = $this->getXMLRoot(); |
||
| 122 | $node = $xml->createElement($node_name); |
||
| 123 | |||
| 124 | if (is_array($arr)) { |
||
|
|
|||
| 125 | // get the attributes first.; |
||
| 126 | if (isset($arr['@attributes'])) { |
||
| 127 | foreach ($arr['@attributes'] as $key => $value) { |
||
| 128 | if (!$this->isValidTagName($key)) { |
||
| 129 | throw new Exception( |
||
| 130 | '[Array2XML] Illegal character in attribute name. attribute: '. |
||
| 131 | $key.' in node: '.$node_name |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | $node->setAttribute($key, $this->bool2str($value)); |
||
| 135 | } |
||
| 136 | unset($arr['@attributes']); //remove the key from the array once done. |
||
| 137 | } |
||
| 138 | // check if it has a value stored in @value, if yes store the value and return |
||
| 139 | // else check if its directly stored as string |
||
| 140 | if (isset($arr['@value'])) { |
||
| 141 | $node->appendChild($xml->createTextNode($this->bool2str($arr['@value']))); |
||
| 142 | unset($arr['@value']); //remove the key from the array once done. |
||
| 143 | //return from recursion, as a note with value cannot have child nodes. |
||
| 144 | return $node; |
||
| 145 | } elseif (isset($arr['@cdata'])) { |
||
| 146 | $node->appendChild($xml->createCDATASection($this->bool2str($arr['@cdata']))); |
||
| 147 | unset($arr['@cdata']); //remove the key from the array once done. |
||
| 148 | //return from recursion, as a note with cdata cannot have child nodes. |
||
| 149 | return $node; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | //create subnodes using recursion |
||
| 153 | if (is_array($arr)) { |
||
| 154 | // recurse to get the node for that key |
||
| 155 | foreach ($arr as $key => $value) { |
||
| 156 | if (!$this->isValidTagName($key)) { |
||
| 157 | throw new Exception( |
||
| 158 | '[Array2XML] Illegal character in tag name. tag: '. |
||
| 159 | $key.' in node: '.$node_name |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | if (is_array($value) && is_numeric(key($value))) { |
||
| 163 | // MORE THAN ONE NODE OF ITS KIND; |
||
| 164 | // if the new array is numeric index, means it is array of nodes of the same kind |
||
| 165 | // it should follow the parent key name |
||
| 166 | foreach ($value as $k => $v) { |
||
| 167 | $node->appendChild($this->convert($key, $v)); |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | // ONLY ONE NODE OF ITS KIND |
||
| 171 | $node->appendChild($this->convert($key, $value)); |
||
| 172 | } |
||
| 173 | unset($arr[$key]); //remove the key from the array once done. |
||
| 174 | } |
||
| 175 | } |
||
| 176 | // after we are done with all the keys in the array (if it is one) |
||
| 177 | // we check if it has any text value, if yes, append it. |
||
| 178 | if (!is_array($arr)) { |
||
| 179 | $node->appendChild($xml->createTextNode($this->bool2str($arr))); |
||
| 180 | } |
||
| 181 | return $node; |
||
| 182 | } |
||
| 216 |