Completed
Push — master ( 3d4931...61b9c3 )
by Pol
02:18
created

Combinations::next()   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;
4
5
/**
6
 * Class Combinations.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Combinations extends Combinatorics implements \Iterator {
11
12
  /**
13
   * @var array
14
   */
15
  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...
16
17
  /**
18
   * @var int
19
   */
20
  protected $count = 0;
21
22
  /**
23
   * @var int
24
   */
25
  protected $pos = 0;
26
27
  /**
28
   * Combinations constructor.
29
   *
30
   * @param array $dataset
31
   * @param null $length
32
   */
33 6
  public function __construct(array $dataset = array(), $length = NULL) {
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
34 6
    parent::__construct($dataset, $length);
35 6
    $this->rewind();
36 6
  }
37
38
  /**
39
   * @return int
40
   */
41
  public function key() {
42
    return $this->pos;
43
  }
44
45
  /**
46
   * @return array
47
   */
48 6
  public function current() {
49 6
    $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...
50 6
    for($i = 0; $i < $this->length; $i++) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
51 6
      $r[] = $this->dataset[$this->c[$i]];
52 6
    }
53 6
    return $r;
54
  }
55
56
  /**
57
   *
58
   */
59 6
  public function next() {
60 6
    if ($this->_next()) {
61 4
      $this->pos++;
62 4
    }
63
    else {
64 6
      $this->pos = -1;
65
    }
66 6
  }
67
68
  /**
69
   *
70
   */
71 6
  public function rewind() {
72 6
    $this->c = range(0, $this->length);
73 6
    $this->pos = 0;
74 6
  }
75
76
  /**
77
   * @return bool
78
   */
79 6
  public function valid() {
80 6
    return $this->pos >= 0;
81
  }
82
83
  /**
84
   * @return bool
85
   */
86 6
  protected function _next() {
0 ignored issues
show
Coding Style Naming introduced by
The method _next is not named in camelCase.

This check marks method 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...
87 6
    $i = $this->length - 1;
88 6
    while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) {
89 6
      $i--;
90 6
    }
91 6
    if($i < 0) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
92 6
      return FALSE;
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
93
    }
94 4
    $this->c[$i]++;
95 4
    while($i++ < $this->length - 1) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
96 4
      $this->c[$i] = $this->c[$i - 1] + 1;
97 4
    }
98
99 4
    return TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
100
  }
101
102
  /**
103
   * @return array
104
   */
105 3
  public function toArray() {
106 3
    $data = [];
107 3
    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...
108 3
      $data[] = $this->current();
109 3
    }
110 3
    return $data;
111
  }
112
113
  /**
114
   * @return int
115
   */
116 3
  public function count() {
117 3
    return $this->fact(count($this->getDataset()))/$this->fact($this->getLength());
118
  }
119
120
}
121