Configuration   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
eloc 31
dl 0
loc 103
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setByPath() 0 17 4
A set() 0 8 2
A merge() 0 3 1
A get() 0 16 4
A __construct() 0 3 1
A replace() 0 3 1
A getConfigByPath() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Configuration;
18
19
final class Configuration implements ConfigurationInterface
20
{
21
    /** @var array<string, mixed> */
22
    private $config;
23
24
    /**
25
     * @param array<string, mixed> $config
26
     */
27 3240
    public function __construct(array $config = [])
28
    {
29 3240
        $this->config = $config;
30 3240
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2901
    public function merge(array $config = []): void
36
    {
37 2901
        $this->config = \array_replace_recursive($this->config, $config);
38 2901
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 18
    public function replace(array $config = []): void
44
    {
45 18
        $this->config = $config;
46 18
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 3126
    public function get(?string $key = null, $default = null)
52
    {
53 3126
        if ($key === null) {
54 12
            return $this->config;
55
        }
56
57
        // accept a/b/c as ['a']['b']['c']
58 3126
        if (\strpos($key, '/')) {
59 1434
            return $this->getConfigByPath($key, $default);
60
        }
61
62 2976
        if (! isset($this->config[$key])) {
63 2916
            return $default;
64
        }
65
66 2949
        return $this->config[$key];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function set(string $key, $value = null): void
73
    {
74
        // accept a/b/c as ['a']['b']['c']
75 3
        if (\strpos($key, '/')) {
76 3
            $this->setByPath($key, $value);
77
        }
78
79 3
        $this->config[$key] = $value;
80 3
    }
81
82
    /**
83
     * @param mixed|null $default
84
     *
85
     * @return mixed|null
86
     */
87 1434
    private function getConfigByPath(string $keyPath, $default = null)
88
    {
89 1434
        $keyArr = \explode('/', $keyPath);
90 1434
        $data   = $this->config;
91 1434
        foreach ($keyArr as $k) {
92 1434
            if (! \is_array($data) || ! isset($data[$k])) {
93 531
                return $default;
94
            }
95
96 1302
            $data = $data[$k];
97
        }
98
99 1302
        return $data;
100
    }
101
102
    /**
103
     * @param mixed|null $value
104
     */
105 3
    private function setByPath(string $keyPath, $value = null): void
106
    {
107 3
        $keyArr  = \explode('/', $keyPath);
108 3
        $pointer = &$this->config;
109 3
        while (($k = \array_shift($keyArr)) !== null) {
110 3
            if (! \is_array($pointer)) {
111 3
                $pointer = [];
112
            }
113
114 3
            if (! isset($pointer[$k])) {
115 3
                $pointer[$k] = null;
116
            }
117
118 3
            $pointer = &$pointer[$k];
119
        }
120
121 3
        $pointer = $value;
122 3
    }
123
}
124