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 ObjectValidatorTest extends TestCase |
||
10 | { |
||
11 | public function testDefaultValidator() |
||
18 | |||
19 | View Code Duplication | public function testSetAllowUnspecified() |
|
27 | |||
28 | View Code Duplication | public function testSetSeparator() |
|
36 | |||
37 | /** |
||
38 | * @dataProvider defaultsDataProvider |
||
39 | * |
||
40 | * @param object $input |
||
41 | * @param object $expected |
||
42 | * |
||
43 | * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
||
44 | */ |
||
45 | View Code Duplication | public function testSimpleValidation($input, $expected) |
|
57 | |||
58 | /** |
||
59 | * @return array |
||
60 | */ |
||
61 | public function defaultsDataProvider() |
||
78 | |||
79 | /** |
||
80 | * @dataProvider invalidDataProvider |
||
81 | * |
||
82 | * @param object $input |
||
83 | * |
||
84 | * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
||
85 | * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
||
86 | */ |
||
87 | View Code Duplication | public function testFailedValidation($input) |
|
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | public function invalidDataProvider() |
||
111 | |||
112 | /** |
||
113 | * @dataProvider doNotAllowUnspecifiedData |
||
114 | * |
||
115 | * @param object $input |
||
116 | * |
||
117 | * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
||
118 | * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException |
||
119 | */ |
||
120 | View Code Duplication | public function testDoNotAllowUnspecified($input) |
|
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | View Code Duplication | public function doNotAllowUnspecifiedData() |
|
141 | } |
||
142 |
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.