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.

AbstractResource::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @package     m1/vars
12
 * @version     1.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars\Resource;
20
21
/**
22
 * Abstract Resource enables normal and dot notation array access on resources
23
 *
24
 * @since 0.1.0
25
 */
26
abstract class AbstractResource implements \ArrayAccess
27
{
28
    /**
29
     * The resource content
30
     *
31
     * @var array
32
     */
33
    protected $content = array();
34
35
    /**
36
     * Sets the resource contents
37
     *
38
     * @param array $content
39
     *
40
     * @return $this
41
     */
42 1
    public function setContent($content)
43
    {
44 1
        $this->content = $content;
45 1
        return $this;
46
    }
47
48
    /**
49
     * Returns the content of the resource
50
     *
51
     * @return array The content
52
     */
53 66
    public function getContent()
54
    {
55 66
        return $this->content;
56
    }
57
58
    /**
59
     * Normal `$example[$key]` access for the array
60
     *
61
     * @param mixed $key The key to get the value for
62
     *
63
     * @return array|bool|null The resource key value
64
     */
65 4
    public function offsetGet($key)
66
    {
67 4
        return $this->internalGet($this->content, $key);
68
    }
69
70
    /**
71
     * Object oriented get access for the array
72
     *
73
     * @param mixed $key The key to get the value for
74
     *
75
     * @return array|bool|null The resource key value
76
     */
77 9
    public function get($key)
78
    {
79 9
        return $this->internalGet($this->content, $key);
80
    }
81
82
    /**
83
     * The internal get function for getting values by their key
84
     *
85
     * @param array $array  The array to use -- will always be $this->content
86
     * @param mixed $key    The key to find the value for
87
     * @param bool  $exists Whether to return null or false dependant on the calling function
88
     *
89
     * @return array|bool|null The resource key value
90
     */
91 9
    private function internalGet(array $array, $key, $exists = false)
92
    {
93 9
        if (isset($array[$key])) {
94 4
            return (!$exists) ? $array[$key] : true;
95
        }
96
97 5
        $parts = explode('.', $key);
98
99 5
        foreach ($parts as $part) {
100 5
            if (!is_array($array) || !isset($array[$part])) {
101 2
                return (!$exists) ? null : false;
102
            }
103
104 4
            $array = $array[$part];
105
        }
106
107 3
        return (!$exists) ? $array : true;
108
    }
109
110
    /**
111
     * Normal `$example[$key] = 'hello'` access for the array
112
     *
113
     * @param mixed $key The key to set the value for
114
     * @param mixed $value The value to set
115
     */
116 2
    public function offsetSet($key, $value)
117
    {
118 2
        $this->internalSet($this->content, $key, $value);
119 2
    }
120
121
    /**
122
     * Object oriented set access for the array
123
     *
124
     * @param string $key The key to set the value for
125
     * @param string $value The value to set
126
     */
127 57
    public function set($key, $value)
128
    {
129 57
        $this->internalSet($this->content, $key, $value);
130 57
    }
131
132
    /**
133
     * Object oriented set access for the array
134
     *
135
     * @param array $array  The array to use -- will always be based on $this->content but can be used recursively
136
     * @param mixed $key    The key to set the value for
137
     * @param mixed $value  The value to set
138
     *
139
     * @return array Returns the modified array
140
     */
141 57
    private function internalSet(array &$array, $key, $value)
142
    {
143 57
        if (is_null($key)) {
144 1
            return $array = $value;
145
        }
146
147 57
        $keys = explode('.', $key);
148
149 57
        while (count($keys) > 1) {
150 15
            $key = array_shift($keys);
151
152 15
            if (! isset($array[$key]) || ! is_array($array[$key])) {
153 15
                $array[$key] = [];
154
            }
155
156 15
            $array = &$array[$key];
157
        }
158
159 57
        $array[array_shift($keys)] = $value;
160
161 57
        return $array;
162
    }
163
164
    /**
165
     * Checks whether the key exists
166
     *
167
     * @param mixed $key The key to check
168
     *
169
     * @return bool Does the key exist
170
     */
171 2
    public function offsetExists($key)
172
    {
173 2
        return $this->internalGet($this->content, $key, true);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->internalGet($this->content, $key, true); of type array|boolean|null adds the type array to the return on line 173 which is incompatible with the return type declared by the interface ArrayAccess::offsetExists of type boolean.
Loading history...
174
    }
175
176
    /**
177
     * Unsets the key
178
     *
179
     * @param mixed $key The key to unset
180
     */
181 2
    public function offsetUnset($key)
182
    {
183 2
        $this->internalUnset($this->content, $key);
184 2
    }
185
186
    /**
187
     * Internal unset for the key
188
     *
189
     * @param array $array The array to use -- will always be based on $this->content but can be used recursively
190
     * @param mixed $key The key to unset
191
     */
192 2
    protected function internalUnset(array &$array, $key)
193
    {
194 2
        $parts = explode(".", $key);
195
196 2
        while (count($parts) > 1) {
197 1
            $part = array_shift($parts);
198
199 1
            if (isset($array[$part]) && is_array($array[$part])) {
200 1
                $array =& $array[$part];
201
            }
202
        }
203
204 2
        unset($array[array_shift($parts)]);
205 2
    }
206
207
    /**
208
     * Port of array_key_exists to \ArrayAccess
209
     *
210
     * @param mixed $key The key to check if exists
211
     *
212
     * @return bool Does the key exist
213
     */
214 4
    public function arrayKeyExists($key)
215
    {
216 4
        if (array_key_exists($key, $this->content)) {
217 2
            return true;
218
        }
219
220 2
        $parts = explode('.', $key);
221 2
        $arr = $this->content;
222 2
        foreach ($parts as $part) {
223 2
            if (!is_array($arr) || !array_key_exists($part, $arr)) {
224 1
                return false;
225
            }
226
227 1
            $arr = $arr[$part];
228
        }
229
230 1
        return true;
231
    }
232
}
233