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 |
||
21 | class IntegerTests extends NumberTestCase |
||
22 | { |
||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | protected function randomNativeNumber() |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | protected function uniqueNativeNumber() |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | protected function negativeNativeNumber() |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | protected function invalidNativeNumber() |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | protected function fromNative($value) |
||
62 | |||
63 | /* |
||
64 | * Test sqrt. |
||
65 | */ |
||
66 | public function testSqrt() |
||
89 | |||
90 | /* |
||
91 | * Test inc. |
||
92 | */ |
||
93 | public function testInc() |
||
107 | |||
108 | /* |
||
109 | * Test dec. |
||
110 | */ |
||
111 | public function testDec() |
||
125 | |||
126 | /* |
||
127 | * Test mod. |
||
128 | */ |
||
129 | public function testMod() |
||
144 | |||
145 | /* |
||
146 | * Test isEven. |
||
147 | */ |
||
148 | View Code Duplication | public function testIsEven() |
|
162 | |||
163 | /* |
||
164 | * Test isOdd. |
||
165 | */ |
||
166 | View Code Duplication | public function testIsOdd() |
|
180 | } |
||
181 |
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.