| Conditions | 14 |
| Paths | 21 |
| Total Lines | 35 |
| Code Lines | 21 |
| 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 |
||
| 66 | function data($target, $key, $default = null) |
||
| 67 | { |
||
| 68 | if (is_null($key)) { |
||
| 69 | return $target; |
||
| 70 | } |
||
| 71 | |||
| 72 | $key = is_array($key) ? $key : explode('.', $key); |
||
| 73 | |||
| 74 | while (!is_null($segment = array_shift($key))) { |
||
| 75 | if ($segment === '*') { |
||
| 76 | if ($target instanceof Collection) { |
||
| 77 | $target = $target->all(); |
||
| 78 | } elseif (!is_array($target)) { |
||
| 79 | return value($default); |
||
| 80 | } |
||
| 81 | |||
| 82 | $result = Arr::pluck($target, $key); |
||
| 83 | |||
| 84 | return in_array('*', $key) ? Arr::collapse($result) : $result; |
||
| 85 | } |
||
| 86 | |||
| 87 | if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
||
| 88 | $target = $target[$segment]; |
||
| 89 | } elseif (is_object($target) && isset($target->{$segment})) { |
||
| 90 | $target = $target->{$segment}; |
||
| 91 | } elseif (is_object($target) && method_exists($target, $segment)) { |
||
| 92 | // This is different than laravel.. |
||
| 93 | $target = $target->{$segment}; |
||
| 94 | } else { |
||
| 95 | return value($default); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return $target; |
||
| 100 | } |
||
| 101 | } |
||
| 102 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.