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() { |
|
52 | 3 | $r = array(); |
|
53 | 3 | for ($i = 0; $i < $this->length; $i++) { |
|
54 | 3 | $r[] = $this->dataset[$this->c[$i]]; |
|
55 | } |
||
56 | 3 | return $r; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 3 | public function next() { |
|
63 | 3 | if ($this->nextHelper()) { |
|
64 | 2 | $this->key++; |
|
65 | } |
||
66 | else { |
||
67 | 3 | $this->key = -1; |
|
68 | } |
||
69 | 3 | } |
|
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() { |
|
93 | 3 | $i = $this->length - 1; |
|
94 | 3 | while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
|
95 | 3 | $i--; |
|
96 | } |
||
97 | 3 | if ($i < 0) { |
|
98 | 3 | return FALSE; |
|
99 | } |
||
100 | 2 | $this->c[$i]++; |
|
101 | 2 | while ($i++ < $this->length - 1) { |
|
102 | 2 | $this->c[$i] = $this->c[$i - 1] + 1; |
|
103 | } |
||
104 | |||
105 | 2 | return TRUE; |
|
106 | } |
||
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() { |
115 | 3 | $data = []; |
|
116 | |||
117 | 3 | for ($this->rewind(); $this->valid(); $this->next()) { |
|
118 | 3 | $data[] = $this->current(); |
|
119 | } |
||
120 | |||
121 | 3 | return $data; |
|
122 | } |
||
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.