Conditions | 14 |
Paths | 8 |
Total Lines | 35 |
Code Lines | 22 |
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 |
||
60 | public function merge(array $array1, array $arrays = null) |
||
61 | { |
||
62 | $arrays = array_slice(func_get_args(), 1); |
||
63 | if (count($arrays) === 0) { |
||
64 | return $array1; |
||
65 | } |
||
66 | |||
67 | // if all arrays are sequential and flag is set, append them all |
||
68 | if ($this->flags & static::FLAG_APPEND_VALUE_ARRAY == static::FLAG_APPEND_VALUE_ARRAY |
||
69 | && array_is_sequential($array1) |
||
70 | && count(array_filter($arrays, 'Graze\ArrayMerger\array_is_sequential')) === count($arrays)) { |
||
71 | return call_user_func_array('array_merge', array_merge([$array1], $arrays)); |
||
72 | } |
||
73 | |||
74 | $merged = $array1; |
||
75 | |||
76 | foreach ($arrays as $toMerge) { |
||
77 | foreach ($toMerge as $key => &$value) { |
||
78 | if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) { |
||
79 | if ($this->flags & static::FLAG_APPEND_VALUE_ARRAY == static::FLAG_APPEND_VALUE_ARRAY |
||
80 | && array_is_sequential($value) |
||
81 | && array_is_sequential($merged[$key])) { |
||
82 | $merged[$key] = array_merge($merged[$key], $value); |
||
83 | } else { |
||
84 | $merged[$key] = $this->merge($merged[$key], $value); |
||
85 | } |
||
86 | } elseif (array_key_exists($key, $merged)) { |
||
87 | $merged[$key] = call_user_func($this->valueMerger, $merged[$key], $value); |
||
88 | } else { |
||
89 | $merged[$key] = $value; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return $merged; |
||
95 | } |
||
97 |