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 |
||
9 | class CValidateTest extends \PHPUnit_Framework_TestCase |
||
10 | { |
||
11 | /** |
||
12 | * Provider for test values |
||
13 | * |
||
14 | * @return array |
||
15 | */ |
||
16 | View Code Duplication | public function providerCheck() |
|
25 | |||
26 | |||
27 | |||
28 | /** |
||
29 | * Test |
||
30 | * |
||
31 | * @return void |
||
32 | * |
||
33 | * @dataProvider providerCheck |
||
34 | * |
||
35 | */ |
||
36 | public function testCheck($value, $rules) |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * Test |
||
48 | * |
||
49 | * @dataProvider providerCheckFail |
||
50 | * |
||
51 | * @expectedException Exception |
||
52 | * |
||
53 | * @return void |
||
54 | * |
||
55 | */ |
||
56 | public function testValidationRuleMissing() |
||
61 | |||
62 | |||
63 | |||
64 | /** |
||
65 | * Provider for test values |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | View Code Duplication | public function providerCheckFail() |
|
78 | |||
79 | |||
80 | |||
81 | /** |
||
82 | * Test |
||
83 | * |
||
84 | * @dataProvider providerCheckFail |
||
85 | * |
||
86 | * @expectedException Exception |
||
87 | * |
||
88 | * @return void |
||
89 | * |
||
90 | */ |
||
91 | public function testCheckFailException($value, $rules) |
||
96 | |||
97 | |||
98 | |||
99 | /** |
||
100 | * Test |
||
101 | * |
||
102 | * @dataProvider providerCheckFail |
||
103 | * |
||
104 | * @return void |
||
105 | * |
||
106 | */ |
||
107 | public function testCheckFail($value, $rules) |
||
113 | } |
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.