Completed
Push — master ( 713d54...d55531 )
by Amine
02:08
created

list.php ➔ all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Tarsana\Functional;
2
/**
3
 * This file contains some useful functions to handle lists (arrays having only numeric keys).
4
 */
5
6
/**
7
 * Curried version of `array_map`.
8
 * ```php
9
 * $doubles = map(function($x) { return 2 * $x; });
10
 * $doubles([1, 2, 3, 4]) // [2, 4, 6, 8]
11
 * ```
12
 *
13
 * @signature (a -> b) -> [a] -> [b]
14
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
15
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
16
 * @return array
17
 */
18
function map() {
19
    $map = function($fn, $list) {
20
        return array_map($fn, $list);
21
    };
22
    return apply(curry($map), func_get_args());
23
}
24
25
/**
26
 * Applies a function to items of the array and concatenates the results.
27
 * This is also known as `flatMap` in some libraries.
28
 * ```php
29
 * $words = chain(split(' '));
30
 * $words(['Hello World', 'How are you']) // ['Hello', 'World', 'How', 'are', 'you']
31
 * ```
32
 *
33
 * @signature (a -> [b]) -> [a] -> [b]
34
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
 * @return array
37
 */
38
function chain() {
39
    $chain = function($fn, $list) {
40
        return concatAll(map($fn, $list));
41
    };
42
    return apply(curry($chain), func_get_args());
43
}
44
45
/**
46
 * Curried version of `array_filter` with modified order of
47
 * arguments. The callback is the first argument then the list.
48
 * ```php
49
 * $list = [1, 'aa', 3, [4, 5]];
50
 * $numeric = F\filter('is_numeric');
51
 * $numeric($list) // [1, 3]
52
 * ```
53
 * @signature (a -> Boolean) -> [a] -> [a]
54
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
56
 * @return array
57
 */
58
function filter() {
59
    $filter = function($fn, $list){
60
        return array_values(array_filter($list, $fn));
61
    };
62
    return apply(curry($filter), func_get_args());
63
}
64
65
/**
66
 * Curried version of `array_reduce` with modified order of
67
 * arguments ($callback, $initial, $list).
68
 * ```php
69
 * $list = [1, 2, 3, 4];
70
 * $sum = reduce('Tarsana\Functional\plus', 0);
71
 * $sum($list) // 10
72
 * ```
73
 *
74
 * @signature (* -> a -> *) -> * -> [a] -> *
75
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
 * @param  mixed $initial
0 ignored issues
show
Bug introduced by
There is no parameter named $initial. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
78
 * @return array
79
 */
80
function reduce() {
81
    $reduce = function($fn, $initial, $list){
82
        return array_reduce($list, $fn, $initial);
83
    };
84
    return apply(curry($reduce), func_get_args());
85
}
86
87
/**
88
 * Applies the callback to each item and returns the original list.
89
 * ```php
90
 * $list = [1, 2, 3, 4];
91
 * each(function($item){
92
 *     echo $item, PHP_EOL;
93
 * }, $list);
94
 * // Outputs:
95
 * // 1
96
 * // 2
97
 * // 3
98
 * // 4
99
 * ```
100
 *
101
 * @signature (a -> *) -> [a] -> [a]
102
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
103
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
 * @return array
105
 */
106 View Code Duplication
function each() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    $each = function($fn, $list){
108
        foreach ($list as $item) {
109
            apply($fn, [$item]);
110
        }
111
        return $list;
112
    };
113
    return apply(curry($each), func_get_args());
114
}
115
116
/**
117
 * Returns the first item of the given array or string.
118
 * ```php
119
 * head([1, 2, 3, 4]) // 1
120
 * head('Hello') // 'H'
121
 * head([]) // null
122
 * head('') // ''
123
 * ```
124
 *
125
 * @signature [a] -> a
126
 * @signature String -> String
127
 * @param  array|string $list
128
 * @return mixed
129
 */
130 View Code Duplication
function head($list) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    if(is_string($list))
132
        return substr($list, 0, 1);
133
    return (count($list) > 0)
134
        ? $list[0]
135
        : null;
136
}
137
138
/**
139
 * Returns the last item of the given array or string.
140
 * ```php
141
 * last([1, 2, 3, 4]) // 4
142
 * last('Hello') // 'o'
143
 * last([]) // null
144
 * last('') // ''
145
 * ```
146
 *
147
 * @signature [a] -> a
148
 * @signature String -> String
149
 * @param  array|string $list
150
 * @return mixed
151
 */
