Completed
Push — master ( a49478...ff8a6e )
by Pol
01:00 queued 10s
created

Product::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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