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

ArrayDefinitionHelper::merge()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 24
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 45
ccs 25
cts 25
cp 1
crap 12
rs 6.9666

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\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