Completed
Push — master ( 4ae196...831fe8 )
by Pol
02:12
created

Product   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 123
Duplicated Lines 7.32 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 19
cbo 1
dl 9
loc 123
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A __construct() 0 9 2
A current() 0 9 2
B next() 0 18 5
A valid() 0 11 3
A rewind() 0 7 2
A count() 0 9 2
A toArray() 9 9 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 {
13
14
  /**
15
   * @var int
16
   */
17
  protected $key;
18
19
  /**
20
   * @var \Iterator[]
21
   */
22
  protected $iterators = array();
23
24
  /**
25
   * Product constructor.
26
   *
27
   * @param array $datasetArray
28
   * @param int|null $length
29
   */
30 4
  public function __construct(array $datasetArray, $length = NULL) {
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
31 4
    parent::__construct($datasetArray);
32
33 4
    foreach ($datasetArray as $dataset) {
34 4
      $this->iterators[] = new \ArrayIterator($dataset);
35
    }
36
37 4
    $this->key = 0;
38 4
  }
39
40
  /**
41
   * @inheritdoc
42
   */
43 4
  public function current() {
44 4
    $tuple = array();
45
46 4
    foreach ($this->iterators as $iterator) {
47 4
      $tuple[] = $iterator->current();
48
    }
49
50 4
    return $tuple;
51
  }
52
53
  /**
54
   * @inheritdoc
55
   */
56 4
  public function next() {
57
    /** @var $reverseIterators \Iterator[] */
58 4
    $reverseIterators = array_reverse($this->iterators);
59
60 4
    foreach ($reverseIterators as $key => $iterator) {
61 4
      $iterator->next();
62 4
      if ($iterator->valid()) {
63 4
        foreach ($this->iterators as $key2 => $iterator2) {
64 4
          if ($key >= count($this->iterators) - $key2) {
65 4
            $iterator2->rewind();
66
          }
67
        }
68 4
        break;
69
      }
70
    }
71
72 4
    $this->key++;
73 4
  }
74
75
  /**
76
   * @inheritdoc
77
   */
78
  public function key() {
79
    return $this->key;
80
  }
81
82
  /**
83
   * @inheritdoc
84
   */
85 4
  public function valid() {
86 4
    $isUnlessOneValid = FALSE;
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
87
88 4
    foreach ($this->iterators as $iterator) {
89 4
      if ($iterator->valid()) {
90 4
        $isUnlessOneValid = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
91
      }
92
    }
93
94 4
    return $isUnlessOneValid;
95
  }
96
97
  /**
98
   * @inheritdoc
99
   */
100 4
  public function rewind() {
101 4
    foreach ($this->iterators as $iterator) {
102 4
      $iterator->rewind();
103
    }
104
105 4
    $this->key = 0;
106 4
  }
107
108
  /**
109
   * @return int
110
   */
111 4
  public function count() {
112 4
    $product = 1;
113
114 4
    foreach ($this->getDataset() as $dataset) {
115 4
      $product *= count($dataset);
116
    }
117
118 4
    return $product;
119
  }
120
121
  /**
122
   * @return array
123
   */
124 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...
125 4
    $data = array();
126
127 4
    for ($this->rewind(); $this->valid(); $this->next()) {
128 4
      $data[] = $this->current();
129
    }
130
131 4
    return $data;
132
  }
133
134
}
135