Passed
Push — master ( a2fa16...8a4497 )
by Pol
11:27
created

Shift::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\phpermutations\Iterators;
6
7
use drupol\phpermutations\Iterators;
8
9
use function array_slice;
10
use function count;
11
12
/**
13
 * Class Shift.
14
 */
15
class Shift extends Iterators
16
{
17
    /**
18
     * Shift constructor.
19
     *
20
     * @param array<int, mixed> $dataset
21
     *                       The dataset
22
     * @param int   $length
23
     *                       The shift length
24
     */
25
    public function __construct(array $dataset = [], $length = 1)
26
    {
27
        parent::__construct($dataset, $length);
28
        $this->current = $this->getDataset();
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function count()
35 12
    {
36
        return count($this->getDataset());
37 12
    }
38 12
39 12
    /**
40
     * {@inheritdoc}
41
     */
42
    public function next()
43
    {
44 12
        $this->doShift(1);
45
    }
46 12
47 12
    /**
48
     * {@inheritdoc}
49 12
     */
50
    public function rewind()
51
    {
52
        $this->doShift(-1);
53
    }
54
55 4
    /**
56
     * {@inheritdoc}
57 4
     */
58
    public function valid()
59
    {
60
        return true;
61
    }
62
63 8
    /**
64
     * Internal function to do the shift.
65 8
     *
66 8
     * @param int $length
67
     */
68
    protected function doShift($length = 1)
69
    {
70
        $parameters = [];
71
72
        if (0 > $length) {
73
            $parameters[] = ['start' => abs($length), 'end' => null];
74
            $parameters[] = ['start' => 0, 'end' => abs($length)];
75
        } else {
76
            $parameters[] = ['start' => -1 * $length, 'end' => null];
77
            $parameters[] = ['start' => 0, 'end' => $this->datasetCount + $length * -1];
78
        }
79
80
        $this->current = array_merge(
81
            array_slice($this->current, $parameters[0]['start'], $parameters[0]['end']),
82
            array_slice($this->current, $parameters[1]['start'], $parameters[1]['end'])
83
        );
84
    }
85
}
86