Conditions | 15 |
Paths | 20 |
Total Lines | 63 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
95 | private static function convert($node_name, $arr = []) |
||
96 | { |
||
97 | //print_arr($node_name); |
||
98 | $xml = self::getXMLRoot(); |
||
99 | $node = $xml->createElement($node_name); |
||
100 | |||
101 | if (is_array($arr)) { |
||
102 | // get the attributes first.; |
||
103 | if (array_key_exists('@attributes', $arr) && is_array($arr['@attributes'])) { |
||
104 | foreach ($arr['@attributes'] as $key => $value) { |
||
105 | if (!self::isValidTagName($key)) { |
||
106 | throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name); |
||
107 | } |
||
108 | $node->setAttribute($key, self::bool2str($value)); |
||
109 | } |
||
110 | unset($arr['@attributes']); //remove the key from the array once done. |
||
111 | } |
||
112 | |||
113 | // check if it has a value stored in @value, if yes store the value and return |
||
114 | // else check if its directly stored as string |
||
115 | if (array_key_exists('@value', $arr)) { |
||
116 | $node->appendChild($xml->createTextNode(self::bool2str($arr['@value']))); |
||
117 | unset($arr['@value']); //remove the key from the array once done. |
||
118 | //return from recursion, as a note with value cannot have child nodes. |
||
119 | return $node; |
||
120 | } elseif (array_key_exists('@cdata', $arr)) { |
||
121 | $node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata']))); |
||
122 | unset($arr['@cdata']); //remove the key from the array once done. |
||
123 | //return from recursion, as a note with cdata cannot have child nodes. |
||
124 | return $node; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | //create subnodes using recursion |
||
129 | if (is_array($arr)) { |
||
130 | // recurse to get the node for that key |
||
131 | foreach ($arr as $key => $value) { |
||
132 | if (!self::isValidTagName($key)) { |
||
133 | throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name); |
||
134 | } |
||
135 | if (is_array($value) && is_numeric(key($value))) { |
||
136 | // MORE THAN ONE NODE OF ITS KIND; |
||
137 | // if the new array is numeric index, means it is array of nodes of the same kind |
||
138 | // it should follow the parent key name |
||
139 | foreach ($value as $k => $v) { |
||
140 | $node->appendChild(self::convert($key, $v)); |
||
141 | } |
||
142 | } else { |
||
143 | // ONLY ONE NODE OF ITS KIND |
||
144 | $node->appendChild(self::convert($key, $value)); |
||
145 | } |
||
146 | unset($arr[$key]); //remove the key from the array once done. |
||
147 | } |
||
148 | } |
||
149 | |||
150 | // after we are done with all the keys in the array (if it is one) |
||
151 | // we check if it has any text value, if yes, append it. |
||
152 | if (!is_array($arr)) { |
||
153 | $node->appendChild($xml->createTextNode(self::bool2str($arr))); |
||
154 | } |
||
155 | |||
156 | return $node; |
||
157 | } |
||
158 | |||
188 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.