152 View Code Duplication
function last($list) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
    if(is_string($list))
154
        return substr($list, -1);
155
    return (count($list) > 0)
156
        ? $list[count($list) - 1]
157
        : null;
158
}
159
160
/**
161
 * Returns all but the last element of the given array or string.
162
 * ```php
163
 * init([1, 2, 3, 4]) // [1, 2, 3]
164
 * init('Hello') // 'Hell'
165
 * init([7]) // []
166
 * init([]) // []
167
 * init('') // ''
168
 * ```
169
 *
170
 * @signature [a] -> a
171
 * @signature String -> String
172
 * @param  array|string $list
173
 * @return array
174
 */
175
function init($list) {
176 View Code Duplication
    if(is_string($list))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
        return (strlen($list) > 1)
178
            ? substr($list, 0, strlen($list) - 1)
179
            : '';
180
    return (count($list) > 1)
181
        ? array_slice($list, 0, count($list) - 1)
182
        : [];
183
}
184
185
/**
186
 * Returns all but the first element of the given array or string.
187
 * ```php
188
 * tail([1, 2, 3, 4]) // [2, 3, 4]
189
 * tail('Hello') // 'ello'
190
 * tail([7]) // []
191
 * tail([]) // []
192
 * tail('') // ''
193
 * ```
194
 *
195
 * @signature [a] -> a
196
 * @signature String -> String
197
 * @param  array|string $list
198
 * @return array
199
 */
200
function tail($list) {
201 View Code Duplication
    if(is_string($list))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
        return (strlen($list) > 1)
203
            ? substr($list, 1)
204
            : '';
205
    return (count($list) > 1)
206
        ? array_slice($list, 1)
207
        : [];
208
}
209
210
/**
211
 * Alias of `array_reverse()` and `strrev()`.
212
 * ```php
213
 * reverse([1, 2, 3, 4]) // [4, 3, 2, 1]
214
 * reverse('Hello') // 'olleH'
215
 * ```
216
 *
217
 * @signature [a] -> [a]
218
 * @signature String -> String
219
 * @param  array|string $list
220
 * @return array
221
 */
222
function reverse($list) {
223
    return is_string($list)
224
        ? strrev($list)
225
        : array_reverse($list);
226
}
227
228
/**
229
 * Alias for `count()` and `strlen()`.
230
 * ```php
231
 * length([1, 2, 3, 4]) // 4
232
 * length('Hello') // 5
233
 * ```
234
 *
235
 * @signature [a] -> Number
236
 * @signature String -> Number
237
 * @param  array|string $list
238
 * @return int
239
 */
240
function length($list) {
241
    return is_string($list)
242
        ? strlen($list)
243
        : count($list);
244
}
245
246
/**
247
 * Checks if the `$predicate` is verified by **all** items of the array.
248
 * ```php
249
 * $allNotNull = all(pipe(eq(0), not()));
250
 * $allNotNull([9, 3, 2, 4]); // true
251
 * $allNotNull([9, 3, 0, 4]); // false
252
 * ```
253
 *
254
 * @signature (a -> Boolean) -> [a] -> Boolean
255
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
256
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
257
 * @return bool
258
 */
259
function all() {
260
    $all = function($predicate, $list) {
261
        return length(filter($predicate, $list)) == length($list);
262
    };
263
    return apply(curry($all), func_get_args());
264
}
265
266
267
/**
268
 * Checks if the `$predicate` is verified by **any** item of the array.
269
 * ```php
270
 * $anyNumeric = any('is_numeric');
271
 * $anyNumeric(['Hello', '12', []]); // true
272
 * $anyNumeric(['Hello', 'Foo']); // false
273
 * ```
274
 *
275
 * @signature (a -> Boolean) -> [a] -> Boolean
276
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
277
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
278
 * @return bool
279
 */
