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