Conditions | 12 |
Paths | 27 |
Total Lines | 32 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
20 | public static function getValue($array, $key, $default = null) |
||
21 | { |
||
22 | if ($key instanceof Closure) { |
||
23 | return $key($array, $default); |
||
24 | } |
||
25 | |||
26 | if (is_array($key)) { |
||
27 | $lastKey = array_pop($key); |
||
28 | foreach ($key as $keyPart) { |
||
29 | $array = static::getValue($array, $keyPart); |
||
30 | } |
||
31 | $key = $lastKey; |
||
32 | } |
||
33 | |||
34 | if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) { |
||
35 | return $array[$key]; |
||
36 | } |
||
37 | |||
38 | if (($pos = strrpos($key, '.')) !== false) { |
||
39 | $array = static::getValue($array, substr($key, 0, $pos), $default); |
||
40 | $key = substr($key, $pos + 1); |
||
41 | } |
||
42 | |||
43 | if (is_object($array)) { |
||
44 | // this is expected to fail if the property does not exist, or __get() is not implemented |
||
45 | // it is not reliably possible to check whether a property is accessible beforehand |
||
46 | return $array->$key; |
||
47 | } elseif (is_array($array)) { |
||
48 | return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default; |
||
49 | } |
||
50 | |||
51 | return $default; |
||
52 | } |
||
72 |