ComposerConfigMerger   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 97
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A merge() 0 4 1
A cleanConfig() 0 4 1
A filter() 0 6 2
A filterArray() 0 10 3
A unsetNotValidKeys() 0 9 3
A mergeConfig() 0 15 3
1
<?php
2
namespace Samurai\Project\Composer;
3
4
/**
5
 * Class ComposerConfigMerger
6
 * @package Samurai\Project\Composer
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class ComposerConfigMerger 
10
{
11
    /**
12
     * @var string[]
13
     */
14
    private static $notValidKeys = [
15
        'version',
16
        'time',
17
    ];
18
19
    /**
20
     * @var string[]
21
     */
22
    private static $sharedKeys = [
23
        'autoload',
24
    ];
25
26
    /**
27
     * @param array $initialConfig
28
     * @param array $updatedConfig
29
     * @return array
30
     */
31 5
    public function merge(array $initialConfig, array $updatedConfig)
32
    {
33 5
        return $this->cleanConfig($this->mergeConfig($initialConfig, $updatedConfig));
34
    }
35
36
    /**
37
     * @param array $config
38
     * @return array
39
     */
40 5
    private function cleanConfig(array $config)
41
    {
42 5
        return $this->filterArray($this->unsetNotValidKeys($config));
43
    }
44
45
    /**
46
     * @param mixed $value
47
     * @return mixed
48
     */
49 5
    private function filter($value){
50 5
        if(is_array($value)){
51 5
            return $this->filterArray($value);
52
        }
53 5
        return $value;
54
    }
55
56
    /**
57
     * @param array $values
58
     * @return array
59
     */
60 5
    private function filterArray(array $values)
61
    {
62 5
        $result = [];
63 5
        foreach ($values as $key => $value) {
64 5
            if ($this->filter($value)) {
65 3
                $result[$key] = $value;
66 3
            }
67 5
        }
68 5
        return $result;
69
    }
70
71
    /**
72
     * @param array $config
73
     * @return array
74
     */
75 5
    private function unsetNotValidKeys(array $config)
76
    {
77 5
        foreach(self::$notValidKeys as $key){
78 5
            if (array_key_exists($key, $config)) {
79 1
                unset($config[$key]);
80 1
            }
81 5
        }
82 5
        return $config;
83
    }
84
85
    /**
86
     * @param array $initialConfig
87
     * @param array $updatedConfig
88
     * @return array
89
     */
90 5
    private function mergeConfig(array $initialConfig, array $updatedConfig)
91
    {
92 5
        $updatedConfig = array_merge($initialConfig, $updatedConfig);
93
94 5
        foreach(self::$sharedKeys as $key){
95 5
            if (isset($initialConfig[$key])) {
96 1
                $updatedConfig[$key] = array_merge_recursive(
97 1
                    $initialConfig[$key],
98 1
                    $updatedConfig[$key]
99 1
                );
100 1
            }
101 5
        }
102
103 5
        return $updatedConfig;
104
    }
105
}
106