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 |
||
7 | class DamerauLevenshteinTest extends TestCase |
||
8 | { |
||
9 | /** |
||
10 | * Data provider for `getSimilarity`. |
||
11 | * |
||
12 | * @return array |
||
13 | */ |
||
14 | public function getSimilarityProvider(): array |
||
36 | |||
37 | /** |
||
38 | * Tests `getSimilarity`. |
||
39 | * |
||
40 | * @param string $firstString |
||
41 | * @param string $secondString |
||
42 | * @param int $expected |
||
43 | * @return void |
||
44 | * @dataProvider getSimilarityProvider |
||
45 | */ |
||
46 | public function testGetSimilarity(string $firstString, string $secondString, int $expected): void |
||
53 | |||
54 | /** |
||
55 | * Tests `getInsCost`. |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | View Code Duplication | public function testGetInsCost(): void |
|
89 | |||
90 | /** |
||
91 | * Tests `setInsCost`. |
||
92 | * |
||
93 | * @param int $cost |
||
94 | * @return void |
||
95 | * @dataProvider setXCostProvider |
||
96 | */ |
||
97 | View Code Duplication | public function testSetInsCost(int $cost): void |
|
105 | |||
106 | /** |
||
107 | * Tests `getDelCost`. |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | View Code Duplication | public function testGetDelCost(): void |
|
141 | |||
142 | /** |
||
143 | * Data provider for `set<x>Cost`. |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function setXCostProvider(): array |
||
154 | |||
155 | /** |
||
156 | * Tests `setDelCost`. |
||
157 | * |
||
158 | * @param int $cost |
||
159 | * @return void |
||
160 | * @dataProvider setXCostProvider |
||
161 | */ |
||
162 | View Code Duplication | public function testSetDelCost(int $cost): void |
|
170 | |||
171 | /** |
||
172 | * Tests `getSubCost`. |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | View Code Duplication | public function testGetSubCost(): void |
|
206 | |||
207 | /** |
||
208 | * Tests `setSubCost`. |
||
209 | * |
||
210 | * @param int $cost |
||
211 | * @return void |
||
212 | * @dataProvider setXCostProvider |
||
213 | */ |
||
214 | View Code Duplication | public function testSetSubCost(int $cost): void |
|
222 | |||
223 | /** |
||
224 | * Tests `getTransCost`. |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | View Code Duplication | public function testGetTransCost(): void |
|
258 | |||
259 | /** |
||
260 | * Tests `setTransCost`. |
||
261 | * |
||
262 | * @param int $cost |
||
263 | * @return void |
||
264 | * @dataProvider setXCostProvider |
||
265 | */ |
||
266 | View Code Duplication | public function testSetTransCost(int $cost): void |
|
274 | |||
275 | /** |
||
276 | * Data provider for `getRelativeDistance`. |
||
277 | * |
||
278 | * @return array |
||
279 | */ |
||
280 | public function getRelativeDistanceProvider(): array |
||
290 | |||
291 | /** |
||
292 | * Tests `getRelativeDistance`. |
||
293 | * |
||
294 | * @param string $firstString |
||
295 | * @param string $secondString |
||
296 | * @param float $expected |
||
297 | * @return void |
||
298 | * @dataProvider getRelativeDistanceProvider |
||
299 | */ |
||
300 | public function testGetRelativeDistance(string $firstString, string $secondString, float $expected): void |
||
308 | |||
309 | /** |
||
310 | * Tests `getMatrix`. |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | public function testGetMatrix(): void |
||
328 | |||
329 | /** |
||
330 | * Tests `displayMatrix`. |
||
331 | * |
||
332 | * @return void |
||
333 | */ |
||
334 | public function testDisplayMatrix(): void |
||
349 | |||
350 | /** |
||
351 | * Returns the default costs. |
||
352 | * |
||
353 | * @return array Costs (insert, delete, substitution, transposition) |
||
354 | */ |
||
355 | protected function getDefaultCosts(): array |
||
364 | |||
365 | /** |
||
366 | * Returns the default strings. |
||
367 | * |
||
368 | * @return array Strings (foo, bar) |
||
369 | */ |
||
370 | protected function getDefaultStrings(): array |
||
377 | } |
||
378 |
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.