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 = []; |
||
|
|
|||
| 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 | 10 | public function __construct(array $dataset = [], $length = null) |
|
| 37 | { |
||
| 38 | 10 | parent::__construct(array_values($dataset), $length); |
|
| 39 | 10 | $this->rewind(); |
|
| 40 | 10 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | public function key() |
||
| 46 | { |
||
| 47 | return $this->key; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | 4 | public function current() |
|
| 54 | { |
||
| 55 | 4 | $r = []; |
|
| 56 | 4 | for ($i = 0; $i < $this->length; $i++) { |
|
| 57 | 4 | $r[] = $this->dataset[$this->c[$i]]; |
|
| 58 | 4 | } |
|
| 59 | |||
| 60 | 4 | return $r; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 4 | public function next() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 10 | public function rewind() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 4 | public function valid() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Convert the iterator into an array. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | * The elements. |
||
| 97 | */ |
||
| 98 | 4 | View Code Duplication | public function toArray() |
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | 10 | public function count() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Custom next() callback. |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | * Return true or false. |
||
| 123 | */ |
||
| 124 | 4 | protected function nextHelper() |
|
| 140 | } |
||
| 141 |
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.