Conditions | 17 |
Paths | 27 |
Total Lines | 73 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
123 | protected function _get($source, array $path, &$result, int &$errorsCount): void |
||
124 | { |
||
125 | // let's iterate every path part from stack |
||
126 | while(count($path)) { |
||
127 | if(is_array($source) && !ArrayHelper::isAssoc($source)) { |
||
128 | // the result will be multiple |
||
129 | if(!is_array($result)) { |
||
130 | $result = []; |
||
131 | } |
||
132 | // and we need to use recursive call for each item of this array |
||
133 | foreach($source as $item) { |
||
134 | $this->_get($item, $path, $result, $errorsCount); |
||
135 | } |
||
136 | // we don't need to do something in this recursive branch |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | $key = array_pop($path); |
||
141 | |||
142 | if(is_array($source)) { |
||
143 | if(!array_key_exists($key, $source)) { |
||
144 | // path part key is missing in source array |
||
145 | $errorsCount++; |
||
146 | // we cannot go deeper |
||
147 | return; |
||
148 | } |
||
149 | // go to the next nested level |
||
150 | $source = $source[$key]; |
||
151 | } elseif(is_object($source)) { |
||
152 | $getterName = 'get'.ucfirst($key); |
||
153 | if(method_exists($source, $getterName)) { |
||
154 | // go to the next nested level |
||
155 | $source = $source->{$getterName}(); |
||
156 | } elseif(property_exists($source, $key)) { |
||
157 | // go to the next nested level |
||
158 | $source = $source->{$key}; |
||
159 | } else { |
||
160 | // path part key is missing in source object |
||
161 | $errorsCount++; |
||
162 | // we cannot go deeper |
||
163 | return; |
||
164 | } |
||
165 | } else { |
||
166 | // source is scalar, so we can't go to the next depth level |
||
167 | $errorsCount++; |
||
168 | // we cannot go deeper |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | // when it's not the last iteration of the stack |
||
173 | // and the source is non-associative array (list) |
||
174 | if(count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
||
175 | // the result will be multiple |
||
176 | if(!is_array($result)) { |
||
177 | $result = []; |
||
178 | } |
||
179 | // and we need to use recursive call for each item of this array |
||
180 | foreach($source as $item) { |
||
181 | $this->_get($item, $path, $result, $errorsCount); |
||
182 | } |
||
183 | // we don't need to do something in this recursive branch |
||
184 | return; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // now path stack is empty — we reached target value of given path in source argument |
||
189 | // so if result is multiple |
||
190 | if(is_array($result)) { |
||
191 | // we append source to result |
||
192 | $result[] = $source; |
||
193 | } else { |
||
194 | // result is single |
||
195 | $result = $source; |
||
196 | } |
||
238 |