Completed
Push — master ( 0e2954...632183 )
by Ítalo
04:23
created

StrictKeyedIterableTrait::last()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Collections\Traits;
4
5
use Collections\ArrayList;
6
use Collections\Dictionary;
7
use Collections\Iterable;
8
use Collections\MapInterface;
9
10
trait StrictKeyedIterableTrait
11
{
12
    use CommonMutableContainerTrait;
13
14
    public function keys()
15
    {
16
        $res = new ArrayList();
17
        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...
18
            $res[] = $k;
19
        }
20
21
        return $res;
22
    }
23
24
    public function map(callable $callback)
25
    {
26
        $res = new Dictionary();
27
        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...
28
            $res[$k] = $callback($v);
29
        }
30
31
        return $res;
32
    }
33
34
    public function mapWithKey($callback)
35
    {
36
        $res = new Dictionary();
37
        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...
38
            $res[$k] = $callback($k, $v);
39
        }
40
41
        return $res;
42
    }
43
44
    public function filter(callable $callback)
45
    {
46
        $res = new Dictionary();
47
        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...
48
            if ($callback($v)) {
49
                $res[$k] = $v;
50
            }
51
        }
52
53
        return $res;
54
    }
55
56
    public function filterWithKey($callback)
57
    {
58
        $res = new Dictionary();
59
        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...
60
            if ($callback($k, $v)) {
61
                $res[$k] = $v;
62
            }
63
        }
64
65
        return $res;
66
    }
67
68
    public function zip($iterable)
69
    {
70
        $res = new Dictionary();
71
        $it = $iterable->getIterator();
72
        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...
73
            if (!$it->valid()) {
74
                break;
75
            }
76
            $res[$k] = new Pair($v, $it->current());
77
            $it->next();
78
        }
79
80
        return $res;
81
    }
82
83
    public function take($size = 1)
84
    {
85
        $res = new Dictionary();
86
        if ($size <= 0) {
87
            return $res;
88
        }
89
        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...
90
            $res[$k] = $v;
91
            if (--$size === 0) {
92
                break;
93
            }
94
        }
95
96
        return $res;
97
    }
98
99
    public function takeWhile($fn)
100
    {
101
        $res = new Dictionary();
102
        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...
103
            if (!$fn($v)) {
104
                break;
105
            }
106
            $res[$k] = $v;
107
        }
108
109
        return $res;
110
    }
111
112
    public function skip($n)
113
    {
114
        $res = new Dictionary();
115
        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...
116
            if ($n <= 0) {
117
                $res[$k] = $v;
118
            } else {
119
                --$n;
120
            }
121
        }
122
123
        return $res;
124
    }
125
126
    public function skipWhile($fn)
127
    {
128
        $res = new Dictionary();
129
        $skip = true;
130
        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...
131
            if ($skip) {
132
                if ($fn($v)) {
133
                    continue;
134
                }
135
                $skip = false;
136
            }
137
            $res[$k] = $v;
138
        }
139
140
        return $res;
141
    }
142
143
    public function slice($start, $lenght)
144
    {
145
        $res = new Dictionary();
146
        if ($lenght <= 0) {
147
            return $res;
148
        }
149
        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...
150
            if ($start !== 0) {
151
                --$start;
152
                continue;
153
            }
154
            $res[$k] = $v;
155
            if (--$lenght === 0) {
156
                break;
157
            }
158
        }
159
160
        return $res;
161
    }
162
163 1
    public function concat(\Traversable $iterable)
164
    {
165 1
        $res = [];
166
167 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...
168 1
            $res[$k] = $v;
169 1
        }
170
171 1
        foreach ($iterable as $k => $v) {
172 1
            $res[$k] = $v;
173 1
        }
174 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...
175
176 1
        return $this;
177
    }
178
179
    public function first()
180
    {
181
        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...
182
            return $v;
183
        }
184
185
        return null;
186
    }
187
188
    public function firstKey()
189
    {
190
        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...
191
            return $k;
192
        }
193
194
        return null;
195
    }
196
197
    public function last()
198
    {
199
        $v = null;
200
        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...
201
        }
202
203
        return $v;
204
    }
205
206
    public function lastKey()
207
    {
208
        $k = null;
209
        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...
210
        }
211
212
        return $k;
213
    }
214
215
    /**
216
     * {@inheritDoc}
217
     * @return $this
218
     */
219
    public function each(callable $callable)
220
    {
221
        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...
222
            $callable($v, $k);
223
        }
224
225
        return $this;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function exists(callable $fn)
232
    {
233
        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...
234
            if ($fn($key, $element)) {
235
                return true;
236
            }
237
        }
238
239
        return false;
240
    }
241
242
    public function concatAll()
243
    {
244
        /** @var MapInterface $results */
245
        $results = new static();
246
        $this->each(function (Iterable $subArray) use ($results) {
247
            $subArray->each(function ($item, $key) use ($results) {
248
                $results->add($key, $item);
249
            });
250
        });
251
252
        return $results;
253
    }
254
}
255