Conditions | 19 |
Paths | 34 |
Total Lines | 68 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
100 | private function nodeToArray(DOMNode $node) |
||
101 | { |
||
102 | $tmp = []; |
||
103 | foreach ($node->childNodes as $each) { |
||
104 | if ($each->nodeType !== XML_ELEMENT_NODE) { |
||
105 | continue; |
||
106 | } |
||
107 | |||
108 | // if node only has a type attribute lets distinguish them directly |
||
109 | // and then ignore the attribtue |
||
110 | if ($each->hasAttribute('type')) { |
||
111 | $key = $each->localName . '@' . $each->getAttribute('type'); |
||
112 | $ignore_attributes = true; |
||
113 | } else { |
||
114 | $key = $each->localName; |
||
115 | $ignore_attributes = false; |
||
116 | } |
||
117 | |||
118 | // in case of special keys, always create array |
||
119 | if (isset($this->n2a_force_array[$key])) { |
||
|
|||
120 | $current = &$tmp[$key][]; |
||
121 | end($tmp[$key]); |
||
122 | $insert_key = key($tmp[$key]); |
||
123 | } |
||
124 | // if key already exists, dynamically create an array |
||
125 | elseif (isset($tmp[$key])) { |
||
126 | if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) { |
||
127 | $tmp[$key] = [$tmp[$key]]; |
||
128 | } |
||
129 | $current = &$tmp[$key][]; |
||
130 | end($tmp[$key]); |
||
131 | $insert_key = key($tmp[$key]); |
||
132 | } |
||
133 | // key was not yet set, so lets start off with a string |
||
134 | else { |
||
135 | $current = &$tmp[$key]; |
||
136 | $insert_key = null; |
||
137 | } |
||
138 | |||
139 | if ($each->hasChildNodes()) { |
||
140 | $current = $this->nodeToArray($each); |
||
141 | } else { |
||
142 | $current = $each->nodeValue; |
||
143 | |||
144 | if (!$ignore_attributes && !isset($this->n2a_ignore_attr[$each->localName]) && $each->hasAttributes()) { |
||
145 | foreach ($each->attributes as $attr) { |
||
146 | |||
147 | // single attribute with empty node, use the attr-value directly |
||
148 | if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) { |
||
149 | $current = $attr->nodeValue; |
||
150 | break; |
||
151 | } |
||
152 | |||
153 | if ($insert_key) { |
||
154 | if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) { |
||
155 | $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]]; |
||
156 | } |
||
157 | $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue; |
||
158 | } else { |
||
159 | $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | return $tmp; |
||
167 | } |
||
168 | } |
||
169 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: