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 Prime extends Combinatorics implements \Iterator, \Countable |
||
13 | { |
||
14 | /** |
||
15 | * The minimum limit. |
||
16 | * |
||
17 | * @var int |
||
18 | */ |
||
19 | protected $min; |
||
20 | |||
21 | /** |
||
22 | * The maximum limit. |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $max; |
||
27 | |||
28 | /** |
||
29 | * The key. |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $key; |
||
34 | |||
35 | /** |
||
36 | * Prime constructor. |
||
37 | */ |
||
38 | 4 | public function __construct() |
|
39 | { |
||
40 | 4 | $this->setMaxLimit(PHP_INT_MAX); |
|
41 | 4 | $this->setMinLimit(0); |
|
42 | 4 | } |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 4 | View Code Duplication | public function current() |
|
|||
48 | { |
||
49 | 4 | for ($i = $this->key(); $i < $this->getMaxLimit(); $i++) { |
|
50 | 4 | if ($this->isPrimeNumber($i)) { |
|
51 | 4 | $this->key = $i; |
|
52 | |||
53 | 4 | return $i; |
|
54 | } |
||
55 | 4 | } |
|
56 | |||
57 | 4 | return $this->getMaxLimit(); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 4 | public function next() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 4 | public function key() |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 4 | public function valid() |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 4 | public function rewind() |
|
91 | |||
92 | /** |
||
93 | * Count elements of an object. |
||
94 | * |
||
95 | * @return int |
||
96 | * The number of element. |
||
97 | */ |
||
98 | 4 | public function count() |
|
102 | |||
103 | /** |
||
104 | * Convert the iterator into an array. |
||
105 | * |
||
106 | * @return array |
||
107 | * The elements. |
||
108 | */ |
||
109 | 4 | public function toArray() |
|
119 | |||
120 | /** |
||
121 | * Set the maximum limit. |
||
122 | * |
||
123 | * @param int $max |
||
124 | * The limit. |
||
125 | */ |
||
126 | 4 | public function setMaxLimit($max) |
|
127 | { |
||
128 | 4 | $this->max = $max; |
|
129 | 4 | } |
|
130 | |||
131 | /** |
||
132 | * Get the maximum limit. |
||
133 | * |
||
134 | * @return int |
||
135 | * The limit. |
||
136 | */ |
||
137 | 4 | public function getMaxLimit() |
|
141 | |||
142 | /** |
||
143 | * Set the minimum limit. |
||
144 | * |
||
145 | * @param int $min |
||
146 | * The limit. |
||
147 | */ |
||
148 | 4 | public function setMinLimit($min) |
|
152 | |||
153 | /** |
||
154 | * Get the minimum limit. |
||
155 | * |
||
156 | * @return int |
||
157 | * The limit. |
||
158 | */ |
||
159 | 4 | public function getMinLimit() |
|
163 | |||
164 | /** |
||
165 | * Test if a number is prime or not. |
||
166 | * |
||
167 | * @param int $number |
||
168 | * The number to test. |
||
169 | * |
||
170 | * @return bool |
||
171 | * The true if the number is prime, false otherwise. |
||
172 | */ |
||
173 | 4 | protected function isPrimeNumber($number) |
|
196 | } |
||
197 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.