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

Combinations   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 113
Duplicated Lines 6.19 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
wmc 16
cbo 1
dl 7
loc 113
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A key() 0 3 1
A rewind() 0 4 1
A valid() 0 3 1
A count() 0 3 1
A current() 0 7 2
A next() 0 8 2
B _next() 0 15 5
A toArray() 7 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 Combinations.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Combinations extends Combinatorics implements \Iterator {
13
14
  /**
15
   * @var array
16
   */
17
  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...
18
19
  /**
20
   * @var int
21
   */
22
  protected $count = 0;
23
24
  /**
25
   * @var int
26
   */
27
  protected $pos = 0;
28
29
  /**
30
   * Combinations constructor.
31
   *
32
   * @param array $dataset
33
   * @param null $length
34
   */
35 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...
36 6
    parent::__construct($dataset, $length);
37 6
    $this->rewind();
38 6
  }
39
40
  /**
41
   * @return int
42
   */
43
  public function key() {
44
    return $this->pos;
45
  }
46
47
  /**
48
   * @return array
49
   */
50 6
  public function current() {
51 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...
52 6
    for ($i = 0; $i < $this->length; $i++) {
53 6
      $r[] = $this->dataset[$this->c[$i]];
54
    }
55 6
    return $r;
56
  }
57
58
  /**
59
   *
60
   */
61 6
  public function next() {
62 6
    if ($this->_next()) {
63 4
      $this->pos++;
64
    }
65
    else {
66 6
      $this->pos = -1;
67
    }
68 6
  }
69
70
  /**
71
   *
72
   */
73 6
  public function rewind() {
74 6
    $this->c = range(0, $this->length);
75 6
    $this->pos = 0;
76 6
  }
77
78
  /**
79
   * @return bool
80
   */
81 6
  public function valid() {
82 6
    return $this->pos >= 0;
83
  }
84
85
  /**
86
   * @return bool
87
   */
88 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...
89 6
    $i = $this->length - 1;
90 6
    while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) {
91 6
      $i--;
92
    }
93 6
    if ($i < 0) {
94 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...
95
    }
96 4
    $this->c[$i]++;
97 4
    while ($i++ < $this->length - 1) {
98 4
      $this->c[$i] = $this->c[$i - 1] + 1;
99
    }
100
101 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...
102
  }
103
104
  /**
105
   * @return array
106
   */
107 6 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...
108 6
    $data = [];
109
110 6
    for ($this->rewind(); $this->valid(); $this->next()) {
111 6
      $data[] = $this->current();
112
    }
113
114 6
    return $data;
115
  }
116
117
  /**
118
   * @return int
119
   */
120 3
  public function count() {
121 3
    return $this->fact(count($this->getDataset())) / $this->fact($this->getLength());
122
  }
123
124
}
125