Shift::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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