Completed
Push — master ( 495169...b31514 )
by Changwan
10:11
created

Config::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Config;
3
4
use InvalidArgumentException;
5
use Wandu\Config\Contracts\Config as ConfigContract;
6
use Wandu\Config\Contracts\Loader;
7
use Wandu\Config\Exception\CannotLoadException;
8
use Wandu\Config\Exception\NotAllowedMethodException;
9
10
class Config implements ConfigContract
11
{
12
    /** @var array */
13
    protected $items;
14
    
15
    /** @var \Wandu\Config\Contracts\Loader[] */
16
    protected $loaders = [];
17
18
    /**
19
     * @param array $items
20
     */
21 22
    public function __construct(array $items = [])
22
    {
23 22
        $this->items = $items;
24 22
    }
25
26
    /**
27
     * @param string $name
28
     * @return \Wandu\Config\Contracts\Config|mixed
29
     */
30 1
    public function __get($name)
31
    {
32 1
        $subset = $this->get($name);
33 1
        if (!is_array($subset)) {
34 1
            return $subset;
35
        }
36 1
        return new static($subset);
37
    }
38
39
    /**
40
     * @param \Wandu\Config\Contracts\Loader $loader
41
     */
42 2
    public function pushLoader(Loader $loader)
43
    {
44 2
        $this->loaders[] = $loader;
45 2
    }
46
47
    /**
48
     * @param string $path
49
     */
50 2
    public function load(string $path)
51
    {
52 2
        foreach ($this->loaders as $loader) {
53 2
            if ($loader->test($path)) {
54 2
                $this->merge($loader->load($path));
0 ignored issues
show
Bug introduced by
It seems like $loader->load($path) targeting Wandu\Config\Contracts\Loader::load() can also be of type null; however, Wandu\Config\Config::merge() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55 2
                return;
56
            }
57
        }
58
        throw new CannotLoadException($path);
59
    }
60
61
    /**
62
     * @param array $appender
63
     */
64 4
    public function merge(array $appender)
65
    {
66 4
        $this->items = $this->recursiveMerge($this->items, $appender);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->recursiveMerge($this->items, $appender) of type * is incompatible with the declared type array of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
67 4
    }
68
69
    /**
70
     * @param mixed $origin
71
     * @param mixed $appender
72
     * @return mixed
73
     */
74 4
    private function recursiveMerge($origin, $appender)
75
    {
76 4
        if (is_array($origin)
77 4
            && array_values($origin) !== $origin
78 4
            && is_array($appender)
79 4
            && array_values($appender) !== $appender) {
80 4
            foreach ($appender as $key => $value) {
81 4
                if (isset($origin[$key])) {
82 2
                    $origin[$key] = $this->recursiveMerge($origin[$key], $value);
83
                } else {
84 4
                    $origin[$key] = $value;
85
                }
86
            }
87 4
            return $origin;
88
        }
89 4
        return $appender;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 4
    public function toArray(): array
96
    {
97 4
        return $this->items ?? [];
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    public function has($name): bool
104
    {
105 1
        if ($name === '') {
106
            return true;
107
        }
108 1
        $names = explode('.', $name);
109 1
        $dataToReturn = $this->items;
110 1
        while (count($names)) {
111 1
            $name = array_shift($names);
112 1
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
113 1
                return false;
114
            }
115 1
            $dataToReturn = $dataToReturn[$name];
116
        }
117 1
        return true;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 12
    public function get($name, $default = null)
124
    {
125 12
        if ($name === '') {
126 1
            return $this->items;
127
        }
128 12
        $names = explode('.', $name);
129 12
        $dataToReturn = $this->items;
130 12
        while (count($names)) {
131 12
            $name = array_shift($names);
132 12
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
133 7
                return $default;
134
            }
135 10
            $dataToReturn = $dataToReturn[$name];
136
        }
137 10
        return $dataToReturn;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 1
    public function subset($name): ConfigContract
144
    {
145 1
        $subset = $this->get($name);
146 1
        if (!is_array($subset)) {
147 1
            throw new InvalidArgumentException('subset must be an array.');
148
        }
149 1
        return new static($subset);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function offsetExists($offset)
156
    {
157
        return $this->has($offset);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 3
    public function offsetGet($offset)
164
    {
165 3
        if (strpos($offset, '||') !== false) {
166 1
            list($offset, $default) = explode('||', $offset);
167 1
            return $this->get($offset, $default);
168
        }
169 3
        return $this->get($offset);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function offsetSet($offset, $value)
176
    {
177 1
        throw new NotAllowedMethodException(__FUNCTION__, __CLASS__);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function offsetUnset($offset)
184
    {
185 1
        throw new NotAllowedMethodException(__FUNCTION__, __CLASS__);
186
    }
187
}
188