280
function any() {
281
    $any = function($predicate, $list) {
282
        return null != findIndex($predicate, $list);
283
    };
284
    return apply(curry($any), func_get_args());
285
}
286
287
/**
288
 * Concatenates two arrays or strings.
289
 * ```php
290
 * concat([1, 2], [3, 4]) // [1, 2, 3, 4]
291
 * concat('Hello ', 'World') // 'Hello World'
292
 * ```
293
 *
294
 * @signature [*] -> [*] -> [*]
295
 * @signature String -> String -> String
296
 * @param  array $list1
0 ignored issues
show
Bug introduced by
There is no parameter named $list1. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
297
 * @param  array $list2
0 ignored issues
show
Bug introduced by
There is no parameter named $list2. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
298
 * @return array
299
 */
300
function concat() {
301
    $concat = function($list1, $list2) {
302
        if (is_string($list1))
303
            return $list1 . $list2;
304
        return array_merge($list1, $list2);
305
    };
306
    return apply(curry($concat), func_get_args());
307
}
308
309
/**
310
 * Concatenates a list of arrays or strings.
311
 * ```php
312
 * concatAll([[1, 2], [3, 4], [5, 6]]) // [1, 2, 3, 4, 5, 6]
313
 * concatAll('Hello ', 'World', ' !') // 'Hello World !'
314
 * ```
315
 *
316
 * @signature [[a]] -> [a]
317
 * @param  array $lists
0 ignored issues
show
Bug introduced by
There is no parameter named $lists. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
318
 * @return array
319
 */
320
function concatAll() {
321
    $concat = function($lists) {
322
        return reduce(concat(), [], $lists);
323
    };
324
    return apply(curry($concat), func_get_args());
325
}
326
327
/**
328
 * Inserts an item at some position into an array or a substring into a string. If `$position < 0` 
329
 * the item or substring is inserted before the last `|$position|` elements/characters.
330
 * ```php
331
 * insert(2, 'x', [1, 2, 3, 4]); // [1, 2, 'x', 3, 4]
332
 * insert(-1,  'x', [1, 2, 3, 4]); // [1, 2, 3, 'x', 4]
333
 * insert(11, 'x', [1, 2, 3, 4]); // [1, 2, 3, 4, 'x']
334
 * insert(0, 'x', [1, 2, 3, 4]); // ['x', 1, 2, 3, 4]
335
 * insert(-11, 'x', [1, 2, 3, 4]); // ['x', 1, 2, 3, 4]
336
 * insert(3, 'l', 'Helo World'); // 'Hello World'
337
 * ```
338
 *
339
 * @signature Number -> a -> [a] -> [a]
340
 * @signature Number -> String -> String -> String
341
 * @param  int $position
0 ignored issues
show
Bug introduced by
There is no parameter named $position. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
342
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
343
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
344
 * @return array
345
 */
346
function insert() {
347
    $insert = function($position, $item, $list) {
348
        return (is_string($list)) 
349
            ? insertAll($position, $item, $list)
350
            : insertAll($position, [$item], $list);
351
    };
352
    return apply(curry($insert), func_get_args());
353
}
354
355
/**
356
 * Same as `insert` but inserts an array instead of a single item.
357
 * ```php
358
 * insertAll(2, ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 'x', 'y', 3, 4]
359
 * insertAll(-1,  ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 3, 'x', 'y', 4]
360
 * insertAll(11, ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 3, 4, 'x', 'y']
361
 * insertAll(0, ['x', 'y'], [1, 2, 3, 4]); // ['x', 'y', 1, 2, 3, 4]
362
 * insertAll(-11, ['x', 'y'], [1, 2, 3, 4]); // ['x', 'y', 1, 2, 3, 4]
363
 * insert(2, 'llo', 'He World'); // 'Hello World'
364
 * ```
365
 *
366
 * @signature Number -> [a] -> [a] -> [a]
367
 * @signature Number -> String -> String -> String
368
 * @param  int $position
0 ignored issues
show
Bug introduced by
There is no parameter named $position. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
369
 * @param  mixed $items
0 ignored issues
show
Bug introduced by
There is no parameter named $items. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
370
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
371
 * @return array
372
 */
373
function insertAll() {
374
    $insertAll = function($position, $items, $list) {
375
        if ($position < 0)
376
            $position = length($list) - $position;
377
        return concatAll([take($position, $list), $items, remove($position, $list)]);
378
    };
379
    return apply(curry($insertAll), func_get_args());
380
}
381
382
/**
383
 * Appends an item to an array or a substring to a string.
384
 * ```php
385
 * append(5, [1, 2, 3]) // [1, 2, 3, 5]
386
 * append(' World', 'Hello') // 'Hello World'
387
 * ```
388
 *
389
 * @signature * -> [*] -> [*]
390
 * @signature String -> String -> String
391
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
392
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
393
 * @return array
394
 */
