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 |
||
19 | class Integer extends Number |
||
20 | { |
||
21 | /** |
||
22 | * @param int $value |
||
23 | * |
||
24 | * @return \Cubiche\Domain\System\Integer |
||
25 | */ |
||
26 | public static function fromNative($value) |
||
30 | |||
31 | /** |
||
32 | * @param int $value |
||
33 | * |
||
34 | * @throws \InvalidArgumentException |
||
35 | */ |
||
36 | protected function __construct($value) |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function toInteger(RoundingMode $roundingMode = null) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function toReal() |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function toDecimal() |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function isInfinite() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function isPositive() |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function isNegative() |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function isZero() |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | protected function invertedAdd(Number $x) |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function addInteger(Integer $x) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function addReal(Real $x) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | public function addDecimal(Decimal $x, $scale = null) |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | protected function invertedSub(Number $x) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function subInteger(Integer $x) |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function subReal(Real $x) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function subDecimal(Decimal $x, $scale = null) |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | protected function invertedMult(Number $x) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function multInteger(Integer $x) |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function multReal(Real $x) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function multDecimal(Decimal $x, $scale = null) |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | protected function invertedDiv(Number $x) |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | View Code Duplication | public function divInteger(Integer $x) |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function divReal(Real $x) |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function divDecimal(Decimal $x, $scale = null) |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | protected function invertedPow(Number $x) |
||
245 | |||
246 | /** |
||
247 | * @param int $x |
||
248 | * |
||
249 | * @return Number |
||
250 | */ |
||
251 | public function powInteger(Integer $x) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function powReal(Real $x) |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function powDecimal(Decimal $x, $scale = null) |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function sqrt($scale = null) |
||
279 | |||
280 | /** |
||
281 | * @return \Cubiche\Domain\System\Integer |
||
282 | */ |
||
283 | public function inc() |
||
287 | |||
288 | /** |
||
289 | * @return \Cubiche\Domain\System\Integer |
||
290 | */ |
||
291 | public function dec() |
||
295 | |||
296 | /** |
||
297 | * @param int $x |
||
298 | * |
||
299 | * @return \Cubiche\Domain\System\Integer |
||
300 | */ |
||
301 | public function mod(Integer $x) |
||
305 | |||
306 | /** |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function isEven() |
||
313 | |||
314 | /** |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function isOdd() |
||
321 | } |
||
322 |
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.