Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
5 | class Arr |
||
6 | { |
||
7 | /** |
||
8 | *Returns true if the provided function returns true for all elements of an array, false otherwise. |
||
9 | * |
||
10 | * @param array $items |
||
11 | * @param mixed $func |
||
12 | */ |
||
13 | 1 | public static function all(array $items, $func): bool |
|
17 | |||
18 | /** |
||
19 | * Returns true if the provided function returns true for at least one element of an array, false otherwise. |
||
20 | * |
||
21 | * @param array $items |
||
22 | * @param mixed $func |
||
23 | */ |
||
24 | 1 | public static function any(array $items, $func): bool |
|
28 | |||
29 | /** |
||
30 | * Chunks an array into smaller arrays of a specified size. |
||
31 | * |
||
32 | * @param array $items |
||
33 | * @param int $size |
||
34 | */ |
||
35 | public static function chunk(array $items, int $size): array |
||
39 | |||
40 | /** |
||
41 | * Deep flattens an array. |
||
42 | * |
||
43 | * @param array $items |
||
44 | */ |
||
45 | 1 | View Code Duplication | public static function deepFlatten(array $items): array |
58 | |||
59 | /** |
||
60 | * Returns a new array with n elements removed from the left. |
||
61 | * |
||
62 | * @param array $items |
||
63 | * @param int $n |
||
64 | */ |
||
65 | 1 | public static function drop(array $items, int $n = 1): array |
|
69 | |||
70 | /** |
||
71 | * Returns the last element for which the provided function returns a truthy value. |
||
72 | * |
||
73 | * @param array $items |
||
74 | * @param mixed $func |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 1 | public static function findLast(array $items, $func) |
|
84 | |||
85 | /** |
||
86 | * Returns the index of the last element for which the provided function returns a truthy value. |
||
87 | * |
||
88 | * @param array $items |
||
89 | * @param mixed $func |
||
90 | */ |
||
91 | 1 | public static function findLastIndex(array $items, $func) |
|
97 | |||
98 | /** |
||
99 | * Flattens an array up to the one level depth. |
||
100 | * |
||
101 | * @param array $items |
||
102 | */ |
||
103 | 1 | View Code Duplication | public static function flatten(array $items): array |
116 | |||
117 | /** |
||
118 | * Groups the elements of an array based on the given function. |
||
119 | * |
||
120 | * @param array $items |
||
121 | * @param mixed $func |
||
122 | */ |
||
123 | 1 | public static function groupBy(array $items, $func): array |
|
139 | |||
140 | /** |
||
141 | * Sorts a collection of arrays or objects by key |
||
142 | * |
||
143 | * @param array $items |
||
144 | * @param mixed $attr |
||
145 | * @param string $order |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 1 | public static function orderBy(array $items, $attr, string $order): array |
|
164 | |||
165 | /** |
||
166 | * Checks a flat list for duplicate values. Returns true if duplicate values exists and false if values are all unique. |
||
167 | * |
||
168 | * @param array $items |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | 1 | public static function hasDuplicates(array $items): bool |
|
176 | |||
177 | /** |
||
178 | * Returns the head of a list. |
||
179 | * |
||
180 | * @param array $items |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | 1 | public static function head(array $items) |
|
188 | |||
189 | /** |
||
190 | * Returns the last element in an array. |
||
191 | * |
||
192 | * @param array $items |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | 1 | public static function last(array $items) |
|
200 | |||
201 | /** |
||
202 | * Retrieves all of the values for a given key: |
||
203 | * |
||
204 | * @param array $items |
||
205 | * @param mixed $key |
||
206 | */ |
||
207 | 1 | public static function pluck(array $items, $key) |
|
213 | |||
214 | /** |
||
215 | * Mutates the original array to filter out the values specified. |
||
216 | * |
||
217 | * @param array $items |
||
218 | * @param mixed ...$params |
||
219 | */ |
||
1 ignored issue
–
show
|
|||
220 | 1 | public static function pull(&$items, ...$params) |
|
226 | |||
227 | /** |
||
228 | * Filters the collection using the given callback. |
||
229 | * |
||
230 | * @param array $items |
||
231 | * @param mixed $func |
||
232 | */ |
||
233 | 1 | public static function reject(array $items, $func) |
|
237 | |||
238 | /** |
||
239 | * Removes elements from an array for which the given function returns false. |
||
240 | * |
||
241 | * @param array $items |
||
242 | * @param mixed $func |
||
243 | */ |
||
244 | 1 | public static function remove(array $items, $func) |
|
250 | |||
251 | /** |
||
252 | * Returns all elements in an array except for the first one. |
||
253 | * |
||
254 | * @param array $items |
||
255 | */ |
||
256 | 1 | public static function tail(array $items) |
|
260 | |||
261 | /** |
||
262 | * Returns an array with n elements removed from the beginning. |
||
263 | * |
||
264 | * @param array $items |
||
265 | * @param int $n |
||
266 | */ |
||
267 | 1 | public static function take(array $items, int $n = 1): array |
|
271 | |||
272 | /** |
||
273 | * Filters out the elements of an array, that have one of the specified values. |
||
274 | * |
||
275 | * @param array $items |
||
276 | * @param mixed ...$params |
||
277 | */ |
||
1 ignored issue
–
show
|
|||
278 | 1 | public static function without(array $items, ...$params) |
|
282 | } |
||
283 |