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 |
||
| 10 | class Combinations extends Combinatorics implements \Iterator |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The values. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $c = []; |
||
|
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * The key. |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $key = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Combinations constructor. |
||
| 28 | * |
||
| 29 | * @param array $dataset |
||
| 30 | * The dataset |
||
| 31 | * @param int|null $length |
||
| 32 | * The length |
||
| 33 | */ |
||
| 34 | 10 | public function __construct(array $dataset = [], $length = null) |
|
| 35 | { |
||
| 36 | 10 | parent::__construct(array_values($dataset), $length); |
|
| 37 | 10 | $this->rewind(); |
|
| 38 | 10 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function key() |
||
| 44 | { |
||
| 45 | return $this->key; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 4 | public function current() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 4 | public function next() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | 10 | public function rewind() |
|
| 77 | { |
||
| 78 | 10 | $this->c = range(0, $this->length); |
|
| 79 | 10 | $this->key = 0; |
|
| 80 | 10 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 4 | public function valid() |
|
| 86 | { |
||
| 87 | 4 | return $this->key >= 0; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Convert the iterator into an array. |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | * The elements |
||
| 95 | */ |
||
| 96 | 4 | View Code Duplication | public function toArray() |
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 10 | public function count() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Custom next() callback. |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | * Return true or false |
||
| 121 | */ |
||
| 122 | 4 | protected function nextHelper() |
|
| 138 | } |
||
| 139 |
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.