1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
// phpcs:disable Zicht.Commenting.FunctionComment.ExtraParamComment |
7
|
|
|
|
8
|
|
|
namespace Zicht\Itertools\lib\Traits; |
9
|
|
|
|
10
|
|
|
use Zicht\Itertools\conversions; |
11
|
|
|
use Zicht\Itertools\lib\MapIterator; |
12
|
|
|
|
13
|
|
|
trait MapTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Make an iterator that applies $strategy to every entry in this iterable |
17
|
|
|
* |
18
|
|
|
* If additional iterables are passed, $strategy is called for each entry |
19
|
|
|
* in the $iterable, where the first argument is the value and the |
20
|
|
|
* second argument is the key of the entry |
21
|
|
|
* |
22
|
|
|
* If additional iterables are passed, $strategy is called with the |
23
|
|
|
* values and the keys from the iterables. For example, the first |
24
|
|
|
* call to $strategy will be: |
25
|
|
|
* $strategy($value_iterable1, $value_iterable2, $key_iterable2, $key_iterable2) |
26
|
|
|
* |
27
|
|
|
* With multiple iterables, the iterator stops when the shortest |
28
|
|
|
* iterable is exhausted. |
29
|
|
|
* |
30
|
|
|
* > $minimal = function ($value) { return min(3, $value); }; |
31
|
|
|
* > iter\iterable([1, 2, 3, 4])->map($minimal); |
32
|
|
|
* 3 3 3 4 |
33
|
|
|
* |
34
|
|
|
* > $average = function ($value1, $value2) { return ($value1 + $value2) / 2; }; |
35
|
|
|
* > iter\iterable([1, 2, 3])->map($average, [4, 5, 6]); |
36
|
|
|
* 2.5 3.5 4.5 |
37
|
|
|
* |
38
|
|
|
* @param null|string|\Closure $strategy |
39
|
|
|
* @param array|string|\Iterator $iterable |
|
|
|
|
40
|
|
|
* @param array|string|\Iterator $iterable2 |
|
|
|
|
41
|
|
|
* @return MapIterator |
42
|
|
|
*/ |
43
|
86 |
|
public function map($strategy /*, $iterable, $iterable2, ... */) |
44
|
|
|
{ |
45
|
86 |
|
if ($this instanceof \Iterator) { |
46
|
85 |
|
if (func_num_args() > 1) { |
47
|
13 |
|
$iterables = array_map( |
48
|
13 |
|
'\Zicht\Itertools\conversions\mixed_to_iterator', |
49
|
13 |
|
array_slice(func_get_args(), 1) |
50
|
|
|
); |
51
|
|
|
} else { |
52
|
72 |
|
$iterables = []; |
53
|
|
|
} |
54
|
85 |
|
$reflectorClass = new \ReflectionClass('\Zicht\Itertools\lib\MapIterator'); |
55
|
85 |
|
return $reflectorClass->newInstanceArgs( |
56
|
85 |
|
array_merge([conversions\mixed_to_value_getter($strategy), $this], $iterables) |
|
|
|
|
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return null; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.