GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 86ad7b...a9edc5 )
by Glenn
02:36 queued 38s
created

Manager::setArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace Glenn\Config;
4
5
use ArrayAccess;
6
7
use Glenn\Config\Contracts\ManagerContract;
8
use Glenn\Config\Contracts\ParserContract;
9
10
class Manager implements ArrayAccess, ManagerContract
11
{
12
    /**
13
     * All of the configuration items.
14
     *
15
     * @var array
16
     */
17
    protected $items = [];
18
19
    /**
20
     * Construct a new Config Manager pre-loaded with items.
21
     *
22
     * @param array $items
23
     *
24
     * @author Glenn McEwan <[email protected]>
25
     */
26
    public function __construct(array $items = [])
27
    {
28
        $this->items = $items;
29
    }
30
31
    /**
32
     * Check if a key exists in the config.
33
     *
34
     * @param string $key The key in the config to check for existence
35
     *
36
     * @return bool
37
     *
38
     * @author Glenn McEwan <[email protected]>
39
     */
40
    public function has($key)
41
    {
42
        $items = $this->all();
43
44
        if (empty($items) || is_null($key)) {
45
            return false;
46
        }
47
48
        if (array_key_exists($key, $items)) {
49
            return true;
50
        }
51
52 View Code Duplication
        foreach (explode('.', $key) as $segment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            if (!is_array($items) || !array_key_exists($segment, $items)) {
54
                return false;
55
            }
56
57
            $items = $items[$segment];
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * Retrieve a given config value by its key.
65
     *
66
     * @param string $key     Config key
67
     * @param mixed  $default Default value if the key doesn't exist
68
     *
69
     * @return mixed The config value
70
     *
71
     * @author Glenn McEwan <[email protected]>
72
     */
73
    public function get($key, $default = null)
74
    {
75
        $items = $this->all();
76
77
        if (array_key_exists($key, $items)) {
78
            return $items[$key];
79
        }
80
81 View Code Duplication
        foreach (explode('.', $key) as $segment) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            if (!is_array($items) || !array_key_exists($segment, $items)) {
83
                return $default;
84
            }
85
86
            $items = $items[$segment];
87
        }
88
89
        return $items;
90
    }
91
92
    /**
93
     * Retrieve all of the config items.
94
     *
95
     * @return array
96
     *
97
     * @author Glenn McEwan <[email protected]>
98
     */
99
    public function all()
100
    {
101
        return $this->items;
102
    }
103
104
    /**
105
     * Set a config entry by key, optional value.
106
     *
107
     * @param string $key   Config key
108
     * @param mixed  $value Config value
109
     *
110
     * @return static return self / $this for chain-ability
111
     *
112
     * @author Glenn McEwan <[email protected]>
113
     */
114
    public function set($key, $value = null)
115
    {
116
        $items = &$this->items;
117
118
        $keys = explode('.', $key);
119
120
        foreach ($keys as $key) {
121
            if (!isset($items[$key]) || !is_array($items[$key])) {
122
                $items[$key] = [];
123
            }
124
125
            $items = &$items[$key];
126
        }
127
128
        $items = $value;
129
130
        return $this;
131
    }
132
133
    /**
134
     * If a parent key is passed, for example, as 'deployment',
135
     * then the data will be put in to the Config as deployment.*,
136
     * otherwise it will be placed at the root level in the config.
137
     *
138
     * Optionally, a parent key in which the config data will reside.
139
     *
140
     * @param mixed  $value The array to set in to the config
141
     * @param string $key   [optional] A parent config key to set the array in to
142
     *
143
     * @author Glenn McEwan <[email protected]>
144
     */
145
    public function setArray($value, $key = null)
146
    {
147
        if ($key !== null) {
148
            $this->set($key, $value);
149
        } else {
150
            foreach ($value as $head => $node) {
151
                $this->set($head, $node);
152
            }
153
        }
154
155
        return $this;
156
    }
157
158
    /*
159
    |--------------------------------------------------------------------------
160
    | Interface - ArrayAccess
161
    |--------------------------------------------------------------------------
162
    |
163
    */
164
165
    /**
166
     * Check if a key exists in the config.
167
     *
168
     * @param string $key The key in the config to check for existence
169
     *
170
     * @return bool
171
     *
172
     * @author Glenn McEwan <[email protected]>
173
     */
174
    public function offsetExists($key)
175
    {
176
        return $this->has($key);
177
    }
178
179
    /**
180
     * Retrieve a given config value by its key.
181
     *
182
     * @param string $key Config key
183
     *
184
     * @return mixed The config value
185
     *
186
     * @author Glenn McEwan <[email protected]>
187
     */
188
    public function offsetGet($key)
189
    {
190
        return $this->get($key);
191
    }
192
193
    /**
194
     * Set a config entry by key, optional value.
195
     *
196
     * @param string $key   Config key
197
     * @param mixed  $value Config value
198
     *
199
     * @author Glenn McEwan <[email protected]>
200
     */
201
    public function offsetSet($key, $value)
202
    {
203
        $this->set($key, $value);
204
    }
205
206
    /**
207
     * Unset a configuration option.
208
     *
209
     * @param string $key
210
     *
211
     * @author Glenn McEwan <[email protected]>
212
     */
213
    public function offsetUnset($key)
214
    {
215
        $this->set($key, null);
216
    }
217
}
218