395
function append() {
396
    $append = function ($item, $list) {
397
        return concat($list, [$item]);
398
    };
399
    return apply(curry($append), func_get_args());
400
}
401
402
/**
403
 * Inserts an item at the begining of an array or a substring at the begining of a string.
404
 * Note that this function is equivalent to `insert(0)`.
405
 * ```php
406
 * prepend(5, [1, 2, 3]) // [5, 1, 2, 3]
407
 * prepend('Hello ', 'World') // 'Hello World'
408
 * ```
409
 *
410
 * @signature a -> [a] -> [a]
411
 * @signature String -> String -> String
412
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
413
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
414
 * @return array
415
 */
416
function prepend() {
417
    return apply(insert(0), func_get_args());
418
}
419
420
/**
421
 * Takes a number of elements from an array. If `$count` is
422
 * negative, the elements are taken from the end of the array.
423
 * ```php
424
 * $items = ['Foo', 'Bar', 'Baz'];
425
 * take(2, $items) // ['Foo', 'Bar']
426
 * take(0, $items) // []
427
 * take(-2, $items) // ['Bar', 'Baz']
428
 * take(5, 'Hello World') // 'Hello'
429
 * take(-5, 'Hello World') // 'World'
430
 * ```
431
 *
432
 * @signature Number -> [a] -> [a]
433
 * @signature Number -> String -> String
434
 * @param  int $count
0 ignored issues
show
Bug introduced by
There is no parameter named $count. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
435
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
436
 * @return array
437
 */
438
function take() {
439
    $take = function($count, $list) {
440
        if ($count > $length || $count < -$length)
0 ignored issues
show
Bug introduced by
The variable $length does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
441
            return [];
442
        if(is_string($list)) {
443
            return ($count >= 0)
444
                ? substr($list, 0, $count)
445
                : substr($list, $count);
446
        }
447
        return ($count >= 0)
448
            ? array_slice($list, 0, $count)
449
            : array_slice($list, $count);
450
    };
451
    return apply(curry($take), func_get_args());
452
}
453
454
/**
455
 * Takes elements from an array while they match the given predicate. It stops at the 
456
 * first element not matching the predicate and does not include it in the result.
457
 * ```php
458
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
459
 * takeWhile(startsWith('F'), $items) // ['Foo', 'Fun']
460
 * takeWhile(startsWith('D'), $items) // []
461
 * ```
462
 *
463
 * @signature (a -> Boolean) -> [a] -> [a]
464
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
465
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
466
 * @return array
467
 */
468 View Code Duplication
function takeWhile() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
    $takeWhile = function($predicate, $list) {
470
        $first = head($list);
471
        return (null == $first || !$predicate($first))
472
            ? []
473
            : prepend($first, takeWhile($predicate, $list));
474
475
    };
476
    return apply(curry($takeWhile), func_get_args());
477
}
478
479
/**
480
 * Same as `takeWhile` but taking elements from the end of the array.
481
 * ```php
482
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
483
 * takeLastWhile(startsWith('B'), $items) // ['Bar', 'Baz']
484
 * takeLastWhile(startsWith('D'), $items) // []
485
 * ```
486
 *
487
 * @signature (a -> Boolean) -> [a] -> [a]
488
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
489
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
490
 * @return array
491
 */
492 View Code Duplication
function takeLastWhile() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
493
    $takeWhile = function($predicate, $list) {
0 ignored issues
show
Unused Code introduced by
$takeWhile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
494
        $last = last($list);
495
        return (null == $last || !$predicate($last))
496
            ? []
497
            : append($last, takeLastWhile($predicate, $list));
498
499
    };
500
    return apply(curry($takeLastWhile), func_get_args());
0 ignored issues
show
Bug introduced by
The variable $takeLastWhile does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
501
}
502
503
/**
504
 * Takes elements from an array **until** the predicate 
505
 * is satisfied, not including the satisfying element.
506
 * ```php
507
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
508
 * takeUntil(startsWith('B'), $items) // ['Foo', 'Fun', 'Dev']
509
 * takeUntil(startsWith('F'), $items) // []
510
 * ```
511
 *
512
 * @signature (a -> Boolean) -> [a] -> [a]
513
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
514
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
515
 * @return array
516
 */
