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

StrictIterableTrait::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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