MappingTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 184
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A mapKeys() 0 3 1
A map() 0 3 1
A apply() 0 3 1
A chunk() 0 3 1
A group() 0 3 1
A flatten() 0 3 1
A project() 0 3 1
A keys() 0 3 1
A fill() 0 3 1
A column() 0 3 1
A flip() 0 3 1
A values() 0 3 1
A unwind() 0 3 1
A reshape() 0 3 1
A setKeys() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved\IteratorPipeline\Traits;
6
7
use Improved as i;
8
use Improved\Iterator\CombineIterator;
9
10
/**
11
 * Mapping and projection methods for iterator pipeline
12
 */
13
trait MappingTrait
14
{
15
    /**
16
     * Define the next step via a callback that returns an array or Traversable object.
17
     *
18
     * @param callable $callback
19
     * @param mixed    ...$args
20
     * @return static
21
     */
22
    abstract public function then(callable $callback, ...$args);
23
24
25
    /**
26
     * Map each element to a value using a callback function.
27
     *
28
     * @param callable $callback
29
     * @return static
30
     */
31 3
    public function map(callable $callback)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::map() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
32
    {
33 3
        return $this->then("Improved\iterable_map", $callback);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::map() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
34
    }
35
36
    /**
37
     * Map the key of each element to a new key using a callback function.
38
     *
39
     * @param callable $callback
40
     * @return static
41
     */
42 1
    public function mapKeys(callable $callback)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::mapKeys() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
43
    {
44 1
        return $this->then("Improved\iterable_map_keys", $callback);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::mapKeys() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
45
    }
46
47
    /**
48
     * Apply a callback to each element of an iterator.
49
     * Any value returned by the callback is ignored.
50
     *
51
     * @param callable $callback
52
     * @return static
53
     */
54 3
    public function apply(callable $callback)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::apply() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
55
    {
56 3
        return $this->then("Improved\iterable_apply", $callback);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::apply() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
57
    }
58
59
    /**
60
     * Divide iterable into chunks of specified size.
61
     *
62
     * @param int $size
63
     * @return static
64
     */
65 1
    public function chunk(int $size)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::chunk() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
66
    {
67 1
        return $this->then("Improved\iterable_chunk", $size);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::chunk() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
68
    }
69
70
    /**
71
     * Group elements of an iterator, with the group name as key and an array of elements as value.
72
     *
73
     * @param callable $grouping
74
     * @return static
75
     */
76 1
    public function group(callable $grouping)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::group() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
77
    {
78 1
        return $this->then("Improved\iterable_group", $grouping);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::group() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
79
    }
80
81
    /**
82
     * Walk through all sub-iterables and concatenate them.
83
     *
84
     * @param bool $preserveKeys
85
     * @return static
86
     */
87 2
    public function flatten(bool $preserveKeys = false)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::flatten() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
88
    {
89 2
        return $this->then("Improved\iterable_flatten", $preserveKeys);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::flatten() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
90
    }
91
92
    /**
93
     * Deconstruct an iterable property/item for each element. The result is one element for each item in the iterable
94
     * property.
95
     *
96
     * @param string      $column
97
     * @param string|null $mapKey        The name of a new property to hold the array index of the element
98
     * @param bool        $preserveKeys  Preserve the keys of the iterable (will result in duplicate keys)
99
     * @return static
100
     */
101 1
    public function unwind(string $column, ?string $mapKey = null, bool $preserveKeys = false)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::unwind() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
102
    {
103 1
        return $this->then("Improved\iterable_unwind", $column, $mapKey, $preserveKeys);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::unwind() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
104
    }
105
106
    /**
107
     * Set all values of the iterable.
108
     *
109
     * @param mixed $value
110
     * @return static
111
     */
112 1
    public function fill($value)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::fill() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
113
    {
114 1
        return $this->then("Improved\iterable_fill", $value);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::fill() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
115
    }
116
117
    /**
118
     * Return the values from a single column / property.
119
     * Create key/value pairs by specifying the key.
120
     *
121
     * @param string|int|null $valueColumn  null to keep values
122
     * @param string|int|null $keyColumn    null to keep keys
123
     * @return static
124
     */
125 2
    public function column($valueColumn, $keyColumn = null)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::column() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
126
    {
127 2
        return $this->then("Improved\iterable_column", $valueColumn, $keyColumn);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::column() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
128
    }
129
130
    /**
131
     * Project each element of an iterator to an associated (or numeric) array.
132
     * Each element should be an array or object.
133
     *
134
     * @param array $mapping  [new key => old key, ...]
135
     * @return static
136
     */
137 1
    public function project(array $mapping)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::project() has parameter $mapping with no value type specified in iterable type array.
Loading history...
introduced by
Method Improved\IteratorPipeline\Pipeline::project() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
introduced by
Method Improved\IteratorPipeline\PipelineBuilder::project() has parameter $mapping with no value type specified in iterable type array.
Loading history...
138
    {
139 1
        return $this->then("Improved\iterable_project", $mapping);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::project() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
140
    }
141
142
    /**
143
     * Reshape each element of an iterator, adding or removing properties or keys.
144
     *
145
     * @param array $columns  [key => bool|string|int, ...]
146
     * @return static
147
     */
148 1
    public function reshape(array $columns)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::reshape() has parameter $columns with no value type specified in iterable type array.
Loading history...
introduced by
Method Improved\IteratorPipeline\Pipeline::reshape() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
introduced by
Method Improved\IteratorPipeline\PipelineBuilder::reshape() has parameter $columns with no value type specified in iterable type array.
Loading history...
149
    {
150 1
        return $this->then("Improved\iterable_reshape", $columns);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::reshape() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
151
    }
152
153
154
    /**
155
     * Keep the values, drop the keys. The keys become an incremental number.
156
     *
157
     * @return static
158
     */
159 8
    public function values()
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::values() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
160
    {
161 8
        return $this->then("Improved\iterable_values");
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::values() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
162
    }
163
164
    /**
165
     * Use the keys as values. The keys become an incremental number.
166
     *
167
     * @return static
168
     */
169 1
    public function keys()
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::keys() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
170
    {
171 1
        return $this->then("Improved\iterable_keys");
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::keys() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
172
    }
173
174
    /**
175
     * Use another iterator as keys and the current iterator as values.
176
     *
177
     * @param iterable $keys
178
     * @return static
179
     */
180 4
    public function setKeys(iterable $keys)
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::setKeys() has parameter $keys with no value type specified in iterable type iterable.
Loading history...
introduced by
Method Improved\IteratorPipeline\Pipeline::setKeys() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
181
    {
182 4
        $combine = function ($values, $keys) {
0 ignored issues
show
introduced by
Anonymous function should have native return typehint "Improved\Iterator\CombineIterator".
Loading history...
183 4
            return new CombineIterator($keys, $values);
184 4
        };
185
186 4
        return $this->then($combine, $keys);
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::setKeys() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
187
    }
188
189
    /**
190
     * Use values as keys and visa versa.
191
     *
192
     * @return static
193
     */
194 1
    public function flip()
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::flip() return type has no value type specified in iterable type static(Improved\IteratorPipeline\Pipeline).
Loading history...
195
    {
196 1
        return $this->then("Improved\iterable_flip");
0 ignored issues
show
introduced by
Method Improved\IteratorPipeline\Pipeline::flip() should return static(Improved\IteratorPipeline\Pipeline) but returns Improved\IteratorPipeline\Pipeline.
Loading history...
197
    }
198
}
199