Combined::merge()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
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 7
cts 7
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
 *
12
 * @extends Wrapper<E>
13
 */
14
final class Combined extends Wrapper
15
{
16
    /**
17
     * @param Dictionary<E> ...$dictionaries
18 9
     */
19
    public function __construct(string $name, Dictionary ...$dictionaries)
20 9
    {
21 9
        parent::__construct(
22 6
            new Invokable($name, function () use ($dictionaries): array {
23
                $data = [];
24 6
25 6
                foreach ($dictionaries as $dictionary) {
26
                    $data = $this->merge($data, iterator_to_array($dictionary));
27
                }
28 6
29 9
                return $data;
30
            })
31 9
        );
32
    }
33
34
    /**
35
     * @param E[] $array1
36
     * @param E[] $array2
37
     *
38
     * @return E[]
39 6
     */
40
    private function merge(array $array1, array $array2): array
41 6
    {
42 1
        if ($array1 === array_values($array1) && $array2 === array_values($array2)) {
43
            return [...$array1, ...$array2];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($array1, $array2) returns the type array<integer,Knp\Dictio...yBundle\Dictionary\E[]> which is incompatible with the documented return type Knp\DictionaryBundle\Dictionary\E[].
Loading history...
44
        }
45 5
46
        $data = [];
47 5
48 5
        foreach ([$array1, $array2] as $array) {
49 5
            foreach ($array as $key => $value) {
50
                $data[$key] = $value;
51
            }
52
        }
53 5
54
        return $data;
55
    }
56
}
57