Passed
Pull Request — master (#12)
by Stephen
09:21
created

arrayRandom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Sfneal\Helpers\Arrays\ArrayHelpers;
4
use Sfneal\Helpers\Arrays\ChunkSizer;
5
6
/**
7
 * Returns a chunked array with calculated chunk size.
8
 *
9
 * @param array $array
10
 * @param int $min
11
 * @param int|null $max
12
 * @param bool $no_remainders
13
 * @param bool $preserve_keys
14
 * @return array
15
 */
16
function arrayChunks(array $array, $min = 0, $max = null, $no_remainders = false, $preserve_keys = true): array
17
{
18
    return (new ArrayHelpers($array))->arrayChunks($min, $max, $no_remainders, $preserve_keys);
19
}
20
21
/**
22
 * Flatten a multidimensional array into a 2D array without nested keys.
23
 *
24
 * @param array $array
25
 * @param bool $nest_keys
26
 * @return array
27
 */
28
function arrayFlattenKeys(array $array, $nest_keys = true): array
29
{
30
    return (new ArrayHelpers($array))->arrayFlattenKeys($nest_keys);
31
}
32
33
/**
34
 * Remove particular keys from a multidimensional array.
35
 *
36
 * @param array $array
37
 * @param array|string $keys
38
 * @return array
39
 */
40
function arrayRemoveKeys(array $array, $keys): array
41
{
42
    return (new ArrayHelpers($array))->arrayRemoveKeys($keys);
43
}
44
45
/**
46
 * Sum the values of two arrays.
47
 *
48
 * @param array $array1
49
 * @param array $array2
50
 * @return array
51
 */
52
function sumArrays(array $array1, array $array2): array
53
{
54
    return (new ArrayHelpers($array1))->sumArrays($array2);
55
}
56
57
/**
58
 * Determine if all values in an array of key => value pairs are unique.
59
 *
60
 * @param array $array
61
 * @return bool
62
 */
63
function arrayValuesUnique(array $array): bool
64
{
65
    return (new ArrayHelpers($array))->arrayValuesUnique();
66
}
67
68
/**
69
 * Determine if all array_values are equal to a certain value.
70
 *
71
 * @param array $array
72
 * @param mixed $value
73
 * @return bool
74
 */
75
function arrayValuesEqual(array $array, $value): bool
76
{
77
    return (new ArrayHelpers($array))->arrayValuesEqual($value);
78
}
79
80
/**
81
 * Determine if an array is multidimensional and has keys.
82
 *
83
 * @param array $array
84
 * @return bool
85
 */
86
function arrayHasKeys(array $array): bool
87
{
88
    return (new ArrayHelpers($array))->arrayHasKeys();
89
}
90
91
if (! function_exists('array_except')) {
92
    /**
93
     * Remove specific arrays of keys without modifying the original array.
94
     *
95
     * @param array $original
96
     * @param array $except
97
     * @return array
98
     */
99
    function array_except(array $original, array $except): array
100
    {
101
        return (new ArrayHelpers($original))->array_except($except);
102
    }
103
}
104
105
/**
106
 * Return a best fit chunk size to be passed to array_chunks functions.
107
 *
108
 * Calculates the remainder of array sizes divided by the divisor
109
 * using modulus division.  Continues to calculate remainders until
110
 * the remainder is zero, signifying evenly sized chunks, or the
111
 * divisor is equal to the array size.  If a remainder of zero is not
112
 * found the lowest remainder is returned.
113
 *
114
 * @param int $array_size size of the array
115
 * @param int $min minimum chunk size
116
 * @param int|null $max maximum chunk size
117
 * @param int $divisor
118
 * @return int $remainder lowest calculated remainder
119
 */
120
function chunkSizer(int $array_size, int $min = 0, int $max = null, int $divisor = 2): int
121
{
122
    return (new ChunkSizer($array_size, $min, $max, $divisor))->execute();
123
}
124
125
/**
126
 * Remove a key from an array & return the key's value.
127
 *
128
 * @param array $array
129
 * @param string $key
130
 * @return mixed
131
 */
132
function arrayUnset(array $array, string $key)
133
{
134
    return (new ArrayHelpers($array))->arrayUnset($key);
135
}
136
137
/**
138
 * Determine if all values in an array are null.
139
 *
140
 * @param array $array
141
 * @return bool
142
 */
143
function arrayValuesNull(array $array): bool
144
{
145
    return (new ArrayHelpers($array))->arrayValuesNull();
146
}
147
148
if (! function_exists('arrayRandom')) {
149
    /**
150
     * Retrieve a random array of elements.
151
     *
152
     * @param array $array
153
     * @param int $items
154
     * @return array
155
     */
156
    function arrayRandom(array $array, int $items): array
157
    {
158
        return (new ArrayHelpers($array))->random($items);
159
    }
160
}
161
162
/**
163
 * Only declare function if Illuminate/Collection is installed.
164
 */
165
if (function_exists('collect')) {
166
    /**
167
     * Return a flat array of values found in the $first array that are not found in the $second.
168
     *
169
     * @param array $first
170
     * @param array $second
171
     * @param bool $toArray
172
     * @return Illuminate\Support\Collection|array
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
173
     */
174
    function array_diff_flat(array $first, array $second, bool $toArray = true)
175
    {
176
        $collection = collect($first)
177
            ->diff($second)
178
            ->flatten();
179
180
        // Return as array
181
        if ($toArray) {
182
            return $collection->toArray();
183
        }
184
185
        // Return as Collection
186
        return $collection;
187
    }
188
}
189