Passed
Push — master ( 18bdd2...b79ef4 )
by Pol
04:33
created

Product::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Product.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Product extends Combinatorics implements \Iterator, \Countable
13
{
14
    /**
15
     * The key.
16
     *
17
     * @var int
18
     */
19
    protected $key;
20
21
    /**
22
     * The iterators.
23
     *
24
     * @var \Iterator[]
25
     */
26
    protected $iterators = [];
27
28
    /**
29
     * Product constructor.
30
     *
31
     * @param array $datasetArray
32
     *   The array of dataset.
33
     */
34 4
    public function __construct(array $datasetArray)
35
    {
36 4
        parent::__construct($datasetArray);
37
38 4
        foreach ($datasetArray as $dataset) {
39 4
            $this->iterators[] = new \ArrayIterator($dataset);
40 4
        }
41
42 4
        $this->key = 0;
43 4
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 4
    public function current()
49
    {
50 4
        $tuple = [];
51
52 4
        foreach ($this->iterators as $iterator) {
53 4
            $tuple[] = $iterator->current();
54 4
        }
55
56 4
        return $tuple;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function next()
0 ignored issues
show
Coding Style Naming introduced by
The variable $count_iterators is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
63
    {
64 4
        foreach (array_reverse($this->iterators) as $key => $iterator) {
65 4
            $iterator->next();
66 4
            if ($iterator->valid()) {
67 4
                $count_iterators = count($this->iterators);
68 4
                foreach ($this->iterators as $key2 => $iterator2) {
69 4
                    if ($key >= $count_iterators - $key2) {
70 4
                        $iterator2->rewind();
71 4
                    }
72 4
                }
73
74 4
                break;
75
            }
76 4
        }
77
78 4
        $this->key++;
79 4
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function key()
85
    {
86
        return $this->key;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 4
    public function valid()
93
    {
94 4
        $isUnlessOneValid = false;
95
96 4
        foreach ($this->iterators as $iterator) {
97 4
            if ($iterator->valid()) {
98 4
                $isUnlessOneValid = true;
99 4
            }
100 4
        }
101
102 4
        return $isUnlessOneValid;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 4
    public function rewind()
109
    {
110 4
        foreach ($this->iterators as $iterator) {
111 4
            $iterator->rewind();
112 4
        }
113
114 4
        $this->key = 0;
115 4
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 4
    public function count()
121
    {
122 4
        $product = 1;
123
124 4
        foreach ($this->getDataset() as $dataset) {
125 4
            $product *= count($dataset);
126 4
        }
127
128 4
        return $product;
129
    }
130
131
    /**
132
     * Convert the iterator into an array.
133
     *
134
     * @return array
135
     *   The elements.
136
     */
137 4 View Code Duplication
    public function toArray()
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...
138
    {
139 4
        $data = [];
140
141 4
        for ($this->rewind(); $this->valid(); $this->next()) {
142 4
            $data[] = $this->current();
143 4
        }
144
145 4
        return $data;
146
    }
147
}
148