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