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 ApiAuthControllerTest extends BaseTest |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public static function setUpBeforeClass() |
||
29 | |||
30 | //============================================================================================================== |
||
31 | //================================================= TESTS ==================================================== |
||
32 | //============================================================================================================== |
||
33 | |||
34 | /** |
||
35 | * @covers ::actionAuthenticate |
||
36 | */ |
||
37 | public function testApiAuthControllerAuthenticateShouldReturnErrorWhenNotPostRequest() |
||
38 | { |
||
39 | $this->setSimpleMockApiAuthService(); |
||
40 | $this->setMockRequestService('GET'); |
||
41 | |||
42 | $apiAuthController = $this->getMockApiAuthController('returnErrorJson', ''); |
||
43 | |||
44 | $apiAuthController->actionAuthenticate(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @covers ::actionAuthenticate |
||
49 | */ |
||
50 | public function testApiAuthControllerAuthenticateShouldReturnBadCredentialsWhenLoginFails() |
||
64 | |||
65 | /** |
||
66 | * @covers ::actionAuthenticate |
||
67 | */ |
||
68 | View Code Duplication | public function testApiAuthControllerAuthenticateShouldReturnInternalServerErrorWhenKeyCannotBeSaved() |
|
84 | |||
85 | /** |
||
86 | * @covers ::actionAuthenticate |
||
87 | */ |
||
88 | View Code Duplication | public function testApiAuthControllerAuthenticateShouldReturnKey() |
|
103 | |||
104 | /** |
||
105 | * @covers ::actionResetPassword |
||
106 | */ |
||
107 | public function testApiAuthControllerResetPasswordShouldReturnErrorWhenNotPostRequest() |
||
116 | |||
117 | /** |
||
118 | * @covers ::actionResetPassword |
||
119 | */ |
||
120 | public function testApiAuthControllerResetPasswordShouldReturnSuccessMessageWhenUserNotFound() |
||
133 | |||
134 | /** |
||
135 | * @covers ::actionResetPassword |
||
136 | */ |
||
137 | public function testApiAuthControllerResetPasswordShouldSendMailWhenUserFound() |
||
156 | |||
157 | //============================================================================================================== |
||
158 | //================================================= MOCKS ==================================================== |
||
159 | //============================================================================================================== |
||
160 | |||
161 | /** |
||
162 | * @param string $method |
||
163 | * @param mixed $param |
||
164 | * @param mixed $return |
||
165 | * @return ApiAuthController|mock |
||
166 | */ |
||
167 | private function getMockApiAuthController($method, $param, $return = null) |
||
181 | |||
182 | /** |
||
183 | * @return UserModel|mock |
||
184 | */ |
||
185 | private function getMockUser() |
||
193 | |||
194 | /** |
||
195 | * @param string $requestType |
||
196 | * @param string $username |
||
197 | * @param string $password |
||
198 | * @param int $count |
||
199 | * |
||
200 | * @return UserPermissionsService|mock |
||
201 | */ |
||
202 | private function setMockRequestService($requestType, $username = null, $password = null, $count = 2) |
||
225 | |||
226 | /** |
||
227 | * @param string $username |
||
228 | * @param string $password |
||
229 | * @param bool $success |
||
230 | * @param UserModel $mockUser |
||
231 | * |
||
232 | * @return UserSessionService|mock |
||
233 | */ |
||
234 | private function setMockUserSessionService($username, $password, $success = true, UserModel $mockUser = null) |
||
255 | |||
256 | /** |
||
257 | * @return mock|ApiAuthService |
||
258 | */ |
||
259 | private function setSimpleMockApiAuthService() |
||
269 | |||
270 | /** |
||
271 | * @param string $key |
||
272 | * @param UserModel $mockUser |
||
273 | * @param bool $success |
||
274 | * |
||
275 | * @return mock|ApiAuthService |
||
276 | */ |
||
277 | private function setMockApiAuthService($key, UserModel $mockUser, $success) |
||
293 | |||
294 | /** |
||
295 | * @param string $username |
||
296 | * @param UserModel $user |
||
297 | * @return mock|UsersService |
||
298 | */ |
||
299 | View Code Duplication | private function setMockUsersService($username, UserModel $user = null) |
|
314 | } |
||
315 |
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.