Passed
Push — master ( 3bdf73...c101c0 )
by Michael
31s
created

Priority::normaliseItem()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3.1406
1
<?php
2
3
namespace micmania1\config\MergeStrategy;
4
5
use micmania1\config\ConfigCollectionInterface;
6
use micmania1\config\ConfigCollection;
7
8
class Priority
9
{
10 16
    public function merge(array $mine, ConfigCollectionInterface $theirs) {
11 16
        foreach ($mine as $key => $item) {
12
13
            // Ensure we have value/metadata keys
14 16
            $item = $this->normaliseItem($item);
15
16
            // If the item doesn't exist in theirs, we can just set it and continue.
17 16
            if(!$theirs->exists($key)) {
18 11
                $theirs->set($key, $item['value']);
19 11
                continue;
20
            }
21
22
            // Get the two values for comparison
23 11
            $value = $item['value'];
24
25
            // If its an array and the key already esists, we can use array_merge
26 11
            if (is_array($value) && is_array($theirs->get($key))) {
27 2
                $value = array_merge($theirs->get($key), $value);
28 2
            }
29
30
            // The key is not set or the value is to be overwritten
31 11
            $theirs->set($key, $value, $item['metadata']);
32 16
        }
33
34 16
        return $theirs;
35
    }
36
37
    /**
38
     * Returns a normalised array with value/metadata keys
39
     *
40
     * @param array
41
     *
42
     * @return array
43
     */
44 16
    protected function normaliseItem(array $item)
45
    {
46 16
        if(!isset($item['value'])) {
47
            $item['value'] = '';
48
        }
49
50 16
        if(!isset($item['metadata'])) {
51 5
            $item['metadata'] = [];
52 5
        }
53
54 16
        return ['value' => $item['value'], 'metadata' => $item['metadata']];
55
    }
56
}
57