Rotation::__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 1
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 Rotation extends Iterators
13
{
14
    /**
15
     * A copy of the original data.
16
     *
17
     * @var mixed[]
18
     */
19
    protected $rotation;
20
21
    /**
22
     * Rotation constructor.
23
     *
24
     * @param array<int, mixed> $dataset
25
     *                       The dataset
26
     */
27 5
    public function __construct(array $dataset = [])
28
    {
29 5
        parent::__construct($dataset, null);
30 5
        $this->rotation = $this->getDataset();
31 5
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 5
    public function count(): int
37
    {
38 5
        return count($this->getDataset());
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 5
    public function current(): mixed
45
    {
46 5
        return $this->rotation;
47
    }
48
49
    /**
50
     * Compute the next value of the Iterator.
51
     *
52
     * @param int|null $offset
53
     *                    The offset
54
     *
55
     * @return void
56
     */
57 5
    public function next($offset = 1): void
58
    {
59 5
        $offset = (null === $offset) ? 1 : $offset % $this->count();
60 5
        $this->rotation = array_merge(
61 5
            array_slice(
62 5
                $this->rotation,
63 5
                $offset
64
            ),
65 5
            array_slice(
66 5
                $this->rotation,
67 5
                0,
68 5
                $offset
69
            )
70
        );
71 5
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 5
    public function rewind(): void
77
    {
78 5
        $this->rotation = $this->getDataset();
79 5
    }
80
81
    /**
82
     * {@inheritdoc}
83
     *
84
     * @return bool
85
     */
86
    public function valid(): bool
87
    {
88
        return true;
89
    }
90
}
91