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 |
||
| 12 | class Combinations extends Combinatorics implements \Iterator { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The values. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $c = array(); |
||
|
|
|||
| 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) { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function key() { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 6 | public function current() { |
|
| 52 | 6 | $r = array(); |
|
| 53 | 6 | for ($i = 0; $i < $this->length; $i++) { |
|
| 54 | 6 | $r[] = $this->dataset[$this->c[$i]]; |
|
| 55 | } |
||
| 56 | 6 | return $r; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | 6 | public function next() { |
|
| 63 | 6 | if ($this->nextHelper()) { |
|
| 64 | 4 | $this->key++; |
|
| 65 | } |
||
| 66 | else { |
||
| 67 | 6 | $this->key = -1; |
|
| 68 | } |
||
| 69 | 6 | } |
|
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | 6 | public function rewind() { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | 6 | public function valid() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Custom next() callback. |
||
| 88 | * |
||
| 89 | * @return bool |
||
| 90 | * Return true or false. |
||
| 91 | */ |
||
| 92 | 6 | protected function nextHelper() { |
|
| 93 | 6 | $i = $this->length - 1; |
|
| 94 | 6 | while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
|
| 95 | 6 | $i--; |
|
| 96 | } |
||
| 97 | 6 | if ($i < 0) { |
|
| 98 | 6 | return FALSE; |
|
| 99 | } |
||
| 100 | 4 | $this->c[$i]++; |
|
| 101 | 4 | while ($i++ < $this->length - 1) { |
|
| 102 | 4 | $this->c[$i] = $this->c[$i - 1] + 1; |
|
| 103 | } |
||
| 104 | |||
| 105 | 4 | return TRUE; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Convert the iterator into an array. |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | * The elements. |
||
| 113 | */ |
||
| 114 | 6 | View Code Duplication | public function toArray() { |
| 115 | 6 | $data = []; |
|
| 116 | |||
| 117 | 6 | for ($this->rewind(); $this->valid(); $this->next()) { |
|
| 118 | 6 | $data[] = $this->current(); |
|
| 119 | } |
||
| 120 | |||
| 121 | 6 | return $data; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 3 | public function count() { |
|
| 130 | |||
| 131 | } |
||
| 132 |
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.