Completed
Pull Request — master (#43)
by
unknown
08:27
created

GenericSequence::filter()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 1
nop 1
crap 6
1
<?php
2
namespace Ds\Traits;
3
4
use Ds\Sequence;
5
use OutOfRangeException;
6
use UnderflowException;
7
8
/**
9
 * Common functionality of all structures that implement 'Sequence'. Because the
10
 * polyfill's only goal is to achieve consistent behaviour, all sequences will
11
 * share the same implementation using an array array.
12
 *
13
 * @package Ds\Traits
14
 */
15
trait GenericSequence
16
{
17
    /**
18
     * @var array internal array used to store the values of the sequence.
19
     */
20
    private $array = [];
21
22
    /**
23
     * @inheritDoc
24
     */
25
    public function __construct($values = null)
26
    {
27
        if ($values) {
28
            $this->pushAll($values);
29
        }
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function toArray(): array
36
    {
37
        return $this->array;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function apply(callable $callback)
44
    {
45
        foreach ($this->array as &$value) {
46
            $value = $callback($value);
47
        }
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function merge($values): Sequence
54
    {
55
        $copy = $this->copy();
0 ignored issues
show
Bug introduced by
It seems like copy() 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...
56
        $copy->pushAll($values);
57
        return $copy;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function count(): int
64
    {
65
        return count($this->array);
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function contains(...$values): bool
72
    {
73
        foreach ($values as $value) {
74
            if ($this->find($value) === false) {
75
                return false;
76
            }
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function filter(callable $callback = null): Sequence
86
    {
87
        return new self(array_filter($this->array, $callback ?: 'boolval'));
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function find($value)
94
    {
95
        return array_search($value, $this->array, true);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function first()
102
    {
103
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
104
            throw new UnderflowException();
105
        }
106
107
        return $this->array[0];
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function get(int $index)
114
    {
115
        if ( ! $this->validIndex($index)) {
116
            throw new OutOfRangeException();
117
        }
118
119
        return $this->array[$index];
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function insert(int $index, ...$values)
126
    {
127
        if ( ! $this->validIndex($index) && $index !== count($this)) {
128
            throw new OutOfRangeException();
129
        }
130
131
        array_splice($this->array, $index, 0, $values);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function join(string $glue = null): string
138
    {
139
        return implode($glue, $this->array);
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function last()
146
    {
147
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
148
            throw new UnderflowException();
149
        }
150
151
        return $this->array[count($this) - 1];
152
    }
153
154
    /**
155
     * @inheritDoc
156
     */
157
    public function map(callable $callback): Sequence
158
    {
159
        return new self(array_map($callback, $this->array));
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function pop()
166
    {
167
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
168
            throw new UnderflowException();
169
        }
170
171
        $value = array_pop($this->array);
172
        $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() 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...
173
174
        return $value;
175
    }
176
177
    /**
178
     * Pushes all values of either an array or traversable object.
179
     */
180
    private function pushAll($values)
181
    {
182
        foreach ($values as $value) {
183
            $this->array[] = $value;
184
        }
185
186
        $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() 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...
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192
    public function push(...$values)
193
    {
194
        $this->pushAll($values);
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200
    public function reduce(callable $callback, $initial = null)
201
    {
202
        return array_reduce($this->array, $callback, $initial);
203
    }
204
205
    /**
206
     * @inheritDoc
207
     */
208
    public function remove(int $index)
209
    {
210
        if ( ! $this->validIndex($index)) {
211
            throw new OutOfRangeException();
212
        }
213
214
        $value = array_splice($this->array, $index, 1, null)[0];
215
        $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() 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...
216
217
        return $value;
218
    }
219
220
    /**
221
     * @inheritDoc
222
     */
223
    public function reverse()
224
    {
225
        $this->array = array_reverse($this->array);
226
    }
227
228
    /**
229
     * @inheritDoc
230
     */
231
    public function reversed(): Sequence
232
    {
233
        return new self(array_reverse($this->array));
234
    }
235
236
    /**
237
     * Converts negative or large rotations into the minimum positive number
238
     * of rotations required to rotate the sequence by a given $r.
239
     */
240
    private function normalizeRotations(int $r)
241
    {
242
        $n = count($this);
243
244
        if ($n < 2) return 0;
245
        if ($r < 0) return $n - (abs($r) % $n);
246
247
        return $r % $n;
248
    }
249
250
    /**
251
     * @inheritDoc
252
     */
253
    public function rotate(int $rotations)
254
    {
255
        for ($r = $this->normalizeRotations($rotations); $r > 0; $r--) {
256
            array_push($this->array, array_shift($this->array));
257
        }
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    public function set(int $index, $value)
264
    {
265
        if ( ! $this->validIndex($index)) {
266
            throw new OutOfRangeException();
267
        }
268
269
        $this->array[$index] = $value;
270
    }
271
272
    /**
273
     * @inheritDoc
274
     */
275
    public function shift()
276
    {
277
        if ($this->isEmpty()) {
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
278
            throw new UnderflowException();
279
        }
280
281
        $value = array_shift($this->array);
282
        $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() 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...
283
284
        return $value;
285
    }
286
287
    /**
288
     * @inheritDoc
289
     */
290
    public function slice(int $offset, int $length = null): Sequence
291
    {
292
        if (func_num_args() === 1) {
293
            $length = count($this);
294
        }
295
296
        return new self(array_slice($this->array, $offset, $length));
297
    }
298
299
    /**
300
     * @inheritDoc
301
     */
302
    public function sort(callable $comparator = null)
303
    {
304
        if ($comparator) {
305
            usort($this->array, $comparator);
306
        } else {
307
            sort($this->array);
308
        }
309
    }
310
311
    /**
312
     * @inheritDoc
313
     */
314
    public function sorted(callable $comparator = null): Sequence
315
    {
316
        $copy = $this->copy();
0 ignored issues
show
Bug introduced by
It seems like copy() 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...
317
        $copy->sort($comparator);
318
        return $copy;
319
    }
320
321
    /**
322
     * @inheritDoc
323
     */
324
    public function sum()
325
    {
326
        return array_sum($this->array);
327
    }
328
329
    /**
330
     * @inheritDoc
331
     */
332
    public function unshift(...$values)
333
    {
334
        if ($values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
335
            $this->array = array_merge($values, $this->array);
336
            $this->checkCapacity();
0 ignored issues
show
Bug introduced by
It seems like checkCapacity() 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...
337
        }
338
    }
339
340
    /**
341
     *
342
     */
343
    private function validIndex(int $index)
344
    {
345
        return $index >= 0 && $index < count($this);
346
    }
347
348
    /**
349
     *
350
     */
351
    public function getIterator()
352
    {
353
        foreach ($this->array as $value) {
354
            yield $value;
355
        }
356
    }
357
358
    /**
359
     * @inheritdoc
360
     */
361
    public function clear()
362
    {
363
        $this->array = [];
364
        $this->capacity = self::MIN_CAPACITY;
0 ignored issues
show
Bug introduced by
The property capacity 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...
365
    }
366
367
    /**
368
     * @inheritdoc
369
     */
370
    public function offsetSet($offset, $value)
371
    {
372
        if ($offset === null) {
373
            $this->push($value);
374
        } else {
375
            $this->set($offset, $value);
376
        }
377
    }
378
379
    /**
380
     * @inheritdoc
381
     */
382
    public function &offsetGet($offset)
383
    {
384
        if ( ! $this->validIndex($offset)) {
385
            throw new OutOfRangeException();
386
        }
387
388
        return $this->array[$offset];
389
    }
390
391
    /**
392
     * @inheritdoc
393
     */
394
    public function offsetUnset($offset)
395
    {
396
        if (is_integer($offset) && $this->validIndex($offset)) {
397
            $this->remove($offset);
398
        }
399
    }
400
401
    /**
402
     * @inheritdoc
403
     */
404
    public function offsetExists($offset)
405
    {
406
        return is_integer($offset)
407
            && $this->validIndex($offset)
408
            && $this->get($offset) !== null;
409
    }
410
}
411