Passed
Pull Request — master (#149)
by Pierre
03:06
created

Combined::merge()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary;
6
7
use Knp\DictionaryBundle\Dictionary;
8
9
/**
10
 * @template E
11
 * @extends Wrapper<E>
12
 */
13
final class Combined extends Wrapper
14
{
15
    /**
16
     * @param Dictionary<E> ...$dictionaries
17
     */
18 9
    public function __construct(string $name, Dictionary ...$dictionaries)
19
    {
20 9
        parent::__construct(
21 9
            new Invokable($name, function () use ($dictionaries) {
22 6
                $data = [];
23
24 6
                foreach ($dictionaries as $dictionary) {
25 6
                    $data = $this->merge($data, iterator_to_array($dictionary));
26
                }
27
28 6
                return $data;
29 9
            })
30
        );
31 9
    }
32
33
    /**
34
     * @param E[] $array1
35
     * @param E[] $array2
36
     *
37
     * @return E[]
38
     */
39 6
    private function merge(array $array1, array $array2): array
40
    {
41 6
        if ($array1 === array_values($array1) && $array2 === array_values($array2)) {
42 1
            return array_merge($array1, $array2);
43
        }
44
45 5
        $data = [];
46
47 5
        foreach ([$array1, $array2] as $array) {
48 5
            foreach ($array as $key => $value) {
49 5
                $data[$key] = $value;
50
            }
51
        }
52
53 5
        return $data;
54
    }
55
}
56