| Conditions | 10 |
| Paths | 14 |
| Total Lines | 22 |
| Code Lines | 12 |
| 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 |
||
| 13 | public static function getValue($array, $key, $default = null) |
||
| 14 | { |
||
| 15 | if ($key instanceof \Closure) { |
||
| 16 | return $key($array, $default); |
||
| 17 | } |
||
| 18 | |||
| 19 | if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) { |
||
| 20 | return $array[$key]; |
||
| 21 | } |
||
| 22 | |||
| 23 | if (($pos = strrpos($key, '.')) !== false) { |
||
| 24 | $array = static::getValue($array, substr($key, 0, $pos), $default); |
||
| 25 | $key = substr($key, $pos + 1); |
||
| 26 | } |
||
| 27 | |||
| 28 | if (is_object($array)) { |
||
| 29 | return $array->$key; |
||
| 30 | } elseif (is_array($array)) { |
||
| 31 | return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default; |
||
| 32 | } |
||
| 33 | |||
| 34 | return $default; |
||
| 35 | } |
||
| 46 | } |