Conditions | 14 |
Paths | 29 |
Total Lines | 41 |
Code Lines | 23 |
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 |
||
43 | function data_get($target, $key, $default = null) |
||
44 | { |
||
45 | if (is_null($key)) { |
||
46 | return $target; |
||
47 | } |
||
48 | |||
49 | $key = is_array($key) ? $key : explode('.', $key); |
||
50 | |||
51 | foreach ($key as $i => $segment) { |
||
52 | unset($key[$i]); |
||
53 | |||
54 | if (is_null($segment)) { |
||
55 | return $target; |
||
56 | } |
||
57 | |||
58 | if ($segment === '*') { |
||
59 | if ($target instanceof Collection) { |
||
60 | $target = $target->all(); |
||
61 | } elseif (!is_array($target)) { |
||
62 | return value($default); |
||
63 | } |
||
64 | |||
65 | $result = []; |
||
66 | |||
67 | foreach ($target as $item) { |
||
68 | $result[] = data_get($item, $key); |
||
69 | } |
||
70 | |||
71 | return in_array('*', $key) ? Arr::collapse($result) : $result; |
||
72 | } |
||
73 | |||
74 | if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
||
75 | $target = $target[$segment]; |
||
76 | } elseif (is_object($target) && isset($target->{$segment})) { |
||
77 | $target = $target->{$segment}; |
||
78 | } else { |
||
79 | return value($default); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $target; |
||
84 | } |
||
187 |