517
function takeUntil() {
518
    $takeUntil = function($predicate, $list) {
519
        return takeWhile(pipe($predicate, not_()), $list);
520
    };
521
    return apply(curry($takeUntil), func_get_args());
522
}
523
524
/**
525
 * Same as `takeUntil` but takes elements from the end of the array.
526
 * ```php
527
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
528
 * takeLastUntil(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz']
529
 * takeLastUntil(startsWith('B'), $items) // []
530
 * ```
531
 *
532
 * @signature (a -> Boolean) -> [a] -> [a]
533
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
534
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
535
 * @return array
536
 */
537
function takeLastUntil() {
538
    $takeUntil = function($predicate, $list) {
539
        return takeLastWhile(pipe($predicate, not()), $list);
540
    };
541
    return apply(curry($takeUntil), func_get_args());
542
}
543
544
/**
545
 * Removes a number of elements from an array.
546
 * If `$count` is negative, the elements are
547
 * removed from the end of the array.
548
 * ```php
549
 * $items = ['Foo', 'Bar', 'Baz'];
550
 * remove(2, $items) // ['Baz']
551
 * remove(-1, $items) // ['Foo', 'Bar']
552
 * remove(5, $items) // []
553
 * remove(6, 'Hello World') // 'World'
554
 * remove(-6, 'Hello World') // 'Hello'
555
 * ```
556
 *
557
 * @signature Number -> [a] -> [a]
558
 * @signature Number -> String -> String
559
 * @param  int $count
0 ignored issues
show
Bug introduced by
There is no parameter named $count. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
560
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
561
 * @return array
562
 */
563
function remove() {
564
    $remove = function($count, $list) {
565
        $length = length($list);
566
        if ($count > $length || $count < -$length)
567
            return [];
568
        return ($count > 0) 
569
            ? take($count - $length)
570
            : take($count + $length);
571
    };
572
    return apply(curry($remove), func_get_args());
573
}
574
575
/**
576
 * Removes elements from an array while they match the given predicate. It stops at the 
577
 * first element not matching the predicate and does not remove it.
578
 * ```php
579
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
580
 * removeWhile(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz']
581
 * removeWhile(startsWith('D'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']
582
 * ```
583
 *
584
 * @signature (a -> Boolean) -> [a] -> [a]
585
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
586
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
587
 * @return array
588
 */
589
function removeWhile() {
590
    $removeWhile = function($predicate, $list) {
591
        return remove(length(takeWhile($predicate, $list)), $list);
592
    };
593
    return apply(curry($removeWhile), func_get_args());
594
}
595
596
/**
597
 * Same as `removeWhile` but removes elements from the end of the array.
598
 * ```php
599
 * $items = ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz'];
600
 * removeLastWhile(startsWith('F'), $items) // ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz']
601
 * removeLastWhile(startsWith('B'), $items) // ['Foo', 'Fun', 'Bye', 'Dev']
602
 * ```
603
 *
604
 * @signature (a -> Boolean) -> [a] -> [a]
605
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
606
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
607
 * @return array
608
 */
609
function removeLastWhile() {
610
    $removeLastWhile = function($predicate, $list) {
611
        return remove(- length(takeLastWhile($predicate, $list)), $list);
612
    };
613
    return apply(curry($removeLastWhile), func_get_args());
614
}
615
616
/**
617
 * Removes elements from an array **until** the predicate 
618
 * is satisfied, not removing the satisfying element.
619
 * ```php
620
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
621
 * removeUntil(startsWith('B'), $items) // ['Bar', 'Baz']
622
 * removeUntil(startsWith('F'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']
623
 * removeUntil(startsWith('A'), $items) // []
624
 * ```
625
 *
626
 * @signature (a -> Boolean) -> [a] -> [a]
627
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
628
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
629
 * @return array
630
 */
