Completed
Push — master ( 15a3ff...ddeb31 )
by Pol
02:16
created

Product::current()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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