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 | * @var array |
||
16 | */ |
||
17 | protected $c = array(); |
||
|
|||
18 | |||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $count = 0; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | protected $pos = 0; |
||
28 | |||
29 | /** |
||
30 | * Combinations constructor. |
||
31 | * |
||
32 | * @param array $dataset |
||
33 | * @param null $length |
||
34 | */ |
||
35 | 6 | public function __construct(array $dataset = array(), $length = NULL) { |
|
39 | |||
40 | /** |
||
41 | * @return int |
||
42 | */ |
||
43 | public function key() { |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | 6 | public function current() { |
|
51 | 6 | $r = array(); |
|
52 | 6 | for ($i = 0; $i < $this->length; $i++) { |
|
53 | 6 | $r[] = $this->dataset[$this->c[$i]]; |
|
54 | } |
||
55 | 6 | return $r; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | */ |
||
61 | 6 | public function next() { |
|
62 | 6 | if ($this->_next()) { |
|
63 | 4 | $this->pos++; |
|
64 | } |
||
65 | else { |
||
66 | 6 | $this->pos = -1; |
|
67 | } |
||
68 | 6 | } |
|
69 | |||
70 | /** |
||
71 | * |
||
72 | */ |
||
73 | 6 | public function rewind() { |
|
77 | |||
78 | /** |
||
79 | * @return bool |
||
80 | */ |
||
81 | 6 | public function valid() { |
|
84 | |||
85 | /** |
||
86 | * @return bool |
||
87 | */ |
||
88 | 6 | protected function _next() { |
|
89 | 6 | $i = $this->length - 1; |
|
90 | 6 | while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
|
91 | 6 | $i--; |
|
92 | } |
||
93 | 6 | if ($i < 0) { |
|
94 | 6 | return FALSE; |
|
1 ignored issue
–
show
|
|||
95 | } |
||
96 | 4 | $this->c[$i]++; |
|
97 | 4 | while ($i++ < $this->length - 1) { |
|
98 | 4 | $this->c[$i] = $this->c[$i - 1] + 1; |
|
99 | } |
||
100 | |||
101 | 4 | return TRUE; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | 6 | View Code Duplication | public function toArray() { |
108 | 6 | $data = []; |
|
109 | |||
110 | 6 | for ($this->rewind(); $this->valid(); $this->next()) { |
|
111 | 6 | $data[] = $this->current(); |
|
112 | } |
||
113 | |||
114 | 6 | return $data; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return int |
||
119 | */ |
||
120 | 3 | public function count() { |
|
123 | |||
124 | } |
||
125 |
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.