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() |
|
55 | { |
||
56 | 4 | $r = array(); |
|
57 | 4 | for ($i = 0; $i < $this->length; $i++) { |
|
58 | 4 | $r[] = $this->dataset[$this->c[$i]]; |
|
59 | } |
||
60 | 4 | return $r; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 4 | public function next() |
|
67 | { |
||
68 | 4 | if ($this->nextHelper()) { |
|
69 | 3 | $this->key++; |
|
70 | } else { |
||
71 | 4 | $this->key = -1; |
|
72 | } |
||
73 | 4 | } |
|
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() |
|
99 | { |
||
100 | 4 | $i = $this->length - 1; |
|
101 | 4 | while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
|
102 | 4 | $i--; |
|
103 | } |
||
104 | 4 | if ($i < 0) { |
|
105 | 4 | return false; |
|
106 | } |
||
107 | 3 | $this->c[$i]++; |
|
108 | 3 | while ($i++ < $this->length - 1) { |
|
109 | 3 | $this->c[$i] = $this->c[$i - 1] + 1; |
|
110 | } |
||
111 | |||
112 | 3 | return true; |
|
113 | } |
||
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() |
122 | { |
||
123 | 4 | $data = []; |
|
124 | |||
125 | 4 | for ($this->rewind(); $this->valid(); $this->next()) { |
|
126 | 4 | $data[] = $this->current(); |
|
127 | } |
||
128 | |||
129 | 4 | return $data; |
|
130 | } |
||
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.