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 |
||
22 | class MyersDiffTest extends \PHPUnit_Framework_TestCase { |
||
23 | /** |
||
24 | * Test empty sequences. |
||
25 | * |
||
26 | * @return void |
||
27 | */ |
||
28 | public function testBothEmpty() { |
||
36 | |||
37 | /** |
||
38 | * Test one empty sequence. |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function testFirstEmpty() { |
||
54 | |||
55 | /** |
||
56 | * Test one empty sequence. |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | public function testSecondEmpty() { |
||
72 | |||
73 | /** |
||
74 | * Test identical sequences containing one token. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | public function testIdenticalOne() { |
||
88 | |||
89 | /** |
||
90 | * Test identical sequences containing two tokens. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | View Code Duplication | public function testIdenticalTwo() { |
|
105 | |||
106 | /** |
||
107 | * Test identical sequences containing three tokens. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | public function testIdenticalThree() { |
||
123 | |||
124 | /** |
||
125 | * Test different strings containing one token. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | View Code Duplication | public function testSingleDifferentONe() { |
|
140 | |||
141 | /** |
||
142 | * Test different strings containing two token. |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | View Code Duplication | public function testSingleDifferentTwo() { |
|
159 | |||
160 | /** |
||
161 | * Test different strings containing three token. |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function testSingleDifferentThree() { |
||
180 | |||
181 | /** |
||
182 | * Test two non-empty sequences. |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function testBothNonEmpty() { |
||
204 | |||
205 | /** |
||
206 | * Test delete-before-insert. Delete/insert gives the same |
||
207 | * result as insert/delete. Our algorithm consistently |
||
208 | * deletes first. |
||
209 | * |
||
210 | * void |
||
211 | */ |
||
212 | View Code Duplication | public function testDeleteBeforeInsert() { |
|
225 | } |
||
226 |
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.