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 | /** |
||
| 16 | * The values. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $c = array(); |
||
|
|
|||
| 21 | |||
| 22 | /** |
||
| 23 | * The key. |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $key = 0; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Combinations constructor. |
||
| 31 | * |
||
| 32 | * @param array $dataset |
||
| 33 | * The dataset. |
||
| 34 | * @param int|null $length |
||
| 35 | * The length. |
||
| 36 | */ |
||
| 37 | 10 | public function __construct(array $dataset = array(), $length = null) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | public function key() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | 4 | public function current() |
|
| 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 | * Custom next() callback. |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | * Return true or false. |
||
| 97 | */ |
||
| 98 | 4 | protected function nextHelper() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Convert the iterator into an array. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | * The elements. |
||
| 120 | */ |
||
| 121 | 4 | View Code Duplication | public function toArray() |
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 10 | public function count() |
|
| 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.