Passed
Branch add-cache (aacd86)
by Michael
02:10
created

Priority   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 37
ccs 18
cts 19
cp 0.9474
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C merge() 0 34 7
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 16
            if(!isset($item['value'])) {
13
                continue;
14
            }
15
16 16
            if(!isset($item['metadata'])) {
17 5
                $item['metadata'] = [];
18 5
            }
19
20
            // If the item doesn't exist in theirs, we can just set it and continue.
21 16
            if(!$theirs->exists($key)) {
22 11
                $theirs->set($key, $item['value']);
23 11
                continue;
24
            }
25
26
            /** @var ConfigItemInterface **/
27 11
            $theirsValue = $theirs->get($key);
28
29
            // Get the two values for comparison
30 11
            $lessImportantValue = $theirsValue;
31 11
            $newValue = $item['value'];
32
33
            // If its an array and the key already esists, we can use array_merge
34 11
            if (is_array($newValue) && is_array($lessImportantValue)) {
35 2
                $newValue = array_merge($lessImportantValue, $newValue);
36 2
            }
37
38
            // The key is not set or the value is to be overwritten
39 11
            $theirs->set($key, $newValue, $item['metadata']);
40 16
        }
41
42 16
        return $theirs;
43
    }
44
}
45