Conditions | 19 |
Paths | 34 |
Total Lines | 68 |
Code Lines | 40 |
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 |
||
118 | private function nodeToArray(DOMNode $node) |
||
119 | { |
||
120 | $tmp = []; |
||
121 | foreach ($node->childNodes as $each) { |
||
122 | if ($each->nodeType !== XML_ELEMENT_NODE) { |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | // if node only has a type attribute lets distinguish them directly |
||
127 | // and then ignore the attribtue |
||
128 | if ($each->hasAttribute('type')) { |
||
129 | $key = $each->localName . '@' . $each->getAttribute('type'); |
||
130 | $ignore_attributes = true; |
||
131 | } else { |
||
132 | $key = $each->localName; |
||
133 | $ignore_attributes = false; |
||
134 | } |
||
135 | |||
136 | // in case of special keys, always create array |
||
137 | if (isset($this->n2a_force_array[$key])) { |
||
138 | $current = &$tmp[$key][]; |
||
139 | end($tmp[$key]); |
||
140 | $insert_key = key($tmp[$key]); |
||
141 | } |
||
142 | // if key already exists, dynamically create an array |
||
143 | elseif (isset($tmp[$key])) { |
||
144 | if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) { |
||
145 | $tmp[$key] = [$tmp[$key]]; |
||
146 | } |
||
147 | $current = &$tmp[$key][]; |
||
148 | end($tmp[$key]); |
||
149 | $insert_key = key($tmp[$key]); |
||
150 | } |
||
151 | // key was not yet set, so lets start off with a string |
||
152 | else { |
||
153 | $current = &$tmp[$key]; |
||
154 | $insert_key = null; |
||
155 | } |
||
156 | |||
157 | if ($each->hasChildNodes()) { |
||
158 | $current = $this->nodeToArray($each); |
||
159 | } else { |
||
160 | $current = $each->nodeValue; |
||
161 | |||
162 | if (!$ignore_attributes && !isset($this->n2a_ignore_attr[$each->localName]) && $each->hasAttributes()) { |
||
163 | foreach ($each->attributes as $attr) { |
||
164 | |||
165 | // single attribute with empty node, use the attr-value directly |
||
166 | if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) { |
||
167 | $current = $attr->nodeValue; |
||
168 | break; |
||
169 | } |
||
170 | |||
171 | if ($insert_key) { |
||
172 | if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) { |
||
173 | $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]]; |
||
174 | } |
||
175 | $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue; |
||
176 | } else { |
||
177 | $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | return $tmp; |
||
185 | } |
||
186 | } |
||
187 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.