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 |
||
15 | class TestUser |
||
16 | extends |
||
17 | PHPUnit_Framework_TestCase |
||
18 | { |
||
19 | protected $obRegister; |
||
20 | protected $obLogin; |
||
21 | protected $obUser; |
||
22 | protected $obValidate; |
||
23 | |||
24 | |||
25 | public function setUp() |
||
32 | |||
33 | |||
34 | View Code Duplication | public function test_authRegister() |
|
52 | |||
53 | /** |
||
54 | * @depends test_authRegister |
||
55 | * Testing for the register with empty username |
||
56 | */ |
||
57 | View Code Duplication | public function test_authregisterEmptyUsername() |
|
78 | |||
79 | /** |
||
80 | * @depends test_authRegister |
||
81 | * Testing for the register with invalid email credentials |
||
82 | */ |
||
83 | View Code Duplication | public function test_authregisterInvalidEmail() |
|
104 | |||
105 | /** |
||
106 | * @depends test_authRegister |
||
107 | * Testing for the register with repeated credentials |
||
108 | */ |
||
109 | public function test_authregisterInvalidCredentials() |
||
138 | |||
139 | /** |
||
140 | * @depends test_authRegister |
||
141 | * Testing for the login with correct credentials |
||
142 | */ |
||
143 | public function test_authLogin() |
||
163 | |||
164 | /** |
||
165 | * @depends test_authRegister |
||
166 | * Testing for the login with empty credentials |
||
167 | */ |
||
168 | public function test_authLoginEmptyValues() |
||
190 | |||
191 | /** |
||
192 | * @depends test_authRegister |
||
193 | * Testing for the login with invalid or wrong email |
||
194 | */ |
||
195 | View Code Duplication | public function test_authLoginWrongEmail() |
|
213 | |||
214 | /** |
||
215 | * @depends test_authRegister |
||
216 | * Testing for the login with invalid email credentials |
||
217 | */ |
||
218 | View Code Duplication | public function test_authLoginInvalidUsernameEmail() |
|
236 | |||
237 | /** |
||
238 | * @depends test_authRegister |
||
239 | * Testing for the login with invalid password credentials |
||
240 | */ |
||
241 | View Code Duplication | public function test_authLoginInvalidPassword() |
|
258 | |||
259 | /** |
||
260 | * @depends test_authRegister |
||
261 | * Testing for the Profile::class with valid login_id |
||
262 | */ |
||
263 | public function test_getProfile() |
||
273 | |||
274 | /** |
||
275 | * @depends test_authRegister |
||
276 | * Testing for the Profile::class with invalid login_id |
||
277 | */ |
||
278 | public function test_getProfileInvalidID() |
||
283 | |||
284 | /** |
||
285 | * @depends test_authRegister |
||
286 | * Testing for the User::class with valid login_id |
||
287 | */ |
||
288 | public function test_userDetails() |
||
304 | |||
305 | /** |
||
306 | * @depends test_authRegister |
||
307 | * Testing for the User::class with invalid data |
||
308 | */ |
||
309 | public function test_userDetailsInvalidID() |
||
314 | |||
315 | /** |
||
316 | * @depends test_authRegister |
||
317 | * Testing for the Validate::class for email |
||
318 | */ |
||
319 | public function test_validateEmailInDb() |
||
324 | |||
325 | /** |
||
326 | * @depends test_authRegister |
||
327 | * Testing for the Validate::class for username |
||
328 | */ |
||
329 | public function test_validateUsernameInDb() |
||
334 | |||
335 | /** |
||
336 | * @depends test_authRegister |
||
337 | * Testing for the Validate::class for non-existing username |
||
338 | */ |
||
339 | public function test_validateUsernameInDbNot() |
||
344 | |||
345 | /** |
||
346 | * @depends test_authRegister |
||
347 | * Testing for the Validate::class for non-existing email |
||
348 | */ |
||
349 | public function test_validateEmailInDbNot() |
||
354 | |||
355 | /** |
||
356 | * @depends test_authRegister |
||
357 | * Testing for the Online::class |
||
358 | */ |
||
359 | public function test_Online() |
||
370 | |||
371 | |||
372 | /** |
||
373 | * @depends test_Online |
||
374 | * Empty the DB |
||
375 | */ |
||
376 | public function test_EmptyDB() |
||
391 | } |
||
392 | |||
393 |
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.