Passed
Push — master ( 1e7555...a49478 )
by Pol
03:09
created

Product::next()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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