Conditions | 16 |
Paths | 106 |
Total Lines | 93 |
Code Lines | 34 |
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 |
||
85 | public static function toArray($xml, $recursionDepth = 0) |
||
86 | { |
||
87 | // Keep an eye on how deeply we are involved in recursion. |
||
88 | if ($recursionDepth > self::MAX_RECURSION_DEPTH_ALLOWED) { |
||
89 | return; |
||
90 | } |
||
91 | |||
92 | // we are at top/root level |
||
93 | if ($recursionDepth === 0) { |
||
94 | // If the external caller doesn't call this function initially |
||
95 | // with a SimpleXMLElement object just return |
||
96 | if (get_class($xml) !== 'SimpleXMLElement') { |
||
97 | return; |
||
98 | } else { // store original SimpleXmlElementObject sent by the caller. |
||
99 | $provided_xml_object = $xml; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | if (is_a($xml, 'SimpleXMLElement')) { |
||
104 | // Get a copy of the simpleXmlElementObject |
||
105 | $copy_of_xml_object = $xml; |
||
106 | // Get the object variables in the SimpleXmlElement object for us to iterate. |
||
107 | $xml = get_object_vars($xml); |
||
108 | } |
||
109 | |||
110 | // It needs to be an array of object variables. |
||
111 | if (is_array($xml)) { |
||
112 | // Initialize the result array. |
||
113 | $resultArray = []; |
||
114 | |||
115 | // Is the input array size 0? Then, we reached the rare CDATA text if any. |
||
116 | if (count($xml) === 0) { |
||
117 | // return the lonely CDATA. It could even be whitespaces. |
||
118 | return (string) $copy_of_xml_object; |
||
119 | } |
||
120 | |||
121 | // walk through the child elements now. |
||
122 | foreach ($xml as $key => $value) { |
||
123 | /* |
||
124 | * Add Attributes to the results array ? |
||
125 | */ |
||
126 | if (self::FETCH_ATTRIBUTES === false) { |
||
127 | // check KEY - is it an attribute? |
||
128 | if ((is_string($key)) && ($key === '@attributes')) { |
||
129 | // yes, don't add, just continue with next element of foreach |
||
130 | continue; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | // increase the recursion depth by one. |
||
135 | ++$recursionDepth; |
||
136 | |||
137 | // WATCH IT ! RECURSION !!! |
||
138 | // recursively process the current (VALUE) element |
||
139 | $resultArray[$key] = self::toArray($value, $recursionDepth); |
||
140 | |||
141 | // decrease the recursion depth by one |
||
142 | --$recursionDepth; |
||
143 | } |
||
144 | |||
145 | // we are reaching the top/root level |
||
146 | if ($recursionDepth === 0) { |
||
147 | /* |
||
148 | * Set the XML root element name as the root [top-level] key of |
||
149 | * the associative array that we are going to return to the caller of this |
||
150 | * recursive function. |
||
151 | */ |
||
152 | $tempArray = $resultArray; |
||
153 | $resultArray = []; |
||
154 | $resultArray[$provided_xml_object->getName()] = $tempArray; |
||
155 | } |
||
156 | |||
157 | /* |
||
158 | * Shifts every key/value pair of @attributes one level up and removes @attributes |
||
159 | */ |
||
160 | if (self::FETCH_ATTRIBUTES && self::REMOVE_ATTRIBUTES_SUBLEVEL === true) { |
||
161 | // ok, add attributes |
||
162 | if (isset($resultArray['@attributes'])) { |
||
163 | // but as key => value elements |
||
164 | foreach ($resultArray['@attributes'] as $key => $value) { |
||
165 | $resultArray[$key] = $value; |
||
166 | } |
||
167 | // removing @attributes |
||
168 | unset($resultArray['@attributes']); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | return $resultArray; |
||
173 | } else { |
||
174 | // it's is either the XML attribute text or text between XML tags |
||
175 | return (string) $xml; |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.