1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pipes\Filter; |
4
|
|
|
|
5
|
|
|
use Pipes\Concept\Emittable; |
6
|
|
|
|
7
|
|
|
trait MapTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Tranforms each element into something else using a callback |
11
|
|
|
* (rough equivalent of array_map()). |
12
|
|
|
* |
13
|
|
|
* The callback should return the new element. |
14
|
|
|
* |
15
|
|
|
* To also change the key, return<code>p()->emit($key,$value)</code> |
16
|
|
|
* <code> |
17
|
|
|
* p()->map(function ($value, $key, $iterator) { |
18
|
|
|
* return p()->emit($key.'_new', $value); |
19
|
|
|
* }); |
20
|
|
|
* |
21
|
|
|
* |
22
|
|
|
* @param callable $callback |
|
|
|
|
23
|
|
|
* |
24
|
|
|
* @return \Pipes\Pipe |
25
|
|
|
*/ |
26
|
|
|
public function map(callable $______callback) |
27
|
|
|
{ |
28
|
|
|
$iterator = $this->getIterator(); |
|
|
|
|
29
|
|
|
$pipe = $this; |
30
|
|
|
|
31
|
|
|
if (!is_array($______callback)) { |
32
|
|
|
$reflectionFunction = new \ReflectionFunction($______callback); |
33
|
|
|
} else { |
34
|
|
|
$reflectionFunction = new \ReflectionMethod($______callback[0], $______callback[1]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($reflectionFunction->isGenerator()) { |
38
|
|
|
$generator = $______callback; |
39
|
|
|
} else { |
40
|
|
|
$generator = function ($iterator) use ($pipe, $______callback) { |
41
|
|
|
foreach ($iterator as $key => $value) { |
42
|
|
|
// yield $key => $pipe->executeCallback($______callback, true, $value, $key, $iterator); |
|
|
|
|
43
|
|
|
$value = $pipe->executeCallback($______callback, true, $value, $key, $iterator); |
|
|
|
|
44
|
|
|
if ($value instanceof Emittable) { |
45
|
|
|
if ($value->hasKey()) { |
46
|
|
|
$key = $value->getKey(); |
47
|
|
|
} |
48
|
|
|
$value = $value->getValue(); |
49
|
|
|
} |
50
|
|
|
yield $key => $value; |
51
|
|
|
} |
52
|
|
|
}; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->chainWith($generator($iterator)); |
|
|
|
|
56
|
|
|
// return $this->chainWith(new \Pipes\Iterator\MapIterator($this->toIterator(), $callback)); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.