1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pipes; |
4
|
|
|
|
5
|
|
|
use Pipes\Iterator\AppendIterator; |
6
|
|
|
|
7
|
|
|
trait PipenessTrait |
8
|
|
|
{ |
9
|
|
|
// use Filter\AppendTrait; |
10
|
|
|
use Filter\ChunkTrait; |
11
|
|
|
use Filter\EachTrait; |
12
|
|
|
use Concept\EmitTrait; |
13
|
|
|
use Filter\FilesTrait; |
14
|
|
|
use Filter\FilterTrait; |
15
|
|
|
use Filter\LimitTrait; |
16
|
|
|
use Filter\MapTrait; |
17
|
|
|
use Filter\SkipTrait; |
18
|
|
|
use Filter\ValuesTrait; |
19
|
|
|
use Filter\SleepTrait; |
20
|
|
|
use Filter\InfiniteTrait; |
21
|
|
|
use Filter\StopIfTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \Iterator $iterator |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
protected function chainWith(\Iterator $iterator) |
29
|
|
|
{ |
30
|
|
|
$this->var = $iterator; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function toArray() |
36
|
|
|
{ |
37
|
|
|
$iterator = $this->var; |
38
|
|
|
|
39
|
|
|
return iterator_to_array($iterator, true); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns a complete iterator. |
44
|
|
|
* (an instance of \IteratorIterator which in turn |
45
|
|
|
* implements \OuterIterator). |
46
|
|
|
* |
47
|
|
|
* Use this method if you need to comply with type-hinting |
48
|
|
|
* from external libraries |
49
|
|
|
* |
50
|
|
|
* @return \IteratorIterator |
51
|
|
|
*/ |
52
|
|
|
public function toIterator() |
53
|
|
|
{ |
54
|
|
|
$iterator = $this->unwrap(); |
55
|
|
|
|
56
|
|
|
if (is_array($iterator)) { |
57
|
|
|
$iterator = new \ArrayIterator($iterator); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$appendIterator = new AppendIterator(); |
61
|
|
|
$appendIterator->append($iterator); |
62
|
|
|
|
63
|
|
|
return new PipeIterator($appendIterator); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// /** |
|
|
|
|
67
|
|
|
// * @return \Traversable |
68
|
|
|
// */ |
69
|
|
|
// protected function getRoot() |
70
|
|
|
// { |
71
|
|
|
// return $this->getBaseOfChain($this, true); |
72
|
|
|
// } |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the latest non pipe Iterator/Traversable in the |
76
|
|
|
* chain. |
77
|
|
|
* |
78
|
|
|
* @return \Traversable |
79
|
|
|
*/ |
80
|
|
|
public function unwrap() |
81
|
|
|
{ |
82
|
|
|
$iterator = func_num_args() ? func_get_arg(0) : $this; |
83
|
|
|
|
84
|
|
|
return $this->getBaseOfChain($iterator); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getBaseOfChain($iterator, $pipeInstance = false) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$last = $iterator; |
|
|
|
|
90
|
|
|
while (true) { |
91
|
|
|
switch (true) { |
92
|
|
|
case is_a($iterator, '\\Pipes\\PipeIterator'): |
93
|
|
|
$last = $iterator; |
94
|
|
|
$iterator = $last->getInnerIterator(); |
95
|
|
|
break; |
96
|
|
|
case is_a($iterator, '\\Pipes\\Pipe'): |
97
|
|
|
$last = $iterator; |
98
|
|
|
$iterator = $last->getIterator(); |
99
|
|
|
break; |
100
|
|
|
case is_a($iterator, '\\ArrayIterator'): |
101
|
|
|
$iterator = $iterator->getArrayCopy(); |
102
|
|
|
break; |
103
|
|
|
default: |
104
|
|
|
// --- was used by getRoot() |
|
|
|
|
105
|
|
|
// if ($pipeInstance) { |
106
|
|
|
// return $last; |
107
|
|
|
// } |
108
|
|
|
|
109
|
|
|
return $iterator; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} // @codeCoverageIgnore |
113
|
|
|
|
114
|
|
|
protected function executeCallback($______callback, $______allArgs, $value, $key, $iterator) |
115
|
|
|
{ |
116
|
|
|
return call_user_func_array( |
117
|
|
|
$______callback, |
118
|
|
|
$______allArgs ? [$value, $key, $iterator] : [$value] |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.