Priority::mergeArray()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.1039

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 15
cts 17
cp 0.8824
rs 8.1795
c 0
b 0
f 0
cc 8
nc 6
nop 2
crap 8.1039
1
<?php
2
3
namespace SilverStripe\Config\MergeStrategy;
4
5
use SilverStripe\Config\Collections\MutableConfigCollectionInterface;
6
7
class Priority
8
{
9
    /**
10
     * Merges an array of values into a collection
11
     *
12
     * @param array $mine Map of key to array with value and metadata sub-keys
13
     * @param MutableConfigCollectionInterface $theirs
14
     * @return MutableConfigCollectionInterface
15
     */
16 18
    public static function merge(array $mine, MutableConfigCollectionInterface $theirs)
17
    {
18 18
        foreach ($mine as $class => $item) {
19
            // Ensure we have value/metadata keys
20 18
            $item = static::normaliseItem($item);
21 18
            $value = $item['value'];
22 18
            $metadata = $item['metadata'];
23
24
            // If the item doesn't exist in theirs, we can just set it and continue.
25 18
            $theirValue = $theirs->get($class, null, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26 18
            if (!isset($theirValue)) {
27
                $theirs->set($class, null, $value, $metadata);
28
                continue;
29
            }
30
31
            // If its an array and the key already esists, we can use array_merge
32 18
            if (is_array($value) && is_array($theirValue)) {
33 7
                $value = static::mergeArray($value, $theirValue);
34 7
            }
35
36
            // Preserve metadata
37 18
            if (!$metadata) {
38 6
                $theirMetadata = $theirs->getMetadata();
39 6
                if (isset($theirMetadata[$class])) {
40
                    $metadata = $theirMetadata[$class];
41
                }
42 6
            }
43 18
            $theirs->set($class, null, $value, $metadata);
44 18
        }
45
46 18
        return $theirs;
47
    }
48
49
    /**
50
     * Deep merges a high priorty array into a lower priority array, overwriting duplicate
51
     * keys. If the keys are integers, then the merges acts like array_merge() and adds a new
52
     * item.
53
     *
54
     * @param array $highPriority
55
     * @param array $lowPriority
56
     *
57
     * @return array
58
     */
59 9
    public static function mergeArray(array $highPriority, array $lowPriority)
60
    {
61 9
        foreach ($highPriority as $key => $value) {
62
            // If value isn't an array, we can overwrite whatever was before it
63 9
            if (!is_array($value)) {
64 8
                if (is_int($key)) {
65 1
                    $lowPriority[] = $value;
66 1
                } else {
67 7
                    $lowPriority[$key] = $value;
68
                }
69
70 8
                continue;
71
            }
72
73
            // If not set, or we're changing type we can set low priority
74 6
            if (is_int($key) || !array_key_exists($key, $lowPriority) || !is_array($lowPriority[$key])) {
75 2
                if (is_int($key)) {
76
                    $lowPriority[] = $value;
77
                } else {
78 2
                    $lowPriority[$key] = $value;
79
                }
80
81 2
                continue;
82
            }
83
84
            // We have two arrays, so we merge
85 4
            $lowPriority[$key] = static::mergeArray($value, $lowPriority[$key]);
86 9
        }
87
88 9
        return $lowPriority;
89
    }
90
91
    /**
92
     * Returns a normalised array with value/metadata keys
93
     *
94
     * @param array
95
     *
96
     * @return array
97
     */
98 18
    protected static function normaliseItem(array $item)
99
    {
100 18
        if (!isset($item['value'])) {
101
            $item['value'] = '';
102
        }
103
104 18
        if (!isset($item['metadata'])) {
105 6
            $item['metadata'] = [];
106 6
        }
107
108 18
        return ['value' => $item['value'], 'metadata' => $item['metadata']];
109
    }
110
}
111