ArrayDefinitionHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 55
ccs 29
cts 29
cp 1
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
C merge() 0 41 12
A mergeArguments() 0 7 2
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 11
            foreach (array_shift($configs) as $key => $value) {
23 11
                if (!is_string($key)) {
24 1
                    throw ExceptionHelper::invalidArrayDefinitionKey($key);
25
                }
26
27 10
                if (!isset($result[$key])) {
28 1
                    $result[$key] = $value;
29 1
                    continue;
30
                }
31
32 10
                if ($key === ArrayDefinition::CONSTRUCTOR) {
33 5
                    if (!is_array($value)) {
34 1
                        throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($value);
35
                    }
36 4
                    if (!is_array($result[$key])) {
37 1
                        throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($result[$key]);
38
                    }
39 3
                    $result[$key] = self::mergeArguments($result[$key], $value);
40 3
                    continue;
41
                }
42
43 6
                if (str_ends_with($key, '()')) {
44 5
                    if (!is_array($value)) {
45 1
                        throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $value);
46
                    }
47 4
                    if (!is_array($result[$key])) {
48 1
                        throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $result[$key]);
49
                    }
50 3
                    $result[$key] = self::mergeArguments($result[$key], $value);
51 3
                    continue;
52
                }
53
54 1
                $result[$key] = $value;
55
            }
56
        }
57
58 8
        return $result;
59
    }
60
61 7
    public static function mergeArguments(array $argumentsA, array $argumentsB): array
62
    {
63 7
        foreach ($argumentsB as $name => $argument) {
64 6
            $argumentsA[$name] = $argument;
65
        }
66
67 7
        return $argumentsA;
68
    }
69
}
70