Configuration   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 73
ccs 26
cts 26
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mergeConfig() 0 4 1
A setConfig() 0 4 1
A getConfig() 0 17 4
A getConfigByPath() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
class Configuration
18
{
19
    protected $config;
20
21
    /**
22
     * @param array $config
23
     */
24 2136
    public function __construct(array $config = [])
25
    {
26 2136
        $this->config = $config;
27 2136
    }
28
29
    /**
30
     * @param array $config
31
     */
32 1953
    public function mergeConfig(array $config = [])
33
    {
34 1953
        $this->config = array_replace_recursive($this->config, $config);
35 1953
    }
36
37
    /**
38
     * @param array $config
39
     */
40 6
    public function setConfig(array $config = [])
41
    {
42 6
        $this->config = $config;
43 6
    }
44
45
    /**
46
     * @param string|null $key
47
     * @param mixed       $default
48
     *
49
     * @return mixed
50
     */
51 2031
    public function getConfig($key = null, $default = null)
52
    {
53 2031
        if ($key === null) {
54 1953
            return $this->config;
55
        }
56
57
        // accept a/b/c as ['a']['b']['c']
58 2031
        if (strpos($key, '/')) {
59 1947
            return $this->getConfigByPath($key, $default);
60
        }
61
62 2031
        if (!isset($this->config[$key])) {
63 63
            return $default;
64
        }
65
66 2010
        return $this->config[$key];
67
    }
68
69
    /**
70
     * @param string      $keyPath
71
     * @param string|null $default
72
     *
73
     * @return mixed
74
     */
75 1947
    protected function getConfigByPath($keyPath, $default = null)
76
    {
77 1947
        $keyArr = explode('/', $keyPath);
78 1947
        $data = $this->config;
79 1947
        foreach ($keyArr as $k) {
80 1947
            if (!is_array($data) || !isset($data[$k])) {
81 6
                return $default;
82
            }
83
84 1947
            $data = $data[$k];
85 649
        }
86
87 1947
        return $data;
88
    }
89
}
90