Completed
Push — master ( e86954...7cbcb1 )
by Ítalo
02:48
created

StrictKeyedIterableTrait::take()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\ArrayList;
6
use Collections\Dictionary;
7
use Collections\Iterable;
8
9
trait StrictKeyedIterableTrait
10
{
11
    use CommonMutableContainerTrait;
12
13
    public function concatAll()
14
    {
15
        $results = new static();
16
        $this->each(function (Iterable $subArray) use ($results) {
17
            $subArray->each(function ($item, $key) use ($results) {
18
                $results->add($key, $item);
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
            });
20
        });
21
22
        return $results;
23
    }
24
25
    public function keys()
26
    {
27
        $res = new ArrayList();
28
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
29
            $res[] = $k;
30
        }
31
32
        return $res;
33
    }
34
35
    public function map(callable $callback)
36
    {
37
        $res = new Dictionary();
38
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
39
            $res[$k] = $callback($v);
40
        }
41
42
        return $res;
43
    }
44
45
    public function mapWithKey($callback)
46
    {
47
        $res = new Dictionary();
48
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
49
            $res[$k] = $callback($k, $v);
50
        }
51
52
        return $res;
53
    }
54
55
    public function filter(callable $callback)
56
    {
57
        $res = new Dictionary();
58
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
59
            if ($callback($v)) {
60
                $res[$k] = $v;
61
            }
62
        }
63
64
        return $res;
65
    }
66
67
    public function filterWithKey($callback)
68
    {
69
        $res = new Dictionary();
70
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
71
            if ($callback($k, $v)) {
72
                $res[$k] = $v;
73
            }
74
        }
75
76
        return $res;
77
    }
78
79
    public function zip($iterable)
80
    {
81
        $res = new Dictionary();
82
        $it = $iterable->getIterator();
83
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
84
            if (!$it->valid()) {
85
                break;
86
            }
87
            $res[$k] = new Pair($v, $it->current());
88
            $it->next();
89
        }
90
91
        return $res;
92
    }
93
94
    public function take($size = 1)
95
    {
96
        $res = new Dictionary();
97
        if ($size <= 0) {
98
            return $res;
99
        }
100
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
101
            $res[$k] = $v;
102
            if (--$size === 0) {
103
                break;
104
            }
105
        }
106
107
        return $res;
108
    }
109
110
    public function takeWhile($fn)
111
    {
112
        $res = new Dictionary();
113
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
114
            if (!$fn($v)) {
115
                break;
116
            }
117
            $res[$k] = $v;
118
        }
119
120
        return $res;
121
    }
122
123
    public function skip($n)
124
    {
125
        $res = new Dictionary();
126
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
127
            if ($n <= 0) {
128
                $res[$k] = $v;
129
            } else {
130
                --$n;
131
            }
132
        }
133
134
        return $res;
135
    }
136
137
    public function skipWhile($fn)
138
    {
139
        $res = new Dictionary();
140
        $skip = true;
141
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
142
            if ($skip) {
143
                if ($fn($v)) {
144
                    continue;
145
                }
146
                $skip = false;
147
            }
148
            $res[$k] = $v;
149
        }
150
151
        return $res;
152
    }
153
154
    public function slice($start, $lenght)
155
    {
156
        $res = new Dictionary();
157
        if ($lenght <= 0) {
158
            return $res;
159
        }
160
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
161
            if ($start !== 0) {
162
                --$start;
163
                continue;
164
            }
165
            $res[$k] = $v;
166
            if (--$lenght === 0) {
167
                break;
168
            }
169
        }
170
171
        return $res;
172
    }
173
174 1
    public function concat(\Traversable $iterable)
175
    {
176 1
        $res = [];
177
178 1
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
179 1
            $res[$k] = $v;
180
        }
181
182 1
        foreach ($iterable as $k => $v) {
183 1
            $res[$k] = $v;
184
        }
185 1
        $this->container = $res;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
186
187 1
        return $this;
188
    }
189
190
    public function first()
191
    {
192
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
193
            return $v;
194
        }
195
196
        return null;
197
    }
198
199
    public function firstKey()
200
    {
201
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
202
            return $k;
203
        }
204
205
        return null;
206
    }
207
208
    public function last()
209
    {
210
        $v = null;
211
        foreach ($this as $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
212
        }
213
214
        return $v;
215
    }
216
217
    public function lastKey()
218
    {
219
        $k = null;
220
        foreach ($this as $k => $_) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
221
        }
222
223
        return $k;
224
    }
225
226
    /**
227
     * {@inheritDoc}
228
     * @return $this
229
     */
230
    public function each(callable $callable)
231
    {
232
        foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
233
            $callable($v, $k);
234
        }
235
236
        return $this;
237
    }
238
239
    /**
240
     * {@inheritdoc}
241
     */
242
    public function exists(callable $fn)
243
    {
244
        foreach ($this as $key => $element) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Collections\Traits\StrictKeyedIterableTrait> is not traversable.
Loading history...
245
            if ($fn($key, $element)) {
246
                return true;
247
            }
248
        }
249
250
        return false;
251
    }
252
}
253