ArrayDefinitionHelper::merge()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 41
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 41
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 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