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 | public function test_authRegister() |
||
52 | |||
53 | /** |
||
54 | * @depends test_authRegister |
||
55 | * Testing for the login with correct credentials |
||
56 | */ |
||
57 | |||
58 | public function test_authLogin() |
||
78 | |||
79 | /** |
||
80 | * @depends test_authRegister |
||
81 | * Testing for the login with empty credentials |
||
82 | */ |
||
83 | |||
84 | public function test_authLoginEmptyValues() |
||
106 | |||
107 | /** |
||
108 | * @depends test_authRegister |
||
109 | * Testing for the login with invalid or wrong email |
||
110 | */ |
||
111 | |||
112 | View Code Duplication | public function test_authLoginWrongEmail() |
|
130 | |||
131 | /** |
||
132 | * @depends test_authRegister |
||
133 | * Testing for the login with invalid email credentials |
||
134 | */ |
||
135 | View Code Duplication | public function test_authLoginInvalidUsernameEmail() |
|
153 | |||
154 | /** |
||
155 | * @depends test_authRegister |
||
156 | * Testing for the login with invalid password credentials |
||
157 | */ |
||
158 | View Code Duplication | public function test_authLoginInvalidPassword() |
|
175 | |||
176 | /** |
||
177 | * @depends test_authRegister |
||
178 | * Testing for the Profile::class with valid login_id |
||
179 | */ |
||
180 | public function test_getProfile() |
||
190 | |||
191 | /** |
||
192 | * @depends test_authRegister |
||
193 | * Testing for the Profile::class with invalid login_id |
||
194 | */ |
||
195 | public function test_getProfileInvalidID() |
||
200 | |||
201 | /** |
||
202 | * @depends test_authRegister |
||
203 | * Testing for the User::class with valid login_id |
||
204 | */ |
||
205 | public function test_userDetails() |
||
221 | |||
222 | /** |
||
223 | * @depends test_authRegister |
||
224 | * Testing for the User::class with invalid data |
||
225 | */ |
||
226 | public function test_userDetailsInvalidID() |
||
231 | |||
232 | /** |
||
233 | * @depends test_authRegister |
||
234 | * Testing for the Validate::class for email |
||
235 | */ |
||
236 | public function test_validateEmailInDb() |
||
241 | |||
242 | /** |
||
243 | * @depends test_authRegister |
||
244 | * Testing for the Validate::class for username |
||
245 | */ |
||
246 | public function test_validateUsernameInDb() |
||
251 | |||
252 | /** |
||
253 | * @depends test_authRegister |
||
254 | * Testing for the Validate::class for non-existing username |
||
255 | */ |
||
256 | public function test_validateUsernameInDbNot() |
||
261 | |||
262 | /** |
||
263 | * @depends test_authRegister |
||
264 | * Testing for the Validate::class for non-existing email |
||
265 | */ |
||
266 | public function test_validateEmailInDbNot() |
||
271 | |||
272 | /** |
||
273 | * @depends test_authRegister |
||
274 | * Testing for the Online::class |
||
275 | */ |
||
276 | public function test_Online() |
||
287 | |||
288 | |||
289 | /** |
||
290 | * @depends test_Online |
||
291 | * Empty the DB |
||
292 | */ |
||
293 | public function test_EmptyDB() |
||
308 | } |
||
309 | |||
310 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.