Completed
Push — master ( 16bda6...f53e58 )
by Mohamed
43:31 queued 41:47
created

Arrays::repeat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace __\Traits;
4
5
trait Arrays
6
{
7
    /**
8
     * Append item to array
9
     *
10
     ** __::append([1, 2, 3], 4);
11
     ** // → [1, 2, 3, 4]
12
     *
13
     * @param array $array original array
14
     * @param null  $value new item or valie to append
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
15
     *
16
     * @return array
17
     *
18
     */
19 1
    public static function append(array $array = [], $value = null)
20
    {
21 1
        $array[] = $value;
22
23 1
        return $array;
24
    }
25
26
    /**
27
     * Creates  an  array  with  all  falsey  values removed. The values
28
     * false, null, 0, "", undefined, and NaN are all falsey.
29
     *
30
     ** __::compact([0, 1, false, 2, '', 3]);
31
     ** // → [1, 2, 3]
32
     *
33
     * @param array $array array to compact
34
     *
35
     * @return array
36
     *
37
     */
38 2
    public static function compact(array $array = [])
39
    {
40 2
        $result = [];
41
42 2
        foreach ($array as $value) {
43 2
            if ($value) {
44 2
                $result[] = $value;
45
            }
46
        }
47
48 2
        return $result;
49
    }
50
51
52
    /**
53
     * base flatten
54
     *
55
     * @param array $array
56
     * @param bool  $shallow
57
     * @param bool  $strict
58
     * @param int   $startIndex
59
     *
60
     * @return array
61
     *
62
     */
63 1
    public static function baseFlatten(array $array, $shallow = false, $strict = true, $startIndex = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $startIndex is not used and could be removed. ( Ignorable by Annotation )

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

63
    public static function baseFlatten(array $array, $shallow = false, $strict = true, /** @scrutinizer ignore-unused */ $startIndex = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
66 1
        $output = [];
67 1
        $idx    = 0;
68
69 1
        foreach ($array as $index => $value) {
70 1
            if (is_array($value)) {
71
72 1
                if (!$shallow) {
73 1
                    $value = static::baseFlatten($value, $shallow, $strict);
74
                }
75 1
                $j   = 0;
76 1
                $len = count($value);
77 1
                while ($j < $len) {
78 1
                    $output[$idx++] = $value[$j++];
79
                }
80
            } else {
81 1
                if (!$strict) {
82 1
                    $output[$idx++] = $value;
83
                }
84
            }
85
        }
86
87 1
        return $output;
88
    }
89
90
    /**
91
     * Flattens a multidimensional array. If you pass shallow, the array will only be flattened a single level.
92
     *
93
     * __::flatten([1, 2, [3, [4]]], [flatten]);
94
     *      >> [1, 2, 3, 4]
95
     *
96
     * @param      $array
97
     * @param bool $shallow
98
     *
99
     * @return array
100
     *
101
     */
102 1
    public static function flatten($array, $shallow = false)
103
    {
104 1
        return static::baseFlatten($array, $shallow, false);
105
    }
106
107
    /**
108
     *  Patches array by xpath.
109
     *
110
     ** __::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]);
111
     ** // → ['addr' => ['country' => 'CA', 'zip' => 54321]]
112
     *
113
     * @param array  $arr     The array to patch
114
     * @param  array $patches List of new xpath-value pairs
115
     * @param string $parent
116
     *
117
     * @return array Returns patched array
118
     *
119
     */
120 1
    public static function patch($arr, $patches, $parent = '')
121
    {
122 1
        foreach ($arr as $key => $value) {
123 1
            $z = $parent . '/' . $key;
124
125 1
            if (isset($patches[$z])) {
126 1
                $arr[$key] = $patches[$z];
127 1
                unset($patches[$z]);
128
129 1
                if (!count($patches)) {
130 1
                    break;
131
                }
132
            }
133
134 1
            if (is_array($value)) {
135 1
                $arr[$key] = static::patch($value, $patches, $z);
136
            }
137
        }
138
139 1
        return $arr;
140
    }
141
142
    /**
143
     * prend item or value to an array
144
     *
145
     ** __::prepend([1, 2, 3], 4);
146
     ** // → [4, 1, 2, 3]
147
     *
148
     * @param array $array
149
     * @param null  $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
150
     *
151
     * @return array
152
     *
153
     */
154 1
    public static function prepend(array $array = [], $value = null)
155
    {
156 1
        \array_unshift($array, $value);
157
158 1
        return $array;
159
    }
160
161
    /**
162
     * generate range of values based on start , end and step
163
     ** __::range(1, 10, 2);
164
     ** // → [1, 3, 5, 7, 9]
165
     *
166
     * @param integer|null $start range start
167
     * @param integer|null $stop  range end
168
     * @param integer      $step  range step value
169
     *
170
     * @return array range of values
171
     *
172
     */
173 1
    public static function range($start = null, $stop = null, $step = 1)
174
    {
175 1
        if ($stop == null && $start != null) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $start of type null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing $stop of type null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
176 1
            $stop  = $start;
177 1
            $start = 1;
178
        }
179
180 1
        return \range($start, $stop, $step);
181
    }
182
183
    /**
184
     * generate array of repeated values
185
     *
186
     ** __::repeat('foo', 3);
187
     ** // → ['foo', 'foo', 'foo']
188
     *
189
     * @param string $object The object to repeat.
190
     * @param null   $times  ow many times has to be repeated.
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $times is correct as it would always require null to be passed?
Loading history...
191
     *
192
     * @return array Returns a new array of filled values.
193
     *
194
     */
195 1
    public static function repeat($object = '', $times = null)
196
    {
197 1
        if ($times == null) {
198
            return [];
199
        } else {
200 1
            return \array_fill(0, $times, $object);
201
        }
202
    }
203
204
205
    /**
206
     * Creates an array of elements split into groups the length of size. If array can't be split evenly, the final
207
     * chunk will be the remaining elements.
208
     *
209
     * __::chunk([1, 2, 3, 4, 5], 3);
210
     * // → [[1, 2, 3], [4, 5]]
211
     *
212
     * @param array   $array        original array
213
     * @param int     $size         the chunk size
214
     * @param boolean $preserveKeys When set to TRUE keys will be preserved. Default is FALSE which will reindex the
215
     *                              chunk numerically
216
     *
217
     * @return array
218
     *
219
     */
220
    public static function chunk(array $array, $size = 1, $preserveKeys = false)
221
    {
222
        return \array_chunk($array, $size, $preserveKeys);
223
    }
224
225
    /**
226
     * Creates a slice of array with n elements dropped from the beginning.
227
     *
228
     ** __::drop([0, 1, 3], 2);
229
     ** // → [3]
230
     *
231
     * @param array $input  The array to query.
232
     * @param int   $number The number of elements to drop.
233
     *
234
     * @return array
235
     *
236
     */
237
    public static function drop(array $input, $number = 1)
238
    {
239
        return array_slice($input, $number);
240
    }
241
242
    /**
243
     * Shuffle an array ensuring no item remains in the same position.
244
     *
245
     ** __::randomize([1, 2, 3]);
246
     ** // → [2, 3, 1]
247
     *
248
     * @param array $array original array
249
     *
250
     * @return array
251
     *
252
     */
253
    public static function randomize(array $array)
254
    {
255
        for ($i = 0, $c = \count($array); $i < $c - 1; $i++) {
256
            $j = \rand($i + 1, $c - 1);
257
            list($array[$i], $array[$j]) = [$array[$j], $array[$i]];
258
        }
259
260
        return $array;
261
    }
262
}