Pipeline   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 94
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 3 1
A with() 0 3 2
A then() 0 9 2
A getIterator() 0 3 1
A toArray() 0 3 1
A walk() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved\IteratorPipeline;
6
7
use Improved as i;
8
9
/**
10
 * Functional-style operations, such as map-reduce transformations on arrays and iterators.
11
 * A pipeline uses Generators, meaning it can be used only once.
12
 */
13
class Pipeline implements \IteratorAggregate
0 ignored issues
show
introduced by
Class Improved\IteratorPipeline\Pipeline implements generic interface IteratorAggregate but does not specify its types: TKey, TValue
Loading history...
14
{
15
    use Traits\MappingTrait;
16
    use Traits\FilteringTrait;
17
    use Traits\SortingTrait;
18
    use Traits\TypeHandlingTrait;
19
    use Traits\FindingTrait;
20
    use Traits\AggregationTrait;
21
22
    /**
23
     * @var iterable
24
     */
25
    protected $iterable;
0 ignored issues
show
introduced by
Property Improved\IteratorPipeline\Pipeline::$iterable type has no value type specified in iterable type iterable.
Loading history...
26
27
    /**
28
     * Pipeline constructor.
29
     *
30
     * @param iterable $iterable
31
     */
32 17
    public function __construct(iterable $iterable)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::__construct() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
33
    {
34 17
        $this->iterable = $iterable;
35 17
    }
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 self
43
     */
44 4
    public function then(callable $callback, ...$args): self
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::then() return type has no value type specified in iterable type Improved\IteratorPipeline\Pipeline.
Loading history...
45
    {
46 4
        $this->iterable = i\type_check(
47 4
            $callback($this->iterable, ...$args),
48 4
            'iterable',
49 4
            new \UnexpectedValueException("Expected step to return an array or Traversable, %s returned")
50
        );
51
52 3
        return $this->iterable instanceof Pipeline ? $this->iterable : $this;
53
    }
54
55
56
    /**
57
     * Get iterator.
58
     *
59
     * @return \Iterator
60
     */
61 10
    public function getIterator(): \Iterator
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::getIterator() return type has no value type specified in iterable type Iterator.
Loading history...
62
    {
63 10
        return i\iterable_to_iterator($this->iterable);
64
    }
65
66
    /**
67
     * Get iterable as array.
68
     *
69
     * @return array
70
     */
71 4
    public function toArray(): array
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::toArray() return type has no value type specified in iterable type array.
Loading history...
72
    {
73 4
        return i\iterable_to_array($this->iterable, true);
74
    }
75
76
    /**
77
     * Traverse over the iterator, not capturing the values.
78
     * This is particularly useful after `apply()`.
79
     *
80
     * @return void
81
     */
82 1
    public function walk(): void
83
    {
84 1
        i\iterable_walk($this->iterable);
85 1
    }
86
87
88
    /**
89
     * Factory method
90
     *
91
     * @param iterable $iterable
92
     * @return static
93
     */
94 5
    final public static function with(iterable $iterable): self
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::with() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
introduced by
Method Improved\IteratorPipeline\Pipeline::with() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
95
    {
96 5
        return $iterable instanceof static ? $iterable : new static($iterable);
0 ignored issues
show
introduced by
Unsafe usage of new static().
Loading history...
97
    }
98
99
    /**
100
     * Factory method for PipelineBuilder
101
     *
102
     * @return PipelineBuilder
103
     */
104 1
    public static function build(): PipelineBuilder
105
    {
106 1
        return new PipelineBuilder();
107
    }
108
}
109