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.

ArrayUtils   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 38
lcom 0
cbo 1
dl 0
loc 269
ccs 104
cts 104
cp 1
rs 8.3999
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A explodeArrayOnKeys() 0 14 4
B fillMissingKeys() 0 22 5
A filterArrayOnKey() 0 10 1
A flattenArray() 0 14 3
A prefixArray() 0 12 1
A isAssociativeArray() 0 4 1
A isIndexedArray() 0 4 1
B calculateArrayDepth() 0 19 5
C mergeRecursive() 0 23 8
A hasAllKeys() 0 10 3
A hasAnyKey() 0 10 3
A toUnderscore() 0 13 3
1
<?php
2
3
namespace WebservicesNl\Utils;
4
5
/**
6
 * Class ArrayUtils.
7
 */
8
class ArrayUtils
9
{
10
    /**
11
     * Explode an array on string.
12
     *
13
     * @param array  $array
14
     * @param string $explodeOn
15
     * @param int    $limit     |null
0 ignored issues
show
Documentation introduced by
Should the type for parameter $limit not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
16
     *
17
     * @return array
18
     */
19 1
    public static function explodeArrayOnKeys($array, $explodeOn, $limit = null)
20
    {
21 1
        $result = [];
22 1
        foreach ($array as $path => $value) {
23 1
            $temp = &$result;
24 1
            $tempArray = ($limit === null) ? explode($explodeOn, $path) : explode($explodeOn, $path, $limit);
25 1
            foreach ($tempArray as $key) {
26 1
                $temp = &$temp[$key];
27 1
            }
28 1
            $temp = $value;
29 1
        }
30
31 1
        return $result;
32
    }
33
34
    /**
35
     * Fill the array adding the missing keys from 0 to the max key, setting values to $value, and orders it.
36
     *
37
     * @param array  $array
38
     * @param string $value
39
     *
40
     * @return array
41
     */
42 3
    public static function fillMissingKeys(array $array, $value = '')
43
    {
44 3
        if (count($array) === 0 || self::isAssociativeArray($array) === true) {
45 2
            return $array;
46
        }
47
48
        // sort and reset pointer to first element
49 1
        ksort($array);
50 1
        $min = key($array);
51 1
        end($array);
52 1
        $max = key($array);
53
54 1
        for ($i = $min; $i < $max; ++$i) {
55 1
            if (array_key_exists($i, $array) === false) {
56 1
                $array[$i] = $value;
57 1
            }
58 1
        }
59
60 1
        ksort($array);
61
62 1
        return $array;
63
    }
64
65
    /**
66
     * Filter an array key.
67
     *
68
     * @param array  $array
69
     * @param string $value
70
     *
71
     * @return array
72
     */
73 1
    public static function filterArrayOnKey(array $array, $value)
74
    {
75 1
        return array_filter(
76 1
            $array,
77
            function ($key) use ($value) {
78 1
                return strpos($key, $value) === 0;
79 1
            },
80
            ARRAY_FILTER_USE_KEY
81 1
        );
82
    }
83
84
    /**
85
     * @param array  $array
86
     * @param string $separator
87
     *
88
     * @return array
89
     */
90 2
    public static function flattenArray(array $array, $separator = '_')
91
    {
92 2
        $resultArray = [];
93 2
        foreach ($array as $key => $val) {
94 2
            if (is_array($val)) {
95
                /* @noinspection AdditionOperationOnArraysInspection */
96 2
                $resultArray += self::prefixArray(self::flattenArray($val), $key, $separator);
97 2
            } else {
98 2
                $resultArray[$key] = $val;
99
            }
100 2
        }
101
102 2
        return $resultArray;
103
    }
104
105
    /**
106
     * @param array  $array
107
     * @param        $prefix
108
     * @param string $separator
109
     *
110
     * @return array
111
     */
112 2
    public static function prefixArray(array $array, $prefix, $separator = '_')
113
    {
114 2
        return array_combine(
115 2
            array_map(
116 2
                function ($key) use ($prefix, $separator) {
117 2
                    return $prefix . $separator . $key;
118 2
                },
119 2
                array_keys($array)
120 2
            ),
121
            $array
122 2
        );
123
    }
124
125
    /**
126
     * Determine if an array is associative or not.
127
     *
128
     * @param array $input
129
     *
130
     * @return bool
131
     */
132 3
    public static function isAssociativeArray(array $input)
133
    {
134 3
        return (bool) count(array_filter(array_keys($input), 'is_string'));
135
    }
136
137
    /**
138
     * Returns whether the array has numeric keys in order 0, 1, ... n or not.
139
     *
140
     * @param array $array
141
     *
142
     * @return bool
143
     */
144 1
    public static function isIndexedArray(array $array)
145
    {
146 1
        return array_keys($array) === range(0, count($array) - 1);
147
    }
148
149
    /**
150
     * Calculate the depth of an array.
151
     * Be aware of array with references to other places in the same array. This can cause memory exhaustion.
152
     *
153
     * @param array $array
154
     * @param bool  $deReference deReference should the array values be de-referenced first?
155
     *
156
     * @throws \RuntimeException
157
     * @throws \InvalidArgumentException
158
     *
159
     * @return int
160
     */
161 2
    public static function calculateArrayDepth(array $array, $deReference = false)
162
    {
163 2
        if ($deReference === true) {
164
            /** @var array $array */
165 1
            $array = unserialize(serialize($array));
166 1
        }
167
168 2
        $maxDepth = 1;
169 2
        foreach ($array as $value) {
170 2
            if (is_array($value)) {
171 2
                $depth = static::calculateArrayDepth($value) + 1;
172 2
                if ($depth > $maxDepth) {
173 2
                    $maxDepth = $depth;
174 2
                }
175 2
            }
176 2
        }
177
178 2
        return $maxDepth;
179
    }
180
181
    /**
182
     * Merge two arrays together (kinda). And a bit of replace.
183
     * Copy the values of source array into the target array, when the keys are numerical .
184
     * this function is scheduled for replacement, please use array_merge or array_replace.
185
     *
186
     * @param array $targetArray   targetArray
187
     * @param array $sourceArray   sourceArray
188
     * @param bool  $combineUnique
189
     *
190
     * @deprecated please use array_merge or array_replace
191
     *
192
     * @return array
193
     */
194 1
    public static function mergeRecursive(array $targetArray, array $sourceArray, $combineUnique = true)
195
    {
196
        // check for each key in source array if it is present in the target array
197 1
        foreach ($sourceArray as $key => $value) {
198 1
            if (array_key_exists($key, $targetArray)) {
199 1
                if (is_int($key)) {
200 1
                    if ($combineUnique === false || !in_array($value, $targetArray, true)) {
201 1
                        $targetArray[] = $value;
202 1
                    }
203 1
                } elseif (is_array($value) && is_array($targetArray[$key])) {
204 1
                    $targetArray[$key] = static::mergeRecursive($targetArray[$key], $value, $combineUnique);
0 ignored issues
show
Deprecated Code introduced by
The method WebservicesNl\Utils\ArrayUtils::mergeRecursive() has been deprecated with message: please use array_merge or array_replace

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
205 1
                } else {
206
                    // replace key
207 1
                    $targetArray[$key] = $value;
208
                }
209 1
            } else {
210
                // replace key
211 1
                $targetArray[$key] = $value;
212
            }
213 1
        }
214
215 1
        return $targetArray;
216
    }
217
218
    /**
219
     * @param array $haystack
220
     * @param array ...$needles
221
     *
222
     * @return bool
223
     */
224 1
    public static function hasAllKeys(array $haystack, ...$needles)
225
    {
226 1
        foreach ($needles as $needle) {
227 1
            if (array_key_exists($needle, $haystack) === false) {
228 1
                return false;
229
            }
230 1
        }
231
232 1
        return true;
233
    }
234
235
    /**
236
     * @param array $haystack
237
     * @param array ...$needles
238
     *
239
     * @return bool
240
     */
241 1
    public static function hasAnyKey(array $haystack, ...$needles)
242
    {
243 1
        foreach ($needles as $needle) {
244 1
            if (array_key_exists($needle, $haystack)) {
245 1
                return true;
246
            }
247 1
        }
248
249 1
        return false;
250
    }
251
252
    /**
253
     * Convert an array to camelcased key version
254
     *
255
     * @param array $array
256
     *
257
     * @throws \InvalidArgumentException
258
     * @throws \Ddeboer\Transcoder\Exception\UnsupportedEncodingException
259
     * @throws \Ddeboer\Transcoder\Exception\ExtensionMissingException
260
     *
261
     * @return array
262
     */
263 2
    public static function toUnderscore(array $array = [])
264
    {
265 2
        $output = [];
266 2
        foreach ($array as $key => $value) {
267 2
            $key = StringUtils::toUnderscore($key);
268 1
            if (is_array($value)) {
269 1
                $value = static::toUnderscore($value);
270 1
            }
271 1
            $output[$key] = $value;
272 1
        }
273
274 1
        return $output;
275
    }
276
}
277