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