| Conditions | 10 |
| Paths | 12 |
| Total Lines | 22 |
| Code Lines | 16 |
| 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 |
||
| 90 | public static function set(array &$array, string $name, $value, $def=null):array |
||
| 91 | { |
||
| 92 | $path = explode('.', $name); |
||
| 93 | $root = &$array; |
||
| 94 | while (count($path) > 1) { |
||
| 95 | $key = array_shift($path); |
||
| 96 | if (is_array($array)) { |
||
| 97 | if (!array_key_exists($key, $array)) { |
||
| 98 | $array[$key] = []; |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $array=[]; |
||
| 102 | } |
||
| 103 | $array = &$array[$key]; |
||
| 104 | } |
||
| 105 | $key = array_shift($path); |
||
| 106 | if (is_array($array) && array_key_exists($key, $array) && is_array($array[$key]) && is_array($value)) { |
||
| 107 | $array[$key] = array_merge($array[$key], is_array($def) ? $def : [], $value); |
||
| 108 | } else { |
||
| 109 | $array[$key] = is_null($value) ? $def : $value; |
||
| 110 | } |
||
| 111 | return $root; |
||
| 112 | } |
||
| 141 |