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 | public function __construct(array $dataset = [], $length = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | public function key() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function current() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | public function next() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function rewind() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function valid() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Convert the iterator into an array. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | * The elements. |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function toArray() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function count() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Custom next() callback. |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | * Return true or false. |
||
| 123 | */ |
||
| 124 | 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.