Conditions | 13 |
Paths | 16 |
Total Lines | 62 |
Code Lines | 30 |
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 |
||
60 | private static function convert($nodeName, $data) |
||
61 | { |
||
62 | //print_arr($nodeName); |
||
63 | $xml = self::getXMLRoot(); |
||
64 | $node = $xml->createElement($nodeName); |
||
65 | |||
66 | if (is_array($data)) { |
||
67 | // get the attributes first.; |
||
68 | if (array_key_exists(self::$prefixAttributes . 'attributes', $data)) { |
||
69 | foreach ($data[self::$prefixAttributes . 'attributes'] as $key => $value) { |
||
70 | self::validateTagName($key, $nodeName, 'attribute'); |
||
71 | $node->setAttribute($key, self::bool2str($value)); |
||
72 | } |
||
73 | unset($data[self::$prefixAttributes . 'attributes']); //remove the key from the array once done. |
||
74 | } |
||
75 | |||
76 | // check if it has a value stored in @value, if yes store the value and return |
||
77 | // else check if its directly stored as string |
||
78 | if (array_key_exists(self::$prefixAttributes . 'value', $data)) { |
||
79 | $node->appendChild($xml->createTextNode(self::bool2str($data[self::$prefixAttributes . 'value']))); |
||
80 | unset($data[self::$prefixAttributes . 'value']); //remove the key from the array once done. |
||
81 | //return from recursion, as a note with value cannot have child nodes. |
||
82 | return $node; |
||
83 | } else { |
||
84 | if (array_key_exists(self::$prefixAttributes . 'cdata', $data)) { |
||
85 | $node->appendChild($xml->createCDATASection(self::bool2str($data[self::$prefixAttributes . 'cdata']))); |
||
86 | unset($data[self::$prefixAttributes . 'cdata']); //remove the key from the array once done. |
||
87 | //return from recursion, as a note with cdata cannot have child nodes. |
||
88 | return $node; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | //create sub-nodes using recursion |
||
94 | if (is_array($data)) { |
||
95 | // recurse to get the node for that key |
||
96 | foreach ($data as $key => $value) { |
||
97 | self::validateTagName($key, $nodeName, 'tag'); |
||
98 | |||
99 | if (is_array($value) && reset($value) && is_numeric(key($value))) { |
||
100 | // MORE THAN ONE NODE OF ITS KIND; |
||
101 | // if the new array is numeric index, means it is array of nodes of the same kind |
||
102 | // it should follow the parent key name |
||
103 | foreach ($value as $k => $v) { |
||
104 | $node->appendChild(self::convert($key, $v)); |
||
105 | } |
||
106 | } else { |
||
107 | // ONLY ONE NODE OF ITS KIND |
||
108 | $node->appendChild(self::convert($key, $value)); |
||
109 | } |
||
110 | unset($data[$key]); //remove the key from the array once done. |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // after we are done with all the keys in the array (if it is one) |
||
115 | // we check if it has any text value, if yes, append it. |
||
116 | if (!is_array($data)) { |
||
117 | $node->appendChild($xml->createTextNode(self::bool2str($data))); |
||
118 | } |
||
119 | |||
120 | return $node; |
||
121 | } |
||
122 | |||
153 | } |