Completed
Push — master ( 07ca78...9faab8 )
by Cees-Jan
08:33
created

functions.php ➔ options_merge()   C

Complexity

Conditions 15
Paths 153

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 31
nc 153
nop 2
dl 0
loc 55
rs 6.051
c 0
b 0
f 0

How to fix   Long Method    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 declare(strict_types=1);
2
3
namespace ApiClients\Foundation;
4
5
/**
6
 * @param array $base
7
 * @param array $options
8
 * @return array
9
 */
10
function options_merge(array $base, array $options): array
11
{
12
    $merge = true;
13
    foreach ($base as $key => $value) {
14
        if (is_numeric($key)) {
15
            $merge = false;
16
        }
17
    }
18
    foreach ($options as $name => $option) {
19
        if (is_numeric($name)) {
20
            $merge = false;
21
        }
22
    }
23
24
    if ($merge === false) {
25
        $new = [];
26
27
        foreach ($base as $key => $value) {
28
            if (in_array($value, $new)) {
29
                continue;
30
            }
31
            $new[] = $value;
32
        }
33
        foreach ($options as $name => $option) {
34
            if (in_array($option, $new)) {
35
                continue;
36
            }
37
            $new[] = $option;
38
        }
39
40
        return $new;
41
    }
42
43
    foreach ($base as $key => $value) {
44
        if (!isset($options[$key])) {
45
            continue;
46
        }
47
48
        $option = $options[$key];
49
        unset($options[$key]);
50
51
        if (is_array($value) && is_array($option)) {
52
            $base[$key] = options_merge($value, $option);
53
            continue;
54
        }
55
56
        $base[$key] = $option;
57
    }
58
59
    foreach ($options as $name => $option) {
60
        $base[$name] = $option;
61
    }
62
63
    return $base;
64
}
65