631
function removeUntil() {
632
    $removeUntil = function($predicate, $list) {
633
        return remove(length(takeUntil($predicate, $list)), $list);
634
    };
635
    return apply(curry($removeUntil), func_get_args());
636
}
637
638
/**
639
 * Same as `removeUntil` but removes elements from the end of the array.
640
 * ```php
641
 * $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'];
642
 * removeLastUntil(startsWith('B'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']
643
 * removeLastUntil(startsWith('F'), $items) // ['Foo', 'Fun']
644
 * removeLastUntil(startsWith('A'), $items) // []
645
 * ```
646
 *
647
 * @signature (a -> Boolean) -> [a] -> [a]
648
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
649
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
650
 * @return array
651
 */
652
function removeLastUntil() {
653
    $removeLastUntil = function($predicate, $list) {
654
        return remove(- length(takeLastUntil($predicate, $list)), $list);
655
    };
656
    return apply(curry($removeLastUntil), func_get_args());
657
}
658
659
/**
660
 * Converts an array of (key, value) pairs to an object (instance of `stdClass`).
661
 * ```php
662
 * fromPairs([['name', 'Foo'], ['age', 11]]); // (object) ['name' => 'Foo', 'age' => 11]
663
 * ```
664
 *
665
 * @signature [(k, v)] -> {k: v}
666
 * @param  array $pairs
0 ignored issues
show
Bug introduced by
There is no parameter named $pairs. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
667
 * @return stdClass
668
 */
669
function fromPairs() {
670
    $fromPairs = function($pairs) {
671
        return reduce(function($result, $pair) {
672
            $result->{$pair[0]} = $pair[1];
673
            return $result;
674
        }, new \stdClass, $pairs);
675
    };
676
    return apply(curry($fromPairs), func_get_args());
677
}
678
679
/**
680
 * Gets an array of slices of size `$size` from an array.
681
 * ```php
682
 * $pairs = slices(2);
683
 * $pairs([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]]
684
 * $pairs("Hello World"); // ['He', 'll', 'o ', 'Wo', 'rl', 'd']
685
 * slices(5, [1, 2]); // [[1, 2]]
686
 * slices(3, []) // []
687
 * slices(3, '') // ''
688
 * ```
689
 *
690
 * @signature Number -> [a] -> [[a]]
691
 * @signature Number -> String -> [String]
692
 * @param  int $size
0 ignored issues
show
Bug introduced by
There is no parameter named $size. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
693
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
694
 * @return array
695
 */
696
function slices() {
697
    $slices = function($size, $list) {
698
        if(empty($list))
699
            return is_string($list) ? '' : [];
700
        if(length($list) <= $size)
701
            return [$list];
702
        return prepend(take($size, $list), slices($size, remove($size, $list)));
703
    };
704
    return apply(curry($slices), func_get_args());
705
}
706
707
/**
708
 * Checks if an array contains an item.
709
 * ```php
710
 * contains('foo', ['foo', 'bar', 'baz']) // true
711
 * contains('hi', ['foo', 'bar', 'baz']) // false
712
 * contains('hi', 'Hello World') // false
713
 * contains('He', 'Hello World') // true
714
 * ```
715
 *
716
 * @signature a -> [a] -> Boolean
717
 * @signature String -> String -> Boolean
718
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
719
 * @param  array|string $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
720
 * @return bool
721
 */
722
function contains() {
723
    $contains = function($item, $list) {
724
        return null != indexOf($item, $list);
725
    };
726
    return apply(curry($contains), func_get_args());
727
}
728
729
/**
730
 * Returns the position/key of the first item satisfying the 
731
 * predicate in the array or null if no such element is found.
732
 * ```php
733
 * findIndex(startsWith('b'), ['foo', 'bar', 'baz']); // 1
734
 * findIndex(startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); // 'b'
735
 * findIndex(startsWith('c'), ['foo', 'bar', 'baz']); // null
736
 * ```
737
 *
738
 * @signature (a -> Boolean) -> [a] -> Maybe(Number)
739
 * @signature (v -> Boolean) -> {k: v} -> Maybe(k)
740
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
741
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
742
 * @return mixed
743
 */
744
function findIndex() {
745
    $findIndex = function($predicate, $list) {
746
        foreach ($list as $key => $value) {
747
            if ($predicate($value))
748
                return $key;
749
        }
750
        return null;
751
    };
752
    return apply(curry($findIndex), func_get_args());
753
}
754
755
/**
756
 * Returns the position/key of the last item satisfying the 
757
 * predicate in the array or null if no such element is found.
758
 * ```php
759
 * findLastIndex(startsWith('b'), ['foo', 'bar', 'baz']); // 2
760
 * findLastIndex(startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); // 'c'
761
 * findLastIndex(startsWith('c'), ['foo', 'bar', 'baz']); // null
762
 * ```
763
 *
764
 * @signature (a -> Boolean) -> [a] -> Maybe(Number)
765
 * @signature (v -> Boolean) -> {k: v} -> Maybe(k)
766
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
767
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
768
 * @return mixed
769
 */
