Completed
Push — master ( b5dbe1...4c076d )
by Pol
02:14
created

Combinations::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\phpermutations;
4
5
/**
6
 * Class Combinations.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Combinations implements \Iterator, \Countable {
11
  protected $c = NULL;
1 ignored issue
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...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
12
  protected $dataset = NULL;
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
13
  protected $count = 0;
14
  protected $length = 0;
15
  protected $pos = 0;
16
17
  /**
18
   * Combinations constructor.
19
   *
20
   * @param array $dataset
21
   * @param null $length
22
   */
23 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...
24 6
    $this->dataset = array_values($dataset);
25 6
    $this->count = count($this->dataset);
26 6
    $this->length = ($length > $this->count) ? $this->count : $length;
27 6
    $this->rewind();
28 6
  }
29
30
  /**
31
   * @return int
32
   */
33
  public function key() {
34
    return $this->pos;
35
  }
36
37
  /**
38
   * @return array
39
   */
40 6
  public function current() {
41 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...
42 6
    for($i = 0; $i < $this->length; $i++)
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of FOR statements.
Loading history...
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
43 6
      $r[] = $this->dataset[$this->c[$i]];
44 6
    return $r;
45
  }
46
47
  /**
48
   *
49
   */
50 6
  public function next() {
51 6
    if ($this->_next())
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
52 6
      $this->pos++;
53
    else
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of ELSE statements.
Loading history...
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
54 6
      $this->pos = -1;
55 6
  }
56
57
  /**
58
   *
59
   */
60 6
  public function rewind() {
61 6
    $this->c = range(0, $this->length);
62 6
    $this->pos = 0;
63 6
  }
64
65
  /**
66
   * @return bool
67
   */
68 6
  public function valid() {
69 6
    return $this->pos >= 0;
70
  }
71
72
  /**
73
   * @return bool
74
   */
75 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...
76 6
    $i = $this->length - 1;
77 6
    while ($i >= 0 && $this->c[$i] == $this->count - $this->length + $i) {
78 6
      $i--;
79 6
    }
80 6
    if($i < 0) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
81 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...
82
    }
83 4
    $this->c[$i]++;
84 4
    while($i++ < $this->length - 1) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
85 4
      $this->c[$i] = $this->c[$i - 1] + 1;
86 4
    }
87
88 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...
89
  }
90
91
  /**
92
   * @return array
93
   */
94 3
  public function toArray() {
95 3
    $data = [];
96 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...
97 3
      $data[] = $this->current();
98 3
    }
99 3
    return $data;
100
  }
101
102
  /**
103
   * @return int
104
   */
105 3
  public function count() {
106 3
    return count($this->dataset);
107
  }
108
109
  /**
110
   * @return array|null
111
   */
112 3
  public function getDataset() {
113 3
    return $this->dataset;
114
  }
115
116
  /**
117
   * @return int|null
118
   */
119 3
  public function getLength() {
120 3
    return $this->length;
121
  }
122
}
123