Passed
Push — master ( 09050d...6a0831 )
by Бабичев
04:00
created

Arr::shuffle()   A

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
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bavix\Helpers;
4
5
use Bavix\Exceptions;
6
use Bavix\Exceptions\NotFound;
7
8
class Arr
9
{
10
11
    /**
12
     * @param array    $storage
13
     * @param callable $callback
14
     *
15
     * @return bool
16
     */
17 1
    public static function walkRecursive(array &$storage, callable $callback)
18
    {
19 1
        return \array_walk_recursive($storage, $callback);
20
    }
21
22
    /**
23
     * @param array    $storage
24
     * @param callable $callback
25
     *
26
     * @return array
27
     */
28 1
    public static function map(array $storage, $callback)
29
    {
30 1
        return \array_map($callback, $storage);
31
    }
32
33
    /**
34
     * @param array $storage
35
     * @param int   $offset
36
     * @param int   $length
37
     *
38
     * @return array
39
     */
40 1
    public static function slice(array $storage, $offset, $length = null)
41
    {
42 1
        return \array_slice($storage, $offset, $length);
43
    }
44
45
    /**
46
     * @param array   $first
47
     * @param array[] ...$second
48
     *
49
     * @return array
50
     */
51 1
    public static function merge(array $first, array ...$second)
52
    {
53 1
        return \array_merge($first, ...$second);
54
    }
55
56
    /**
57
     * @param array    $storage
58
     * @param callable $callback
59
     *
60
     * @return array
61
     */
62 1
    public static function filter(array $storage, $callback)
63
    {
64 1
        return \array_filter($storage, $callback, ARRAY_FILTER_USE_BOTH);
65
    }
66
67
    /**
68
     * @param     $string
69
     * @param     $length
70
     * @param int $start
71
     *
72
     * @return array
73
     */
74 1
    public static function fill($string, $length, $start = 0)
75
    {
76 1
        return \array_fill($start, $length, $string);
77
    }
78
79
    /**
80
     * @param mixed $begin
81
     * @param mixed $end
82
     * @param int   $step
83
     *
84
     * @return array
85
     */
86 1
    public static function range($begin, $end, $step = 1)
87
    {
88 1
        return \range($begin, $end, $step);
89
    }
90
91
    /**
92
     * @param array $storage
93
     * @param mixed $needle
94
     * @param bool  $strict
95
     *
96
     * @return bool
97
     */
98 1
    public static function in(array $storage, $needle, $strict = true)
99
    {
100 1
        return \in_array($needle, $storage, $strict);
101
    }
102
103
    /**
104
     * @param array $storage
105
     *
106
     * @return bool
107
     */
108 1
    public static function shuffle(array &$storage)
109
    {
110 1
        return \shuffle($storage);
111
    }
112
113
    /**
114
     * @param array  $storage
115
     * @param string $path
116
     * @param mixed  $value
117
     */
118 1
    public static function set(array &$storage, $path, $value)
119
    {
120 1
        $rows           = &static::link($storage, $path, $lastKey);
121 1
        $rows[$lastKey] = $value;
122 1
    }
123
124
    /**
125
     * @param array  $storage
126
     * @param string $path
127
     */
128 1
    public static function remove(array &$storage, $path)
129
    {
130 1
        $rows = &static::link($storage, $path, $lastKey);
131 1
        unset($rows[$lastKey]);
132 1
    }
133
134
    /**
135
     * @param array $storage
136
     * @param string $key
137
     *
138
     * @return array|mixed
139
     *
140
     * @throws NotFound\Path
141
     * @throws Exceptions\Blank
142
     */
143 3
    public static function getRequired(array $storage, $key)
144
    {
145 3
        return static::findPath($storage, static::keys($key));
146
    }
147
148
    /**
149
     * @param array $storage
150
     * @param array $keys
151
     *
152
     * @return array|mixed
153
     *
154
     * @throws NotFound\Path
155
     * @throws Exceptions\Blank
156
     */
157 6
    protected static function findPath(array $storage, array $keys)
158
    {
159 6
        if (!count($keys) || $keys[0] === '')
160
        {
161 1
            throw new Exceptions\Blank('Not found keys');
162
        }
163
164 5
        $rows = &$storage;
165
166 5
        foreach ($keys as $key)
167
        {
168 5
            if (!static::keyExists($rows, $key))
169
            {
170 2
                throw new NotFound\Path('Path `' . implode('.', $keys) . '` not found');
171
            }
172
173 4
            $rows = &$rows[$key];
174
        }
175
176 4
        return $rows;
177
    }
178
179
    /**
180
     * @param array $storage
181
     * @param       $mixed
182
     *
183
     * @return int
184
     */
185 1
    public static function push(array &$storage, $mixed)
186
    {
187 1
        return \array_push($storage, $mixed);
188
    }
189
190
    /**
191
     * @param array $storage
192
     *
193
     * @return mixed
194
     */
195 1
    public static function pop(array &$storage)
196
    {
197 1
        return \array_pop($storage);
198
    }
199
200
    /**
201
     * @param array $storage
202
     *
203
     * @return mixed
204
     */
205 1
    public static function shift(array &$storage)
206
    {
207 1
        return \array_shift($storage);
208
    }
209
210
    /**
211
     * @param array $storage
212
     * @param       $mixed
213
     *
214
     * @return int
215
     */
216 1
    public static function unShift(array &$storage, $mixed)
217
    {
218 1
        return \array_unshift($storage, $mixed);
219
    }
220
221
    /**
222
     * @param array  $storage
223
     * @param string $key
224
     * @param mixed  $default
225
     *
226
     * @return mixed
227
     */
228 3
    public static function get(array $storage, $key, $default = null)
229
    {
230
        try
231
        {
232 3
            return static::findPath($storage, static::keys($key));
233
        }
234 1
        catch (\Throwable $throwable)
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
235
        {
236 1
            return $default;
237
        }
238
    }
239
240
    /**
241
     * @param \Traversable $iterator
242
     *
243
     * @return array
244
     */
245 1
    public static function iterator($iterator)
246
    {
247 1
        return \iterator_to_array($iterator);
248
    }
249
    
250
    /**
251
     * @param array $storage
252
     *
253
     * @return array
254
     */
255 1
    public static function getKeys(array $storage)
256
    {
257 1
        return \array_keys($storage);
258
    }
259
260
    /**
261
     * @param string $offset
262
     *
263
     * @return array
264
     */
265 7
    public static function keys($offset)
266
    {
267 7
        $offset = \trim($offset);
268 7
        $offset = \preg_replace('~\[(?<s>[\'"]?)(.*?)(\k<s>)\]~u', '.$2', $offset);
269
270 7
        return \explode('.', $offset);
271
    }
272
273
    /**
274
     * @param array  $storage
275
     * @param string $key
276
     *
277
     * @return bool
278
     */
279 5
    public static function keyExists(array $storage, $key)
280
    {
281 5
        return isset($storage[$key]) || \array_key_exists($key, $storage);
282
    }
283
284
    /**
285
     * @param array  $storage
286
     * @param string $key
287
     *
288
     * @return mixed
289
     */
290 1
    public static function at(array $storage, $key)
291
    {
292 1
        return $storage[$key];
293
    }
294
295
    /**
296
     * @param array  $storage
297
     * @param string $key
298
     * @param int    $value
299
     */
300 1
    public static function initOrPush(array &$storage, $key, $value)
301
    {
302 1
        if (empty($storage[$key]))
303
        {
304 1
            $storage[$key] = [];
305
        }
306
307 1
        $storage[$key][] = $value;
308 1
    }
309
310
    /**
311
     * @param array  $storage
312
     * @param string $path
313
     * @param string $lastKey
314
     *
315
     * @return array
316
     */
317 2
    protected static function &link(array &$storage, $path, &$lastKey)
318
    {
319 2
        $keys    = static::keys($path);
320 2
        $lastKey = \array_pop($keys);
321 2
        $rows    = &$storage;
322
323 2
        foreach ($keys as $key)
324
        {
325 1
            if (!static::keyExists($rows, $key))
326
            {
327 1
                $rows[$key] = [];
328
            }
329 1
            $rows = &$rows[$key];
330
        }
331
332 2
        return $rows;
333
    }
334
335
}
336