Product::valid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\phpermutations\Iterators;
6
7
use ArrayIterator;
8
use drupol\phpermutations\Iterators;
9
use Iterator;
10
11
use function count;
12
13
class Product extends Iterators
14
{
15
    /**
16
     * The iterators.
17
     *
18
     * @var Iterator[]
19
     */
20
    protected $iterators = [];
21
22
    /**
23
     * Product constructor.
24
     *
25
     * @param array<int, mixed> $datasetArray
26
     *                            The array of dataset
27
     */
28 2
    public function __construct(array $datasetArray)
29
    {
30 2
        parent::__construct($datasetArray);
31
32 2
        foreach ($datasetArray as $dataset) {
33 2
            $this->iterators[] = new ArrayIterator($dataset);
34
        }
35
36 2
        $this->key = 0;
37 2
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function count(): int
43
    {
44 2
        $product = 1;
45
46 2
        foreach ($this->getDataset() as $dataset) {
47 2
            $product *= count($dataset);
48
        }
49
50 2
        return $product;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function current(): mixed
57
    {
58 2
        $tuple = [];
59
60 2
        foreach ($this->iterators as $iterator) {
61 2
            $tuple[] = $iterator->current();
62
        }
63
64 2
        return $tuple;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     *
70
     * @return void
71
     */
72 2
    public function next(): void
73
    {
74 2
        foreach (array_reverse($this->iterators) as $key => $iterator) {
75 2
            $iterator->next();
76
77 2
            if ($iterator->valid()) {
78 2
                $count_iterators = count($this->iterators);
79
80 2
                foreach ($this->iterators as $key2 => $iterator2) {
81 2
                    if ($count_iterators - $key2 <= $key) {
82 2
                        $iterator2->rewind();
83
                    }
84
                }
85
86 2
                break;
87
            }
88
        }
89
90 2
        ++$this->key;
91 2
    }
92
93
    /**
94
     * {@inheritdoc}
95
     *
96
     * @return void
97
     */
98 2
    public function rewind(): void
99
    {
100 2
        foreach ($this->iterators as $iterator) {
101 2
            $iterator->rewind();
102
        }
103
104 2
        $this->key = 0;
105 2
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @return bool
111
     */
112 2
    public function valid(): bool
113
    {
114 2
        $isUnlessOneValid = false;
115
116 2
        foreach ($this->iterators as $iterator) {
117 2
            if ($iterator->valid()) {
118 2
                $isUnlessOneValid = true;
119
            }
120
        }
121
122 2
        return $isUnlessOneValid;
123
    }
124
}
125