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 ( 82e905...31190b )
by Freek
29:30
created

array_functions.php ➔ array_rand_value()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace Spatie;
4
5
/**
6
 * Get a random value from an array.
7
 *
8
 * @param array $array
9
 * @param int   $numReq The amount of values to return
10
 *
11
 * @return mixed
12
 */
13
function array_rand_value(array $array, $numReq = 1)
14
{
15
    if (!count($array)) {
16
        return;
17
    }
18
19
    $keys = array_rand($array, $numReq);
20
21
    if ($numReq === 1) {
22
        return $array[$keys];
23
    }
24
25
    return array_intersect_key($array, array_flip($keys));
26
}
27
28
/**
29
 * Get a random value from an array, with the ability to skew the results.
30
 * Example: array_rand_weighted(['foo' => 1, 'bar' => 2]) has a 66% chance of returning bar.
31
 *
32
 * @param array $array
33
 *
34
 * @return mixed
35
 */
36
function array_rand_weighted(array $array)
37
{
38
    $options = [];
39
40
    foreach ($array as $option => $weight) {
41
        for ($i = 0; $i < $weight; ++$i) {
42
            $options[] = $option;
43
        }
44
    }
45
46
    return array_rand_value($options);
47
}
48
49
/**
50
 * Determine if all given needles are present in the haystack.
51
 *
52
 * @param array|string $needles
53
 * @param array        $haystack
54
 *
55
 * @return bool
56
 */
57
function values_in_array($needles, array $haystack)
58
{
59
    if (!is_array($needles)) {
60
        $needles = [$needles];
61
    }
62
63
    return count(array_intersect($needles, $haystack)) === count($needles);
64
}
65
66
/**
67
 * Determine if all given needles are present in the haystack as array keys.
68
 *
69
 * @param array|string $needles
70
 * @param array        $haystack
71
 *
72
 * @return bool
73
 */
74
function array_keys_exist($needles, array $haystack)
75
{
76
    if (!is_array($needles)) {
77
        return array_key_exists($needles, $haystack);
78
    }
79
80
    return values_in_array($needles, array_keys($haystack));
81
}
82
83
/**
84
 * Returns an array with two elements.
85
 *
86
 * Iterates over each value in the array passing them to the callback function.
87
 * If the callback function returns true, the current value from array is returned in the first
88
 * element of result array. If not, it is return in the second element of result array.
89
 *
90
 * Array keys are preserved.
91
 *
92
 * @param array    $array
93
 * @param callable $callback
94
 *
95
 * @return array
96
 */
97
function array_split_filter(array $array, callable $callback)
98
{
99
    $passesFilter = array_filter($array, $callback);
100
101
    $negatedCallback = function ($item) use ($callback) { return !$callback($item); };
102
103
    $doesNotPassFilter = array_filter($array, $negatedCallback);
104
105
    return [$passesFilter, $doesNotPassFilter];
106
}
107
108
/**
109
 * Split an array in the given amount of pieces.
110
 *
111
 * @param array $array
112
 * @param int   $numberOfPieces
113
 * @param bool  $preserveKeys
114
 *
115
 * @return array
116
 */
117
function array_split(array $array, $numberOfPieces = 2, $preserveKeys = false)
118
{
119
    if (count($array) === 0) {
120
        return [];
121
    }
122
123
    $splitSize = ceil(count($array) / $numberOfPieces);
124
125
    return array_chunk($array, $splitSize, $preserveKeys);
126
}
127
128
/**
129
 * Returns an array with the unique values from all the given arrays.
130
 *
131
 * @param \array[] $arrays
132
 *
133
 * @return array
134
 */
135
function array_merge_values(array ...$arrays)
136
{
137
    $allValues = array_reduce($arrays, function ($carry, $array) {
138
         return array_merge($carry, $array);
139
    }, []);
140
141
    return array_values(array_unique($allValues));
142
}
143
144
/**
145
 * Flatten an array of arrays. The `$levels` parameter specifies how deep you want to
146
 * recurse in the array. If `$levels` is -1, the function will recurse infinitely.
147
 *
148
 * @param array $array
149
 * @param int   $levels
150
 *
151
 * @return array
152
 */
153
function array_flatten(array $array, $levels = -1)
154
{
155
    if ($levels === 0) {
156
        return $array;
157
    }
158
159
    $flattened = [];
160
161
    if ($levels !== -1) {
162
        --$levels;
163
    }
164
165
    foreach ($array as $element) {
166
        $flattened = array_merge(
167
            $flattened,
168
            is_array($element) ? array_flatten($element, $levels) : [$element]
169
        );
170
    }
171
172
    return $flattened;
173
}
174