Passed
Push — master ( 84546e...250220 )
by Pol
06:17
created

Combinations::current()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Combinations.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Combinations extends Combinatorics implements \Iterator
13
{
14
15
  /**
16
   * The values.
17
   *
18
   * @var array
19
   */
20
    protected $c = array();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
21
22
  /**
23
   * The key.
24
   *
25
   * @var int
26
   */
27
    protected $key = 0;
28
29
  /**
30
   * Combinations constructor.
31
   *
32
   * @param array $dataset
33
   *   The dataset.
34
   * @param int|null $length
35
   *   The length.
36
   */
37 10
    public function __construct(array $dataset = array(), $length = null)
38
    {
39 10
        parent::__construct(array_values($dataset), $length);
40 10
        $this->rewind();
41 10
    }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
    public function key()
47
    {
48
        return $this->key;
49
    }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54 4
    public function current()
55
    {
56 4
        $r = array();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
57 4
        for ($i = 0; $i < $this->length; $i++) {
58 4
            $r[] = $this->dataset[$this->c[$i]];
59 4
        }
60 4
        return $r;
61
    }
62
63
  /**
64
   * {@inheritdoc}
65
   */
66 4
    public function next()
67
    {
68 4
        if ($this->nextHelper()) {
69 3
            $this->key++;
70 3
        } else {
71 4
            $this->key = -1;
72
        }
73 4
    }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78 10
    public function rewind()
79
    {
80 10
        $this->c = range(0, $this->length);
81 10
        $this->key = 0;
82 10
    }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87 4
    public function valid()
88
    {
89 4
        return $this->key >= 0;
90
    }
91
92
  /**
93
   * Custom next() callback.
94
   *
95
   * @return bool
96
   *   Return true or false.
97
   */
98 4
    protected function nextHelper()
99
    {
100 4
        $i = $this->length - 1;
101 4
        while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) {
102 4
            $i--;
103 4
        }
104 4
        if ($i < 0) {
105 4
            return false;
106
        }
107 3
        $this->c[$i]++;
108 3
        while ($i++ < $this->length - 1) {
109 3
            $this->c[$i] = $this->c[$i - 1] + 1;
110 3
        }
111
112 3
        return true;
113
    }
114
115
  /**
116
   * Convert the iterator into an array.
117
   *
118
   * @return array
119
   *   The elements.
120
   */
121 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...
122
    {
123 4
        $data = [];
124
125 4
        for ($this->rewind(); $this->valid(); $this->next()) {
126 4
            $data[] = $this->current();
127 4
        }
128
129 4
        return $data;
130
    }
131
132
  /**
133
   * {@inheritdoc}
134
   */
135 10
    public function count()
136
    {
137 10
        return $this->fact(count($this->getDataset())) /
138 10
          ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
139
    }
140
}
141