Passed
Push — v3 ( 37cdd6...e623f3 )
by
unknown
29:19 queued 26:35
created

Arr::shift()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Helpers;
20
21
use Closure;
22
use RecursiveArrayIterator;
23
use RecursiveIteratorIterator;
24
25
/**
26
 * This helper contains functions to work with arrays.
27
 *
28
 * @package GameQ\Helpers
29
 */
30
class Arr
31
{
32
    use Arr\Recursively;
33
34
    /**
35
     * This helper does process each element of the provided array recursively.
36
     * It does so allowing for modifications to the provided array and without
37
     * using actual recursive calls.
38
     *
39
     * @param array $data
40
     * @param Closure $callback
41
     *
42
     * @return array
43
     */
44 36
    public static function recursively(array $data, Closure $callback)
45
    {
46
        /* Initialize the RecursiveArrayIterator for the provided data */
47 36
        $arrayIterator = new RecursiveArrayIterator($data);
48
49
        /* Configure the Iterator for the RecursiveIterator */
50 36
        $recursiveIterator = new RecursiveIteratorIterator($arrayIterator);
51
52
        /* Traverse the provided data */
53 36
        foreach ($recursiveIterator as $key => $value) {
54
            /* Get the current sub iterator with Type hinting */
55
            /** @var RecursiveArrayIterator */
56 36
            $subIterator = $recursiveIterator->getSubIterator();
57
58
            /* Wrap the implementation to handle PHP < 8.1 behaviour */
59 36
            static::handleArrayIteratorCopyOrReference(
60 36
                $data,
61 36
                $recursiveIterator,
62 36
                $subIterator,
63 30
                function () use ($callback, &$value, $key, $subIterator) {
64
                    /* Execute the callback */
65 36
                    $callback($value, $key, $subIterator);
66
67
                    /* Update the modified value */
68 36
                    $subIterator->offsetSet($key, $value);
0 ignored issues
show
Bug introduced by
The method offsetSet() does not exist on RecursiveIterator. It seems like you code against a sub-type of RecursiveIterator such as Phar or RecursiveCachingIterator or SimpleXMLElement or RecursiveArrayIterator or SimpleXMLIterator or Phar. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                    $subIterator->/** @scrutinizer ignore-call */ 
69
                                  offsetSet($key, $value);
Loading history...
69 36
                }
70 24
            );
71 6
        }
72
73
        /* Return the processed data */
74 36
        return static::getArrayIteratorCopyOrReference($data, $arrayIterator);
75
    }
76
77
    /**
78
     * This helper is intended to hash the provided array's values
79
     * and return it back as key => hash.
80
     *
81
     * @param array $array
82
     * @return array<string|int, string>
83
     */
84 30
    public static function hashes(array $array)
85
    {
86 30
        $hashes = [];
87
88
        /* Process the provided array */
89 30
        foreach ($array as $key => $value) {
90
            /* Serialze and hash each value individually */
91 30
            $hashes[$key] = md5(serialize($value));
92 6
        }
93
94
        /* Return array containing the hashes */
95 30
        return $hashes;
96
    }
97
98
    /**
99
     * This helper is intended to set a value inside the provided array.
100
     *
101
     * @param array &$array
102
     * @param array $path
103
     * @param mixed $value
104
     * @return array
105
     */
106 30
    public static function set(array &$array, array $path, $value)
107
    {
108 30
        $current = &$array;
109
110
        /* Process the path until the last element */
111 30
        foreach ($path as $i => $element) {
112
            /* Remove the element from the path */
113 30
            unset($path[$i]);
114
115
            /* Create missing key */
116 30
            if (! isset($current[$element])) {
117 20
                $current[$element] = [];
118 4
            }
119
120
            /* Set current to a reference of next */
121 30
            $current = &$current[$element];
122 6
        }
123
124
        /* Finally set the value using the last key */
125 30
        $current = $value;
126
127
        /* Return the current, modified array (level) */
128 30
        return $array;
129
    }
130
131
    /**
132
     * This helper method is intended to shift the provided arguments to the left.
133
     *
134
     * **Example:** foo, bar, baz becomes bar, baz, baz
135
     *
136
     * @param mixed &...$args
137
     * @return void
138
     */
139 12
    public static function shift(&...$args)
140
    {
141
        /* Get the array keys to ensure numeric index */
142 12
        $keys = array_keys($args);
143
144
        /* Iterate the provided arguments keys in order */
145 12
        foreach ($keys as $i => $key) {
146
            /* Process until the last argument */
147 12
            if ($i < count($keys) - 1) {
148
                /* Shift next into current */
149 12
                $args[$key] = $args[$keys[$i + 1]];
150 2
            }
151 2
        }
152 6
    }
153
}
154