Config::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 18
    public function __construct(array $items = [])
22
    {
23 18
        $this->items = $items;
24 18
    }
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 3
    public function pushLoader(Loader $loader)
43
    {
44 3
        $this->loaders[] = $loader;
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3
    public function load(string $path)
51
    {
52 3
        foreach ($this->loaders as $loader) {
53 3
            if ($loader->test($path)) {
54 3
                $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 3
                return;
56
            }
57
        }
58
        throw new CannotLoadException($path);
59
    }
60
61
    /**
62
     * @param array $appender
63
     */
64 5
    public function merge(array $appender)
65
    {
66 5
        $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 5
    }
68
69
    /**
70
     * @param mixed $origin
71
     * @param mixed $appender
72
     * @return mixed
73
     */
74 5
    private function recursiveMerge($origin, $appender)
75
    {
76 5
        if (is_array($origin)
77 5
            && array_values($origin) !== $origin
78 5
            && is_array($appender)
79 5
            && 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 5
        return $appender;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 5
    public function toArray(): array
96
    {
97 5
        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 9
    public function get($name, $default = null)
124
    {
125 9
        if ($name === '') {
126 1
            return $this->items;
127
        }
128 9
        $names = explode('.', $name);
129 9
        $dataToReturn = $this->items;
130 9
        while (count($names)) {
131 9
            $name = array_shift($names);
132 9
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
133 6
                return $default;
134
            }
135 8
            $dataToReturn = $dataToReturn[$name];
136
        }
137 8
        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