Completed
Push — master ( 72d462...18bdd2 )
by Pol
06:00 queued 04:04
created

Product   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 6.62 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
cbo 1
dl 9
loc 136
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A current() 0 10 2
B next() 0 18 5
A key() 0 4 1
A valid() 0 12 3
A rewind() 0 8 2
A count() 0 10 2
A toArray() 9 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(array $datasetArray)
35
    {
36
        parent::__construct($datasetArray);
37
38
        foreach ($datasetArray as $dataset) {
39
            $this->iterators[] = new \ArrayIterator($dataset);
40
        }
41
42
        $this->key = 0;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function current()
49
    {
50
        $tuple = [];
51
52
        foreach ($this->iterators as $iterator) {
53
            $tuple[] = $iterator->current();
54
        }
55
56
        return $tuple;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    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
        foreach (array_reverse($this->iterators) as $key => $iterator) {
65
            $iterator->next();
66
            if ($iterator->valid()) {
67
                $count_iterators = count($this->iterators);
68
                foreach ($this->iterators as $key2 => $iterator2) {
69
                    if ($key >= $count_iterators - $key2) {
70
                        $iterator2->rewind();
71
                    }
72
                }
73
74
                break;
75
            }
76
        }
77
78
        $this->key++;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function key()
85
    {
86
        return $this->key;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function valid()
93
    {
94
        $isUnlessOneValid = false;
95
96
        foreach ($this->iterators as $iterator) {
97
            if ($iterator->valid()) {
98
                $isUnlessOneValid = true;
99
            }
100
        }
101
102
        return $isUnlessOneValid;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function rewind()
109
    {
110
        foreach ($this->iterators as $iterator) {
111
            $iterator->rewind();
112
        }
113
114
        $this->key = 0;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function count()
121
    {
122
        $product = 1;
123
124
        foreach ($this->getDataset() as $dataset) {
125
            $product *= count($dataset);
126
        }
127
128
        return $product;
129
    }
130
131
    /**
132
     * Convert the iterator into an array.
133
     *
134
     * @return array
135
     *   The elements.
136
     */
137 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
        $data = [];
140
141
        for ($this->rewind(); $this->valid(); $this->next()) {
142
            $data[] = $this->current();
143
        }
144
145
        return $data;
146
    }
147
}
148