Passed
Pull Request — master (#1)
by Harry
03:00
created

ArrayMerger::merge()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 22
nc 7
nop 2
dl 0
loc 35
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Graze\ArrayMerger;
4
5
use Graze\ArrayMerger\ValueMerger\LastValue;
6
7
/**
8
 * Class ArrayMerger
9
 *
10
 * This does a simple, `array_merge`, but you can choose how to merge the values yourself
11
 */
12
class ArrayMerger implements ArrayMergerInterface
13
{
14
    use StaticMethodsTrait;
15
16
    /** @var callable */
17
    protected $valueMerger;
18
    /** @var int */
19
    private $flags;
20
21
    /**
22
     * @param callable $valueMerger
23
     * @param int      $flags
24
     */
25
    public function __construct(callable $valueMerger = null, $flags = 0)
26
    {
27
        $this->valueMerger = $valueMerger ?: new LastValue();
28
        $this->flags = $flags;
29
    }
30
31
    /**
32
     * Merge using the supplied Value Merge
33
     *
34
     * @param callable $valueMerger
35
     * @param array    $array1
36
     * @param array    $arrays
37
     *
38
     * @return array
39
     */
40
    public static function mergeUsing(callable $valueMerger, array $array1, array $arrays)
41
    {
42
        $merger = new static($valueMerger);
43
        return call_user_func_array([$merger, 'merge'], array_merge([$array1], array_slice(func_get_args(), 2)));
44
    }
45
46
    /**
47
     * Merge the values from all the array supplied, the first array is treated as the base array to merge into
48
     *
49
     * @param array      $array1
50
     * @param array|null $arrays
51
     *
52
     * @return array
53
     */
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
    }
90
}
91