Conditions | 27 |
Paths | 40 |
Total Lines | 75 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
223 | public static function callFuncArray($callback, array $args) |
||
224 | { |
||
225 | // deal with "class::method" syntax |
||
226 | if (is_string($callback) && strpos($callback, '::') !== false) { |
||
227 | $callback = explode('::', $callback); |
||
228 | } |
||
229 | |||
230 | // if an array is passed, extract the object and method to call |
||
231 | if (is_array($callback) && isset($callback[1]) && is_object($callback[0])) { |
||
232 | list($instance, $method) = $callback; |
||
233 | |||
234 | // Calling the method directly is faster then call_user_func_array() ! |
||
235 | switch (count($args)) { |
||
236 | case 0: |
||
237 | return $instance->$method(); |
||
238 | |||
239 | case 1: |
||
240 | return $instance->$method($args[0]); |
||
241 | |||
242 | case 2: |
||
243 | return $instance->$method($args[0], $args[1]); |
||
244 | |||
245 | case 3: |
||
246 | return $instance->$method($args[0], $args[1], $args[2]); |
||
247 | |||
248 | case 4: |
||
249 | return $instance->$method($args[0], $args[1], $args[2], $args[3]); |
||
250 | } |
||
251 | } elseif (is_array($callback) && isset($callback[1]) && is_string($callback[0])) { |
||
252 | list($class, $method) = $callback; |
||
253 | $class = '\\'.ltrim($class, '\\'); |
||
254 | |||
255 | // Calling the method directly is faster then call_user_func_array() ! |
||
256 | switch (count($args)) { |
||
257 | case 0: |
||
258 | return $class::$method(); |
||
259 | |||
260 | case 1: |
||
261 | return $class::$method($args[0]); |
||
262 | |||
263 | case 2: |
||
264 | return $class::$method($args[0], $args[1]); |
||
265 | |||
266 | case 3: |
||
267 | return $class::$method($args[0], $args[1], $args[2]); |
||
268 | |||
269 | case 4: |
||
270 | return $class::$method($args[0], $args[1], $args[2], $args[3]); |
||
271 | } |
||
272 | // if it's a string, it's a native function or a static method call |
||
273 | } elseif (is_string($callback) || $callback instanceof Closure) { |
||
274 | is_string($callback) && $callback = ltrim($callback, '\\'); |
||
275 | |||
276 | // calling the method directly is faster then call_user_func_array() ! |
||
277 | switch (count($args)) { |
||
278 | case 0: |
||
279 | return $callback(); |
||
280 | |||
281 | case 1: |
||
282 | return $callback($args[0]); |
||
283 | |||
284 | case 2: |
||
285 | return $callback($args[0], $args[1]); |
||
286 | |||
287 | case 3: |
||
288 | return $callback($args[0], $args[1], $args[2]); |
||
289 | |||
290 | case 4: |
||
291 | return $callback($args[0], $args[1], $args[2], $args[3]); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | // fallback, handle the old way |
||
296 | return call_user_func_array($callback, $args); |
||
297 | } |
||
298 | |||
311 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.