770
function findLastIndex() {
771
    $findLastIndex = function($predicate, $list) {
772
        foreach (reverse(toPairs($list)) as $pair) {
773
            if($predicate($pair[1]))
774
                return $pair[0];
775
        }
776
        return null;
777
    };
778
    return apply(curry($findLastIndex), func_get_args());
779
}
780
781
/**
782
 * Returns the first item satisfying the predicate in 
783
 * the array or null if no such element is found.
784
 * ```php
785
 * find(startsWith('b'), ['foo', 'bar', 'baz']); // 'bar'
786
 * find(startsWith('c'), ['foo', 'bar', 'baz']); // null
787
 * ```
788
 *
789
 * @signature (a -> Boolean) -> [a] -> Maybe(a)
790
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
791
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
792
 * @return mixed
793
 */
794
function find() {
795
    $find = function($predicate, $list) {
796
        return get(findIndex($predicate, $list), $list);
797
    };
798
    return apply(curry($find), func_get_args());
799
}
800
801
/**
802
 * Returns the last item satisfying the predicate in 
803
 * the array or null if no such element is found.
804
 * ```php
805
 * findLast(startsWith('b'), ['foo', 'bar', 'baz']); // 'baz'
806
 * findLast(startsWith('c'), ['foo', 'bar', 'baz']); // null
807
 * ```
808
 *
809
 * @signature (a -> Boolean) -> [a] -> Maybe(a)
810
 * @param  callable $predicate
0 ignored issues
show
Bug introduced by
There is no parameter named $predicate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
811
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
812
 * @return mixed
813
 */
814
function findLast() {
815
    $findLast = function($predicate, $list) {
816
        return get(findLastIndex($predicate, $list), $list);
817
    };
818
    return apply(curry($findLast), func_get_args());
819
}
820
821
/**
822
 * If `$list` is an array, it returns the **key** of the first **item** which is equal to `$item` or `null` if not found.
823
 * If `$list` is a string, it returns the **position** of the first **substring** which is equal to `$item` or `null` if not found.
824
 * If `$list` is an object, it returns the **name** of the first **attribute** which is equal to `$item` or `null` if not found.
825
 * Note that elements are deeply compared using `Tarsana\Functional\equals`.
826
 * ```php
827
 * indexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); // 1
828
 * indexOf(['Hello'], [1, ['Hello', 'World'], true]); // -1
829
 * indexOf('World', 'Hello World'); // 6
830
 * indexOf('World !', 'Hello World'); // -1
831
 * indexOf('foo', (object) ['name' => 'foo', 'age' => 11]); // 'name'
832
 * ```
833
 *
834
 * @signature a -> [a] -> Maybe(Number) 
835
 * @signature v -> {k: v} -> Maybe(k)
836
 * @signature String -> String -> Maybe(Number)
837
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
838
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
839
 * @return int
840
 */
841 View Code Duplication
function indexOf() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
842
    $indexOf = function($item, $list) {
843
        if (is_string($list)) {
844
            $index = strpos($list, $item);
845
            return (-1 == $index)
846
                ? null
847
                : $index;
848
        }
849
        return findIndex(equals($item), $list);
850
    };
851
    return apply(curry($indexOf), func_get_args());
852
}
853
854
/**
855
 * Same as `indexOf` but returns the key/position/name of the last item/substring/attribute.
856
 * ```php
857
 * lastIndexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); // 3
858
 * lastIndexOf(['Hello'], [1, ['Hello', 'World'], true]); // -1
859
 * lastIndexOf('World', 'Hello World'); // 6
860
 * lastIndexOf('World !', 'Hello World'); // -1
861
 * lastIndexOf('foo', (object) ['name' => 'foo', 'age' => 11]); // 'name'
862
 * ```
863
 *
864
 * @signature a -> [a] -> Number 
865
 * @signature String -> String -> Number
866
 * @param  mixed $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
867
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
868
 * @return int
869
 */
