Passed
Push — master ( 6cfdb1...d9c2c2 )
by Sergei
02:31
created

ArrayDefinitionHelper::mergeArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions\Helpers;
6
7
use Yiisoft\Definitions\ArrayDefinition;
8
9
use Yiisoft\Definitions\Exception\InvalidConfigException;
10
11
use function is_array;
12
13
final class ArrayDefinitionHelper
14
{
15
    /**
16
     * @throws InvalidConfigException
17
     */
18 13
    public static function merge(array ...$configs): array
19
    {
20 13
        $result = array_shift($configs) ?: [];
21 13
        while (!empty($configs)) {
22
            /** @var mixed $value */
23 11
            foreach (array_shift($configs) as $key => $value) {
24 11
                if (!is_string($key)) {
25 1
                    throw ExceptionHelper::invalidArrayDefinitionKey($key);
26
                }
27
28 10
                if (!isset($result[$key])) {
29
                    /** @var mixed */
30 1
                    $result[$key] = $value;
31 1
                    continue;
32
                }
33
34 10
                if ($key === ArrayDefinition::CONSTRUCTOR) {
35 5
                    if (!is_array($value)) {
36 1
                        throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($value);
37
                    }
38 4
                    if (!is_array($result[$key])) {
39 1
                        throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($result[$key]);
40
                    }
41 3
                    $result[$key] = self::mergeArguments($result[$key], $value);
42 3
                    continue;
43
                }
44
45 6
                if (str_ends_with($key, '()')) {
46 5
                    if (!is_array($value)) {
47 1
                        throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $value);
48
                    }
49 4
                    if (!is_array($result[$key])) {
50 1
                        throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $result[$key]);
51
                    }
52
                    /** @var mixed */
53 3
                    $result[$key] = self::mergeArguments($result[$key], $value);
54 3
                    continue;
55
                }
56
57
                /** @var mixed */
58 1
                $result[$key] = $value;
59
            }
60
        }
61
62 8
        return $result;
63
    }
64
65 7
    public static function mergeArguments(array $argumentsA, array $argumentsB): array
66
    {
67
        /** @var mixed $argument */
68 7
        foreach ($argumentsB as $name => $argument) {
69
            /** @var mixed */
70 6
            $argumentsA[$name] = $argument;
71
        }
72
73 7
        return $argumentsA;
74
    }
75
}
76