Passed
Pull Request — master (#14)
by
unknown
08:37
created

ConfigCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Config;
5
6
class ConfigCollection implements ConfigInterface
7
{
8
    /** @var ConfigInterface[] */
9
    private $configs;
0 ignored issues
show
Coding Style introduced by
Private member variable "configs" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
10
11
    public function __construct(ConfigInterface ...$configs)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
12
    {
13
        $this->configs = $configs;
14
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
15
16
    public function get(string $dotPath, $default = null)
17
    {
18
        return array_reduce(array_reverse($this->configs), $this->reducer($dotPath), $default);
19
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
20
21
    public function set(string $dotPath, $value, $hard = false)
22
    {
23
        $this->configs[0]->set($dotPath, $value, $hard);
24
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
25
26
    private function reducer(string $dotPath): callable
27
    {
28
        return static function ($currentValue, ConfigInterface $config) use ($dotPath) {
29
            $found = $config->get($dotPath, null);
30
31
            if ($found === null) {
32
                return $currentValue;
33
            }
34
35
            if (is_array($currentValue) && is_array($found)) {
36
                return array_replace_recursive($currentValue, $found);
37
            }
38
39
            return $found;
40
        };
41
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
42
}
43