Completed
Push — master ( 7482d8...b6686e )
by Rudi
02:24
created

Sequence::rotate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
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->pushAll($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 = $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...
47
        $merged->pushAll($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 pushAll($values)
190
    {
191
        if ( ! is_array($values) && ! $values instanceof Traversable) {
192
            throw new Error();
193
        }
194
195
        $this->push(...$values);
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function reduce(callable $callback, $initial = null)
202
    {
203
        return array_reduce($this->internal, $callback, $initial);
204
    }
205
206
    /**
207
     * @inheritDoc
208
     */
209
    public function remove(int $index)
210
    {
211
        $this->checkRange($index);
212
213
        $value = array_splice($this->internal, $index, 1, null)[0];
214
        $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...
215
216
        return $value;
217
    }
218
219
    /**
220
     * @inheritDoc
221
     */
222
    public function reverse(): \Ds\Sequence
223
    {
224
        return new self(array_reverse($this->internal));
225
226
    }
227
228
    private function reverseRange(int $a, int $b)
229
    {
230
        $swap = function(&$a, &$b) {
231
            $t = $a;
232
            $a = $b;
233
            $b = $t;
234
        };
235
236
        while (--$b > $a) {
237
            $swap($this->internal[$a++], $this->internal[$b--]);
238
        }
239
    }
240
241
    private function normalizeRotations(int $rotations, int $count)
242
    {
243
        if ($rotations < 0) {
244
            return $count - (abs($rotations) % $count);
245
        }
246
247
        return $rotations % $count;
248
    }
249
250
    /**
251
     * @inheritDoc
252
     */
253
    public function rotate(int $rotations)
254
    {
255
        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...
256
            return;
257
        }
258
259
        $n = count($this);
260
        $r = $this->normalizeRotations($rotations, $n);
261
262
        if ($r > 0) {
263
            $this->reverseRange(0,  $r);
264
            $this->reverseRange($r, $n);
265
            $this->reverseRange(0,  $n);
266
        }
267
    }
268
269
    /**
270
     * @inheritDoc
271
     */
272
    public function set(int $index, $value)
273
    {
274
        $this->checkRange($index);
275
        $this->internal[$index] = $value;
276
    }
277
278
    /**
279
     * @inheritDoc
280
     */
281 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...
282
    {
283
        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...
284
            throw new UnderflowException();
285
        }
286
287
        $value = array_shift($this->internal);
288
        $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...
289
290
        return $value;
291
    }
292
293
    /**
294
     * @inheritDoc
295
     */
296
    public function slice(int $offset, int $length = null): \Ds\Sequence
297
    {
298
        if (func_num_args() === 1) {
299
            return new self(array_slice($this->internal, $offset));
300
        }
301
302
        return new self(array_slice($this->internal, $offset, $length));
303
    }
304
305
    /**
306
     * @inheritDoc
307
     */
308
    public function sort(callable $comparator = null): \Ds\Sequence
309
    {
310
        $internal = $this->internal;
311
312
        if ($comparator) {
313
            usort($internal, $comparator);
314
        } else {
315
            sort($internal);
316
        }
317
318
        return new self($internal);
319
    }
320
321
    /**
322
     * @inheritDoc
323
     */
324
    public function unshift(...$values)
325
    {
326
        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...
327
            array_unshift($this->internal, ...$values);
328
            $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...
329
        }
330
    }
331
332
    /**
333
     * Check Range
334
     *
335
     * @param int $index
336
     */
337
    private function checkRange(int $index)
338
    {
339
        if ($index < 0 || $index >= count($this->internal)) {
340
            throw new OutOfRangeException();
341
        }
342
    }
343
344
    /**
345
     * Get Iterator
346
     */
347
    public function getIterator()
348
    {
349
        foreach ($this->internal as $value) {
350
            yield $value;
351
        }
352
    }
353
354
    /**
355
     *
356
     */
357
    public function clear()
358
    {
359
        $this->internal = [];
360
        $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...
361
    }
362
363
    /**
364
     * @inheritdoc
365
     */
366
    public function offsetSet($offset, $value)
367
    {
368
        if ($offset === null) {
369
            $this->push($value);
370
        } else {
371
            $this->set($offset, $value);
372
        }
373
    }
374
375
    /**
376
     * @inheritdoc
377
     */
378
    public function &offsetGet($offset)
379
    {
380
        $this->checkRange($offset);
381
        return $this->internal[$offset];
382
    }
383
384
    /**
385
     * @inheritdoc
386
     */
387
    public function offsetUnset($offset)
388
    {
389
        // Unset should be quiet, so we shouldn't allow 'remove' to throw.
390
        if (is_integer($offset) && $offset >= 0 && $offset < count($this)) {
391
            $this->remove($offset);
392
        }
393
    }
394
395
    /**
396
     * @inheritdoc
397
     */
398
    public function offsetExists($offset)
399
    {
400
        if ($offset < 0 || $offset >= count($this)) {
401
            return false;
402
        }
403
404
        return $this->get($offset) !== null;
405
    }
406
}
407