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 |
||
144 | protected function _get($source, array $path, &$result, int &$errorsCount): void |
||
145 | { |
||
146 | // let's iterate every path part from stack |
||
147 | while(count($path)) { |
||
148 | if(is_array($source) && !ArrayHelper::isAssoc($source)) { |
||
149 | // the result will be multiple |
||
150 | if(!is_array($result)) { |
||
151 | $result = []; |
||
152 | } |
||
153 | // and we need to use recursive call for each item of this array |
||
154 | foreach($source as $item) { |
||
155 | $this->_get($item, $path, $result, $errorsCount); |
||
156 | } |
||
157 | // we don't need to do something in this recursive branch |
||
158 | return; |
||
159 | } |
||
160 | |||
161 | $key = array_pop($path); |
||
162 | |||
163 | if(is_array($source)) { |
||
164 | if(!array_key_exists($key, $source)) { |
||
165 | // path part key is missing in source array |
||
166 | $errorsCount++; |
||
167 | // we cannot go deeper |
||
168 | return; |
||
169 | } |
||
170 | // go to the next nested level |
||
171 | $source = $source[$key]; |
||
172 | } elseif(is_object($source)) { |
||
173 | $getterName = 'get'.ucfirst($key); |
||
174 | if(method_exists($source, $getterName)) { |
||
175 | // go to the next nested level |
||
176 | $source = $source->{$getterName}(); |
||
177 | } elseif(property_exists($source, $key)) { |
||
178 | // go to the next nested level |
||
179 | $source = $source->{$key}; |
||
180 | } else { |
||
181 | // path part key is missing in source object |
||
182 | $errorsCount++; |
||
183 | // we cannot go deeper |
||
184 | return; |
||
185 | } |
||
186 | } else { |
||
187 | // source is scalar, so we can't go to the next depth level |
||
188 | $errorsCount++; |
||
189 | // we cannot go deeper |
||
190 | return; |
||
191 | } |
||
192 | |||
193 | // when it's not the last iteration of the stack |
||
194 | // and the source is non-associative array (list) |
||
195 | if(count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
||
196 | // the result will be multiple |
||
197 | if(!is_array($result)) { |
||
198 | $result = []; |
||
199 | } |
||
200 | // and we need to use recursive call for each item of this array |
||
201 | foreach($source as $item) { |
||
202 | $this->_get($item, $path, $result, $errorsCount); |
||
203 | } |
||
204 | // we don't need to do something in this recursive branch |
||
205 | return; |
||
206 | } |
||
207 | } |
||
208 | |||
209 | // now path stack is empty — we reached target value of given path in source argument |
||
210 | // so if result is multiple |
||
211 | if(is_array($result)) { |
||
212 | // we append source to result |
||
213 | $result[] = $source; |
||
214 | } else { |
||
215 | // result is single |
||
216 | $result = $source; |
||
217 | } |
||
289 |