FilteringTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 102
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 3 1
A limit() 0 3 1
A slice() 0 3 1
A cleanup() 0 3 1
A unique() 0 3 1
A after() 0 3 1
A before() 0 3 1
A uniqueKeys() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved\IteratorPipeline\Traits;
6
7
/**
8
 * Filtering methods for iterator pipeline.
9
 */
10
trait FilteringTrait
11
{
12
    /**
13
     * Define the next step via a callback that returns an array or Traversable object.
14
     *
15
     * @param callable $callback
16
     * @param mixed    ...$args
17
     * @return static
18
     */
19
    abstract public function then(callable $callback, ...$args);
20
21
22
    /**
23
     * Eliminate elements based on a criteria.
24
     *
25
     * @param callable $matcher
26
     * @return static
27
     */
28 1
    public function filter(callable $matcher)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::filter() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
29
    {
30 1
        return $this->then("Improved\iterable_filter", $matcher);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::filter() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
31
    }
32
33
    /**
34
     * Filter out `null` values from iteratable.
35
     *
36
     * @return static
37
     */
38 1
    public function cleanup()
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::cleanup() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
39
    {
40 1
        return $this->then("Improved\iterable_cleanup");
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::cleanup() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
41
    }
42
43
    /**
44
     * Filter on unique elements.
45
     *
46
     * @param callable|null $grouper  If provided, filtering will be based on return value.
47
     * @return static
48
     */
49 6
    public function unique(?callable $grouper = null)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::unique() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
50
    {
51 6
        return $this->then("Improved\iterable_unique", $grouper);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::unique() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
52
    }
53
54
    /**
55
     * Filter our duplicate keys.
56
     * Unlike associative arrays, the keys of iterators don't have to be unique.
57
     *
58
     * @return static
59
     */
60 1
    public function uniqueKeys()
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::uniqueKeys() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
61
    {
62 1
        return $this->then("Improved\iterable_unique", function ($value, $key) {
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::uniqueKeys() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
63 1
            return $key;
64 1
        });
65
    }
66
67
    /**
68
     * Get only the first elements of an iterator.
69
     *
70
     * @param int $size
71
     * @return static
72
     */
73 3
    public function limit(int $size)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::limit() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
74
    {
75 3
        return $this->then("Improved\iterable_slice", 0, $size);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::limit() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
76
    }
77
78
    /**
79
     * Get a limited subset of the elements using an offset.
80
     *
81
     * @param int      $offset
82
     * @param int|null $size    size limit
83
     * @return static
84
     */
85 1
    public function slice(int $offset, ?int $size = null)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::slice() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
86
    {
87 1
        return $this->then("Improved\iterable_slice", $offset, $size);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::slice() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
88
    }
89
90
    /**
91
     * Get elements until a match is found.
92
     *
93
     * @param callable $matcher
94
     * @param bool     $include
95
     * @return static
96
     */
97 2
    public function before(callable $matcher, bool $include = false)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::before() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
98
    {
99 2
        return $this->then("Improved\iterable_before", $matcher, $include);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::before() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
100
    }
101
102
    /**
103
     * Get elements after a match is found.
104
     *
105
     * @param callable $matcher
106
     * @param bool     $include
107
     * @return static
108
     */
109 2
    public function after(callable $matcher, bool $include = false)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::after() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
110
    {
111 2
        return $this->then("Improved\iterable_after", $matcher, $include);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::after() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
112
    }
113
}
114