870 View Code Duplication
function lastIndexOf() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
871
    $lastIndexOf = function($item, $list) {
872
        if (is_string($list)) {
873
            $index = strrpos($list, $item);
874
            return (-1 == $index)
875
                ? null
876
                : $index;
877
        }
878
        return findLastIndex(equals($item), $list);
879
    };
880
    return apply(curry($lastIndexOf), func_get_args());
881
}
882
883
/**
884
 * Removes duplicates from a list by keeping the first occurence of each group 
885
 * of items which are equivalent according to the given `$areEqual` callable.
886
 * ```php
887
 * uniqueBy(eq(), [1, '2', '1', 3, '3', 2, 2]); // [1, '2', 3]
888
 * ```
889
 *
890
 * @signature (a -> a -> Boolean) -> [a] -> [a]
891
 * @param  callable $areEqual
0 ignored issues
show
Bug introduced by
There is no parameter named $areEqual. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
892
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
893
 * @return array
894
 */
895
function uniqueBy() {
896
    $uniqueBy = function($areEqual, $list) {
897
        // make sure the compare function is curried
898
        $compare = function($a) use($areEqual) {
0 ignored issues
show
Unused Code introduced by
$compare is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
The parameter $a is not used and could be removed.

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

Loading history...
899
            return function($b) use($areEqual) {
900
                return $areEqual($a, $b);
0 ignored issues
show
Bug introduced by
The variable $a does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
901
            };
902
        };
903
904
        return reduce(function($result, $item) {
905
            return (null == findIndex($compare($item), $result))
0 ignored issues
show
Bug introduced by
The variable $compare does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
906
                ? append($item, $result)
907
                : $result;
908
        }, [], $list);
909
    };
910
    return apply(curry($uniqueBy), func_get_args());
911
}
912
913
/**
914
 * Alias of `uniqueBy(equals())`.
915
 * ```php
916
 * unique([1, '1', [1, 2], 1, ['1', 2], [1, 2]]); // [1, '1', [1, 2], ['1', 2]]
917
 * ```
918
 *
919
 * @signature [a] -> [a]
920
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
921
 * @return array
922
 */
923
function unique() {
924
    return apply(curry(uniqueBy(equals())), func_get_args());
925
}
926
927
/**
928
 * Converts an array to an associative array, based on the result of calling `$fn` 
929
 * on each element, and grouping the results according to values returned.
930
 * Note that `$fn` should take an item from the list and return a string.
931
 * ```php
932
 * $persons = [
933
 *     ['name' => 'foo', 'age' => 11],
934
 *     ['name' => 'bar', 'age' => 9],
935
 *     ['name' => 'baz', 'age' => 16],
936
 *     ['name' => 'zeta', 'age' => 33],
937
 *     ['name' => 'beta', 'age' => 25]
938
 * ];
939
 * $phase = function($person) {
940
 *     $age = $person['age'];
941
 *     return $age < 13 ? 'child' :
942
 *            $age < 19 ? 'teenager' :
943
 *            'adult';
944
 * };
945
 * groupBy($phase, $persons);
946
 * // [
947
 * //  'child' => [['name' => 'foo', 'age' => 11], ['name' => 'bar', 'age' => 9]],
948
 * //  'teenager' => [['name' => 'baz', 'age' => 16]],
949
 * //  'adult' => [['name' => 'zeta', 'age' => 33], ['name' => 'beta', 'age' => 25]]
950
 * // ]
951
 * ```
952
 *
953
 * @signature (a -> String) -> [a] -> {String: a}
954
 * @param  callable $fn
0 ignored issues
show
Bug introduced by
There is no parameter named $fn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
955
 * @param  array $list
0 ignored issues
show
Bug introduced by
There is no parameter named $list. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
956
 * @return array
957
 */
958
function groupBy() {
959
    $groupBy = function($fn, $list) {
0 ignored issues
show
Unused Code introduced by
$groupBy is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
960
        return reduce(function($result, $item) use($fn) {
961
            $index = $fn($item);
962
            if (! isset($result[$index]))
963
                $result[$index] = [];
964
            $result[$index][] = $item;
965
            return $result;
966
        }, [], $list);
967
    };
968
    return apply(curry(uniqueBy(equals())), func_get_args());
969
}
970