| Conditions | 20 |
| Paths | 34 |
| Total Lines | 49 |
| Code Lines | 31 |
| 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 |
||
| 97 | function data_set(&$target, $key, $value, $overwrite = true) |
||
| 98 | { |
||
| 99 | $segments = is_array($key) ? $key : explode('.', $key); |
||
| 100 | |||
| 101 | if (($segment = array_shift($segments)) === '*') { |
||
| 102 | if (!Arr::accessible($target)) { |
||
| 103 | $target = []; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($segments) { |
||
| 107 | foreach ($target as &$inner) { |
||
| 108 | data_set($inner, $segments, $value, $overwrite); |
||
| 109 | } |
||
| 110 | } elseif ($overwrite) { |
||
| 111 | foreach ($target as &$inner) { |
||
| 112 | $inner = $value; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } elseif (Arr::accessible($target)) { |
||
| 116 | if ($segments) { |
||
| 117 | if (!Arr::exists($target, $segment)) { |
||
| 118 | $target[$segment] = []; |
||
| 119 | } |
||
| 120 | |||
| 121 | data_set($target[$segment], $segments, $value, $overwrite); |
||
| 122 | } elseif ($overwrite || !Arr::exists($target, $segment)) { |
||
| 123 | $target[$segment] = $value; |
||
| 124 | } |
||
| 125 | } elseif (is_object($target)) { |
||
| 126 | if ($segments) { |
||
| 127 | if (!isset($target->{$segment})) { |
||
| 128 | $target->{$segment} = []; |
||
| 129 | } |
||
| 130 | |||
| 131 | data_set($target->{$segment}, $segments, $value, $overwrite); |
||
| 132 | } elseif ($overwrite || !isset($target->{$segment})) { |
||
| 133 | $target->{$segment} = $value; |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $target = []; |
||
| 137 | |||
| 138 | if ($segments) { |
||
| 139 | data_set($target[$segment], $segments, $value, $overwrite); |
||
| 140 | } elseif ($overwrite) { |
||
| 141 | $target[$segment] = $value; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $target; |
||
| 146 | } |
||
| 187 |