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

StrictIterableTrait::filter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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