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 |
||
17 | class JFormRuleEmailTest extends \PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | /** |
||
20 | * set up for testing |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | public function setUp() |
||
28 | |||
29 | /** |
||
30 | * Tear down test |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | protected function tearDown() |
||
37 | |||
38 | /** |
||
39 | * Test the Joomla\Form\Rule\Email::test method. |
||
40 | * |
||
41 | * @return void |
||
42 | */ |
||
43 | View Code Duplication | public function testEmail() |
|
76 | |||
77 | /** |
||
78 | * Data Provider for email rule test with no multiple attribute and no tld attribute |
||
79 | * |
||
80 | * @return array |
||
81 | * |
||
82 | * @since 11.1 |
||
83 | */ |
||
84 | public function emailData1() |
||
98 | |||
99 | /** |
||
100 | * Test the email rule |
||
101 | * |
||
102 | * @param string $emailAddress Email to be tested |
||
103 | * @param boolean $expectedResult Result of test |
||
104 | * |
||
105 | * @dataProvider emailData1 |
||
106 | * |
||
107 | * @return void |
||
108 | * |
||
109 | * @since 11.1 |
||
110 | */ |
||
111 | View Code Duplication | public function testEmailData($emailAddress, $expectedResult) |
|
121 | |||
122 | /** |
||
123 | * Data Provider for email rule test with multiple attribute and no tld attribute |
||
124 | * |
||
125 | * @return array |
||
126 | * |
||
127 | * @since 12.3 |
||
128 | */ |
||
129 | public function emailData2() |
||
136 | |||
137 | /** |
||
138 | * Test the email rule with the multiple attribute |
||
139 | * |
||
140 | * @param string $emailAddress Email to be tested |
||
141 | * @param boolean $expectedResult Result of test |
||
142 | * |
||
143 | * @dataProvider emailData2 |
||
144 | * |
||
145 | * @return void |
||
146 | * |
||
147 | * @since 12.3 |
||
148 | */ |
||
149 | View Code Duplication | public function testEmailData2($emailAddress, $expectedResult) |
|
159 | |||
160 | /** |
||
161 | * Data Provider for email rule test with tld attribute |
||
162 | * |
||
163 | * @return array |
||
164 | * |
||
165 | * @since 12.3 |
||
166 | */ |
||
167 | public function emailData3() |
||
177 | |||
178 | /** |
||
179 | * Test the email rule with the tld attribute |
||
180 | * |
||
181 | * @param string $emailAddress Email to be tested |
||
182 | * @param boolean $expectedResult Result of test |
||
183 | * |
||
184 | * @dataProvider emailData3 |
||
185 | * |
||
186 | * @return void |
||
187 | * |
||
188 | * @since 12.3 |
||
189 | */ |
||
190 | View Code Duplication | public function testEmailData3($emailAddress, $expectedResult) |
|
200 | } |
||
201 |
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.