Failed Conditions
Push — master ( dcb275...c3bea1 )
by Arnold
02:34
created

Pipeline::then()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ipl\IteratorPipeline;
6
7
use Ipl as i;
8
use function Jasny\expect_type;
9
10
/**
11
 * Functional-style operations, such as map-reduce transformations on arrays and iterators.
12
 * A pipeline uses Generators, meaning it can be used only once.
13
 */
14
class Pipeline implements \IteratorAggregate
15
{
16
    use Traits\MappingTrait;
17
    use Traits\FilteringTrait;
18
    use Traits\SortingTrait;
19
    use Traits\FindingTrait;
20
    use Traits\AggregationTrait;
21
22
    /**
23
     * @var iterable
24
     */
25
    protected $iterable;
26
27
    /**
28
     * Pipeline constructor.
29
     *
30
     * @param iterable $iterable
31
     */
32 15
    public function __construct(iterable $iterable)
33
    {
34 15
        $this->iterable = $iterable;
35 15
    }
36
37
    /**
38
     * Define the next step via a callback that returns an array or Traversable object.
39
     *
40
     * @param callable $callback
41
     * @param mixed    ...$args
42
     * @return $this
43
     */
44 3
    public function then(callable $callback, ...$args): self
45
    {
46 3
        $next = $callback($this->iterable, ...$args);
47
48 3
        expect_type(
49 3
            $next,
50 3
            'iterable',
51 3
            \UnexpectedValueException::class,
52 3
            "Expected an array or Traversable, %s returned"
53
        );
54
55 2
        $this->iterable = $next;
56
57 2
        return $this;
58
    }
59
60
61
    /**
62
     * Get iterator.
63
     *
64
     * @return \Iterator
65
     */
66 4
    public function getIterator(): \Iterator
67
    {
68 4
        return i\iterable_to_iterator($this->iterable);
1 ignored issue
show
Bug introduced by
The function iterable_to_iterator was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        return /** @scrutinizer ignore-call */ i\iterable_to_iterator($this->iterable);
Loading history...
69
    }
70
71
    /**
72
     * Get iterable as array.
73
     *
74
     * @return array
75
     */
76 4
    public function toArray(): array
77
    {
78 4
        return i\iterable_to_array($this->iterable, true);
1 ignored issue
show
Bug introduced by
The function iterable_to_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        return /** @scrutinizer ignore-call */ i\iterable_to_array($this->iterable, true);
Loading history...
79
    }
80
81
    /**
82
     * Traverse over the iterator, not capturing the values.
83
     * This is particularly useful after `apply()`.
84
     *
85
     * @return void
86
     */
87 1
    public function walk(): void
88
    {
89 1
        i\iterable_walk($this->iterable);
1 ignored issue
show
Bug introduced by
The function iterable_walk was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        /** @scrutinizer ignore-call */ 
90
        i\iterable_walk($this->iterable);
Loading history...
90 1
    }
91
92
93
    /**
94
     * Factory method
95
     *
96
     * @param iterable $iterable
97
     * @return static
98
     */
99 4
    final public static function with(iterable $iterable): self
100
    {
101 4
        return new static($iterable);
102
    }
103
104
    /**
105
     * Factory method for PipelineBuilder
106
     *
107
     * @return PipelineBuilder
108
     */
109 1
    public static function build(): PipelineBuilder
110
    {
111 1
        return new PipelineBuilder();
112
    }
113
}
114