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 | 3 | public function current() { |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 3 | public function next() { |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 6 | public function rewind() { |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 3 | public function valid() { |
|
85 | |||
86 | /** |
||
87 | * Custom next() callback. |
||
88 | * |
||
89 | * @return bool |
||
90 | * Return true or false. |
||
91 | */ |
||
92 | 3 | protected function nextHelper() { |
|
107 | |||
108 | /** |
||
109 | * Convert the iterator into an array. |
||
110 | * |
||
111 | * @return array |
||
112 | * The elements. |
||
113 | */ |
||
114 | 3 | View Code Duplication | public function toArray() { |
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 6 | 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.