Completed
Push — master ( 790b7c...67857d )
by Rudi
02:56
created

Sequence::pushAll()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
namespace Ds\Traits;
3
4
use Error;
5
use OutOfRangeException;
6
use Traversable;
7
use UnderflowException;
8
9
/**
10
 * Sequence
11
 *
12
 * @package Ds\Traits
13
 */
14
trait Sequence
15
{
16
    /**
17
     * @var array
18
     */
19
    private $internal = [];
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function __construct($values = null)
25
    {
26
        if (is_array($values) || $values instanceof Traversable) {
27
            $this->push(...$values);
28
        } elseif (is_integer($values)) {
29
            $this->allocate($values);
0 ignored issues
show
Bug introduced by
It seems like allocate() 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...
30
        }
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function toArray(): array
37
    {
38
        return $this->internal;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function merge($values): \Ds\Sequence
45
    {
46
        $merged = clone $this;
47
        $merged->push(...$values);
48
49
        return $merged;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function count(): int
56
    {
57
        return count($this->internal);
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63 View Code Duplication
    public function contains(...$values): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        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...
66
            return false;
67
        }
68
69
        foreach ($values as $value) {
70
            if ($this->find($value) === false) {
71
                return false;
72
            }
73
        }
74
75
        return true;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function filter(callable $callback = null): \Ds\Sequence
82
    {
83
        if ($callback) {
84
            return new self(array_filter($this->internal, $callback));
85
        }
86
87
        return new self(array_filter($this->internal));
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function find($value)
94
    {
95
        return array_search($value, $this->internal, true);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function first()
102
    {
103
        if (empty($this->internal)) {
104
            throw new UnderflowException();
105
        }
106
107
        return $this->internal[0];
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function get(int $index)
114
    {
115
        $this->checkRange($index);
116
117
        return $this->internal[$index];
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function insert(int $index, ...$values)
124
    {
125
        if ($index < 0 || $index > count($this->internal)) {
126
            throw new OutOfRangeException();
127
        }
128
129
        array_splice($this->internal, $index, 0, $values);
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function join(string $glue = null): string
136
    {
137
        return implode($glue, $this->internal);
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function last()
144
    {
145
        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...
146
            throw new UnderflowException();
147
        }
148
149
        return end($this->internal);
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function map(callable $callback): \Ds\Sequence
156
    {
157
        return new self(array_map($callback, $this->internal));
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163 View Code Duplication
    public function pop()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        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...
166
            throw new UnderflowException();
167
        }
168
169
        $value = array_pop($this->internal);
170
        $this->adjustCapacity();
0 ignored issues
show
Bug introduced by
It seems like adjustCapacity() 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...
171
172
        return $value;
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    public function push(...$values)
179
    {
180
        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...
181
            array_push($this->internal, ...$values);
182
            $this->adjustCapacity();
0 ignored issues
show
Bug introduced by
It seems like adjustCapacity() 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...
183
        }
184
    }
185
186
    /**
187
     * @inheritDoc
188
     */
189
    public function reduce(callable $callback, $initial = null)
190
    {
191
        return array_reduce($this->internal, $callback, $initial);
192
    }
193
194
    /**
195
     * @inheritDoc
196
     */
197
    public function remove(int $index)
198
    {
199
        $this->checkRange($index);
200
201
        $value = array_splice($this->internal, $index, 1, null)[0];
202
        $this->adjustCapacity();
0 ignored issues
show
Bug introduced by
It seems like adjustCapacity() 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...
203
204
        return $value;
205
    }
206
207
    /**
208
     * @inheritDoc
209
     */
210
    public function reverse(): \Ds\Sequence
211
    {
212
        return new self(array_reverse($this->internal));
213
214
    }
215
216
    private function reverseRange(int $a, int $b)
217
    {
218
        $swap = function(&$a, &$b) {
219
            $t = $a;
220
            $a = $b;
221
            $b = $t;
222
        };
223
224
        while (--$b > $a) {
225
            $swap($this->internal[$a++], $this->internal[$b--]);
226
        }
227
    }
228
229
    private function normalizeRotations(int $rotations, int $count)
230
    {
231
        if ($rotations < 0) {
232
            return $count - (abs($rotations) % $count);
233
        }
234
235
        return $rotations % $count;
236
    }
237
238
    /**
239
     * @inheritDoc
240
     */
241
    public function rotate(int $rotations)
242
    {
243
        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...
244
            return;
245
        }
246
247
        $n = count($this);
248
        $r = $this->normalizeRotations($rotations, $n);
249
250
        if ($r > 0) {
251
            $this->reverseRange(0,  $r);
252
            $this->reverseRange($r, $n);
253
            $this->reverseRange(0,  $n);
254
        }
255
    }
256
257
    /**
258
     * @inheritDoc
259
     */
260
    public function set(int $index, $value)
261
    {
262
        $this->checkRange($index);
263
        $this->internal[$index] = $value;
264
    }
265
266
    /**
267
     * @inheritDoc
268
     */
269 View Code Duplication
    public function shift()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271
        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...
272
            throw new UnderflowException();
273
        }
274
275
        $value = array_shift($this->internal);
276
        $this->adjustCapacity();
0 ignored issues
show
Bug introduced by
It seems like adjustCapacity() 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...
277
278
        return $value;
279
    }
280
281
    /**
282
     * @inheritDoc
283
     */
284
    public function slice(int $offset, int $length = null): \Ds\Sequence
285
    {
286
        if (func_num_args() === 1) {
287
            return new self(array_slice($this->internal, $offset));
288
        }
289
290
        return new self(array_slice($this->internal, $offset, $length));
291
    }
292
293
    /**
294
     * @inheritDoc
295
     */
296
    public function sort(callable $comparator = null): \Ds\Sequence
297
    {
298
        $internal = $this->internal;
299
300
        if ($comparator) {
301
            usort($internal, $comparator);
302
        } else {
303
            sort($internal);
304
        }
305
306
        return new self($internal);
307
    }
308
309
    /**
310
     * @inheritDoc
311
     */
312
    public function unshift(...$values)
313
    {
314
        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...
315
            array_unshift($this->internal, ...$values);
316
            $this->adjustCapacity();
0 ignored issues
show
Bug introduced by
It seems like adjustCapacity() 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
        }
318
    }
319
320
    /**
321
     * Check Range
322
     *
323
     * @param int $index
324
     */
325
    private function checkRange(int $index)
326
    {
327
        if ($index < 0 || $index >= count($this->internal)) {
328
            throw new OutOfRangeException();
329
        }
330
    }
331
332
    /**
333
     * Get Iterator
334
     */
335
    public function getIterator()
336
    {
337
        foreach ($this->internal as $value) {
338
            yield $value;
339
        }
340
    }
341
342
    /**
343
     *
344
     */
345
    public function clear()
346
    {
347
        $this->internal = [];
348
        $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...
349
    }
350
351
    /**
352
     * @inheritdoc
353
     */
354
    public function offsetSet($offset, $value)
355
    {
356
        if ($offset === null) {
357
            $this->push($value);
358
        } else {
359
            $this->set($offset, $value);
360
        }
361
    }
362
363
    /**
364
     * @inheritdoc
365
     */
366
    public function &offsetGet($offset)
367
    {
368
        $this->checkRange($offset);
369
        return $this->internal[$offset];
370
    }
371
372
    /**
373
     * @inheritdoc
374
     */
375
    public function offsetUnset($offset)
376
    {
377
        // Unset should be quiet, so we shouldn't allow 'remove' to throw.
378
        if (is_integer($offset) && $offset >= 0 && $offset < count($this)) {
379
            $this->remove($offset);
380
        }
381
    }
382
383
    /**
384
     * @inheritdoc
385
     */
386
    public function offsetExists($offset)
387
    {
388
        if ($offset < 0 || $offset >= count($this)) {
389
            return false;
390
        }
391
392
        return $this->get($offset) !== null;
393
    }
394
}
395