Completed
Push — master ( 2ef9a2...e0a631 )
by Pol
02:35
created

Combinations   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 5.83 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 16
cbo 1
dl 7
loc 120
ccs 42
cts 44
cp 0.9545
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 current() 0 7 2
A next() 0 8 2
A rewind() 0 4 1
A valid() 0 3 1
B nextHelper() 0 15 5
A toArray() 7 9 2
A count() 0 3 1

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
   * The values.
16
   *
17
   * @var array
18
   */
19
  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...
20
21
  /**
22
   * The key.
23
   *
24
   * @var int
25
   */
26
  protected $key = 0;
27
28
  /**
29
   * Combinations constructor.
30
   *
31
   * @param array $dataset
32
   *   The dataset.
33
   * @param int|null $length
34
   *   The length.
35
   */
36 6
  public function __construct(array $dataset = array(), $length = NULL) {
37 6
    parent::__construct(array_values($dataset), $length);
38 6
    $this->rewind();
39 6
  }
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function key() {
45
    return $this->key;
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51 3
  public function current() {
52 3
    $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...
53 3
    for ($i = 0; $i < $this->length; $i++) {
54 3
      $r[] = $this->dataset[$this->c[$i]];
55 3
    }
56 3
    return $r;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 3
  public function next() {
63 3
    if ($this->nextHelper()) {
64 2
      $this->key++;
65 2
    }
66
    else {
67 3
      $this->key = -1;
68
    }
69 3
  }
70
71
  /**
72
   * {@inheritdoc}
73
   */
74 6
  public function rewind() {
75 6
    $this->c = range(0, $this->length);
76 6
    $this->key = 0;
77 6
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82 3
  public function valid() {
83 3
    return $this->key >= 0;
84
  }
85
86
  /**
87
   * Custom next() callback.
88
   *
89
   * @return bool
90
   *   Return true or false.
91
   */
92 3
  protected function nextHelper() {
93 3
    $i = $this->length - 1;
94 3
    while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) {
95 3
      $i--;
96 3
    }
97 3
    if ($i < 0) {
98 3
      return FALSE;
99
    }
100 2
    $this->c[$i]++;
101 2
    while ($i++ < $this->length - 1) {
102 2
      $this->c[$i] = $this->c[$i - 1] + 1;
103 2
    }
104
105 2
    return TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
106
  }
107
108
  /**
109
   * Convert the iterator into an array.
110
   *
111
   * @return array
112
   *   The elements.
113
   */
114 3 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...
115 3
    $data = [];
116
117 3
    for ($this->rewind(); $this->valid(); $this->next()) {
118 3
      $data[] = $this->current();
119 3
    }
120
121 3
    return $data;
122
  }
123
124
  /**
125
   * {@inheritdoc}
126
   */
127 6
  public function count() {
128 6
    return $this->fact(count($this->getDataset())) / ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
129
  }
130
131
}
132