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 ( c0c9c4...ad965f )
by Freek
01:40
created

Blink::stringContains()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 2
1
<?php
2
3
namespace Spatie\Blink;
4
5
use Countable;
6
use ArrayAccess;
7
8
class Blink implements ArrayAccess, Countable
9
{
10
    /** @var array */
11
    protected $values = [];
12
13
    /**
14
     * Put a value in the store.
15
     *
16
     * @param string|array $key
17
     * @param mixed $value
18
     *
19
     * @return $this
20
     */
21
    public function put($key, $value = null)
22
    {
23
        $newValues = $key;
24
25
        if (!is_array($key)) {
26
            $newValues = [$key => $value];
27
        }
28
29
        $this->values = array_merge($this->values, $newValues);
30
31
        return $this;
32
    }
33
34
    /**
35
     * Get a value from the store.
36
     *
37
     * This function has support for the '*' wildcard.
38
     *
39
     * @param string $key
40
     * @param mixed $default
41
     *
42
     * @return null|string|array
43
     */
44
    public function get(string $key, $default = null)
45
    {
46
        if ($this->stringContainsWildcard($key)) {
47
            $values = $this->getValuesForKeys($this->getKeysMatching($key));
48
49
            return count($values) ? $values : $default;
50
        }
51
52
        return $this->has($key)
53
            ? $this->values[$key]
54
            : $default;
55
    }
56
57
    /*
58
     * Determine if the store has a value for the given name.
59
     *
60
     * This function has support for the '*' wildcard.
61
     */
62
    public function has(string $key): bool
63
    {
64
        if ($this->stringContainsWildcard($key)) {
65
            return count($this->getKeysMatching($key)) > 0;
66
        }
67
68
        return array_key_exists($key, $this->values);
69
    }
70
71
    /**
72
     * Get all values from the store.
73
     *
74
     * @return array
75
     */
76
    public function all(): array
77
    {
78
        return $this->values;
79
    }
80
81
    /**
82
     * Forget a value from the store.
83
     *
84
     * This function has support for the '*' wildcard.
85
     *
86
     * @param string $key
87
     *
88
     * @return $this
89
     */
90
    public function forget(string $key)
91
    {
92
        $keys = $this->stringContainsWildcard('*')
93
            ? $this->getKeysMatching($key)
94
            : [$key];
95
96
        foreach ($keys as $key) {
97
            unset($this->values[$key]);
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * Flush all values from the store.
105
     *
106
     * @return $this
107
     */
108
    public function flush()
109
    {
110
        return $this->values = [];
111
    }
112
113
    /**
114
     * Get and forget a value from the store.
115
     *
116
     * This function has support for the '*' wildcard.
117
     *
118
     * @param string $key
119
     *
120
     * @return null|string
121
     */
122
    public function pull(string $key)
123
    {
124
        if ($this->stringContainsWildcard($key)) {
125
            $keys = $this->getKeysMatching($key);
126
127
            $values = $this->getValuesForKeys($keys);
128
129
            foreach($keys as $key) {
130
                $this->forget($key);
131
            }
132
133
            return $values;
134
        }
135
136
137
        $value = $this->get($key);
138
139
        $this->forget($key);
140
141
        return $value;
142
    }
143
144
    /**
145
     * Increment a value from the store.
146
     *
147
     * @param string $key
148
     * @param int $by
149
     *
150
     * @return int|null|string
151
     */
152
    public function increment(string $key, int $by = 1)
153
    {
154
        $currentValue = $this->get($key) ?? 0;
155
156
        $newValue = $currentValue + $by;
157
158
        $this->put($key, $newValue);
159
160
        return $newValue;
161
    }
162
163
    /**
164
     * Decrement a value from the store.
165
     *
166
     * @param string $key
167
     * @param int $by
168
     *
169
     * @return int|null|string
170
     */
171
    public function decrement(string $key, int $by = 1)
172
    {
173
        return $this->increment($key, $by * -1);
174
    }
175
176
    /**
177
     * Whether a offset exists.
178
     *
179
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
180
     *
181
     * @param mixed $offset
182
     *
183
     * @return bool
184
     */
185
    public function offsetExists($offset)
186
    {
187
        return $this->has($offset);
188
    }
189
190
    /**
191
     * Offset to retrieve.
192
     *
193
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
194
     *
195
     * @param mixed $offset
196
     *
197
     * @return mixed
198
     */
199
    public function offsetGet($offset)
200
    {
201
        return $this->get($offset);
202
    }
203
204
    /**
205
     * Offset to set.
206
     *
207
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
208
     *
209
     * @param mixed $offset
210
     * @param mixed $value
211
     */
212
    public function offsetSet($offset, $value)
213
    {
214
        $this->put($offset, $value);
215
    }
216
217
    /**
218
     * Offset to unset.
219
     *
220
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
221
     *
222
     * @param mixed $offset
223
     */
224
    public function offsetUnset($offset)
225
    {
226
        $this->forget($offset);
227
    }
228
229
    /**
230
     * Count elements.
231
     *
232
     * @link http://php.net/manual/en/countable.count.php
233
     *
234
     * @return int
235
     */
236
    public function count()
237
    {
238
        return count($this->all());
239
    }
240
241
    protected function filterKeysStartingWith(array $values, string $startsWith): array
242
    {
243
        return array_filter($values, function ($key) use ($startsWith) {
244
            return $this->startsWith($key, $startsWith);
245
        }, ARRAY_FILTER_USE_KEY);
246
    }
247
248
    protected function filterKeysNotStartingWith(array $values, string $startsWith): array
249
    {
250
        return array_filter($values, function ($key) use ($startsWith) {
251
            return !$this->startsWith($key, $startsWith);
252
        }, ARRAY_FILTER_USE_KEY);
253
    }
254
255
    protected function startsWith(string $haystack, string $needle): bool
256
    {
257
        return substr($haystack, 0, strlen($needle)) === $needle;
258
    }
259
260
    protected function getKeysMatching(string $pattern): array
261
    {
262
        $keys = array_keys($this->values);
263
264
        return array_filter($keys, function ($key) use ($pattern) {
265
            return fnmatch($pattern, $key);
266
        });
267
    }
268
269
    protected function stringContainsWildcard($string): bool
270
    {
271
        return $this->stringContains($string, '*');
272
    }
273
274
    protected function stringContains($haystack, $needles)
275
    {
276
        foreach ((array)$needles as $needle) {
277
            if ($needle != '' && mb_strpos($haystack, $needle) !== false) {
278
                return true;
279
            }
280
        }
281
282
        return false;
283
    }
284
285
    public function getValuesForKeys(array $keys)
286
    {
287
        return array_filter($this->values, function ($key) use ($keys) {
288
            return in_array($key, $keys);
289
        }, ARRAY_FILTER_USE_KEY);
290
    }
291
292
}
293