Passed
Pull Request — master (#62)
by Sergei
13:01
created

ArrayCollectionHelper::mergeBase()   B

Complexity

Conditions 11
Paths 4

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 35
rs 7.3166
cc 11
nc 4
nop 1

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
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays\Collection;
6
7
use InvalidArgumentException;
8
use Yiisoft\Arrays\Collection\Modifier\BeforeMergeModifierInterface;
9
10
final class ArrayCollectionHelper
11
{
12
    /**
13
     * @param array|ArrayCollection ...$args
14
     * @return ArrayCollection
15
     */
16
    public static function merge(...$args): ArrayCollection
17
    {
18
        $arrays = [];
19
        foreach ($args as $arg) {
20
            $arrays[] = $arg instanceof ArrayCollection ? $arg->getData() : $arg;
21
        }
22
23
        $collections = [];
24
        foreach ($args as $arg) {
25
            $collection = $arg instanceof ArrayCollection ? $arg : new ArrayCollection($arg);
26
            foreach ($collection->getModifiers() as $modifier) {
27
                if ($modifier instanceof BeforeMergeModifierInterface) {
28
                    $collection->setData(
29
                        $modifier->beforeMerge($collection->getData(), $arrays)
30
                    );
31
                }
32
            }
33
            $collections[] = $collection;
34
        }
35
36
        return static::mergeBase(...$args);
37
    }
38
39
    /**
40
     * @param array|ArrayCollection ...$args
41
     * @return ArrayCollection
42
     */
43
    private static function mergeBase(...$args): ArrayCollection
44
    {
45
        $collection = new ArrayCollection();
46
47
        while (!empty($args)) {
48
            $array = array_shift($args);
49
50
            if ($array instanceof ArrayCollection) {
51
                $collection->pullCollectionArgs($array);
52
                $collection->setData(
53
                    static::mergeBase($collection->getData(), $array->getData())->getData()
54
                );
55
                continue;
56
            } elseif (!is_array($array)) {
57
                throw new InvalidArgumentException();
58
            }
59
60
            foreach ($array as $k => $v) {
61
                if (is_int($k)) {
62
                    if ($collection->keyExists($k)) {
63
                        if ($collection[$k] !== $v) {
64
                            $collection[] = $v;
65
                        }
66
                    } else {
67
                        $collection[$k] = $v;
68
                    }
69
                } elseif (static::isMergable($v) && isset($collection[$k]) && static::isMergable($collection[$k])) {
70
                    $collection[$k] = static::mergeBase($collection[$k], $v)->getData();
71
                } else {
72
                    $collection[$k] = $v;
73
                }
74
            }
75
        }
76
77
        return $collection;
78
    }
79
80
    /**
81
     * @param mixed $value
82
     * @return bool
83
     */
84
    private static function isMergable($value): bool
85
    {
86
        return is_array($value) || $value instanceof ArrayCollection;
87
    }
88
}
89