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 |
||
16 | class UsersController extends FOSRestController |
||
17 | { |
||
18 | use HandleUserTrait; |
||
19 | |||
20 | const TOKEN_DIGITS = 6; |
||
21 | /** |
||
22 | * @Annotations\Post("/users") |
||
23 | * @ParamConverter("user", converter="fos_rest.request_body") |
||
24 | */ |
||
25 | 10 | public function postUsersAction(User $user) |
|
26 | { |
||
27 | 10 | $manager = $this->get('fos_user.user_manager'); |
|
28 | |||
29 | 10 | $token = $manager->deleteIfNonEnabledExists($user); |
|
30 | 10 | $errors = $this->validates( |
|
31 | 10 | $user, |
|
|
|||
32 | 'mobile_app_registration' |
||
33 | 10 | ); |
|
34 | 10 | if (count($errors) > 0) { |
|
35 | 5 | return $this->handleView( |
|
36 | 5 | new View($errors, Response::HTTP_BAD_REQUEST) |
|
37 | 5 | ); |
|
38 | } |
||
39 | |||
40 | // we need to create a new user to get the salt |
||
41 | 8 | $newUser = $manager->createUser(); |
|
42 | |||
43 | 8 | $phoneNumber = $user->getPhoneNumber(); |
|
44 | 8 | $email = $user->getEmail(); |
|
45 | |||
46 | // normalize phone number with international suffix |
||
47 | 8 | if (!is_null($phoneNumber)) { |
|
48 | 5 | $phoneNumber = str_replace('+', '00', $phoneNumber); |
|
49 | 5 | } |
|
50 | |||
51 | 8 | $newUser->setUsername($user->getUsername()); |
|
52 | 8 | $newUser->setPhoneNumber($phoneNumber); |
|
53 | 8 | $newUser->setEmail($email); |
|
54 | 8 | $newUser->setPlainPassword($user->getPlainPassword()); |
|
55 | 8 | $newUser->setRoles(array('ROLE_USER')); |
|
56 | |||
57 | 8 | if (is_null($token)) { |
|
58 | 8 | $token = $this->generateToken(); |
|
59 | 8 | } |
|
60 | 8 | $this->sendToken($email, $phoneNumber, $token); |
|
61 | |||
62 | 8 | $newUser->setConfirmationToken($token); |
|
63 | 8 | $newUser->setEnabled(false); |
|
64 | 8 | $newUser->setLocked(true); |
|
65 | 8 | $manager->updateUser($newUser); |
|
66 | |||
67 | 8 | return $this->handleView( |
|
68 | 8 | new View( |
|
69 | array( |
||
70 | 8 | 'id' => $newUser->getId(), |
|
71 | 8 | ), |
|
72 | Response::HTTP_ACCEPTED |
||
73 | 8 | ) |
|
74 | 8 | ); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @Annotations\Patch("/users/{id}/password") |
||
79 | */ |
||
80 | 2 | public function patchUserPasswordAction(User $user, Request $request) |
|
101 | |||
102 | /** |
||
103 | * Request change user's email or phone. |
||
104 | * |
||
105 | * @Annotations\Patch("/users/{id}/request-change-contact-info") |
||
106 | */ |
||
107 | 6 | public function patchUserRequestChangeContactInfoAction( |
|
168 | |||
169 | /** |
||
170 | * change user's email or phone, with validation code received in previous step. |
||
171 | * |
||
172 | * @Annotations\Patch("/users/{id}/contact-info") |
||
173 | */ |
||
174 | 5 | public function patchUserChangeContactInfoAction(User $user, Request $request) |
|
175 | { |
||
176 | 5 | $requestData = $this->requestIsJsonWithKeysOrThrow( |
|
177 | 5 | $request, |
|
178 | 5 | ['new_contact_info', 'validation_code'] |
|
179 | 5 | ); |
|
180 | |||
181 | 5 | if ($requestData['validation_code'] !== $user->getConfirmationToken()) { |
|
182 | 1 | throw new BadRequestHttpException('wrong validation code'); |
|
183 | } |
||
184 | |||
185 | 4 | $contactInfo = $requestData['new_contact_info']; |
|
186 | |||
187 | 4 | $manager = $this->get('fos_user.user_manager'); |
|
188 | 4 | $validator = $this->container->get('validator'); |
|
189 | |||
190 | 4 | $emailAssert = new Assert\Email(); |
|
191 | 4 | $emailAssert->message = 'bst.email.invalid'; |
|
192 | |||
193 | 4 | $errors = $validator->validateValue($contactInfo, $emailAssert); |
|
194 | 4 | View Code Duplication | if (count($errors) === 0) { |
195 | 1 | $this->get('logger')->info( |
|
196 | 'updated email of '. |
||
197 | 1 | $user->getId(). |
|
198 | 1 | ' with '. |
|
199 | $contactInfo |
||
200 | 1 | ); |
|
201 | 1 | $user->setEmail($contactInfo); |
|
202 | 1 | $manager->updateUser($user); |
|
203 | |||
204 | 1 | return $this->handleView(new View()); |
|
205 | } |
||
206 | |||
207 | // we set user directly here so we can reuse the validator |
||
208 | // of User entity for phone number |
||
209 | 3 | $phoneNumber = str_replace('+', '00', $contactInfo); |
|
210 | 3 | $user->setPhoneNumber($phoneNumber); |
|
211 | |||
212 | 3 | $errors = $validator->validate($user, ['phone_check']); |
|
213 | 3 | View Code Duplication | if (count($errors) === 0) { |
214 | 1 | $this->get('logger')->info( |
|
215 | 'updated phone of '. |
||
216 | 1 | $user->getId(). |
|
217 | 1 | ' with '. |
|
218 | $phoneNumber |
||
219 | 2 | ); |
|
220 | 1 | $manager->updateUser($user); |
|
221 | |||
222 | 1 | return $this->handleView(new View()); |
|
223 | } |
||
224 | |||
225 | 2 | return $this->handleView( |
|
226 | 2 | new View( |
|
227 | 2 | ['message' => 'bst.changecontactinfo.invalid'], |
|
228 | Response::HTTP_BAD_REQUEST |
||
229 | 2 | ) |
|
230 | 2 | ); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Permit a user who has forgotten his password to request |
||
235 | * a validation to be sent to either his email or phone number. |
||
236 | * |
||
237 | * @Annotations\Post("/users/forgot-password") |
||
238 | */ |
||
239 | 2 | public function postUsersForgotPasswordAction(Request $request) |
|
271 | |||
272 | /** |
||
273 | * Used for a user to resend his confirmation token. |
||
274 | * |
||
275 | * @param User $user the user who's reseting password |
||
276 | * @param Request $request |
||
277 | * |
||
278 | * @Annotations\Patch("/users/{id}/resend-confirmation-token") |
||
279 | */ |
||
280 | 1 | public function patchUserResendConfirmationTokenAction(User $user) |
|
290 | |||
291 | /** |
||
292 | * Used for a user to reset his password if he's in possession |
||
293 | * of a validation code send to him during an earlier step. |
||
294 | * |
||
295 | * @param User $user the user who's reseting password |
||
296 | * @param Request $request |
||
297 | * |
||
298 | * @Annotations\Patch("/users/{id}/reset-password") |
||
299 | */ |
||
300 | 2 | public function patchUserResetPasswordAction( |
|
317 | |||
318 | /** |
||
319 | * @Annotations\Put("/users/{id}/confirmation-token/{confirmationToken}") |
||
320 | */ |
||
321 | 2 | public function putUserActivationCodeAction(User $user, $confirmationToken) |
|
340 | |||
341 | /** |
||
342 | * @return bool |
||
343 | */ |
||
344 | 7 | private function isPasswordCorrect(User $user, $password) |
|
355 | |||
356 | /** |
||
357 | */ |
||
358 | 2 | private function updateUserPassword(User $user, $newPassword) |
|
364 | |||
365 | /** |
||
366 | * Check if the JSON sent data is correct |
||
367 | * for the current called action |
||
368 | * and throws a bad request exception if the input is wrong. |
||
369 | * |
||
370 | * @return array |
||
371 | */ |
||
372 | 17 | View Code Duplication | private function requestIsJsonWithKeysOrThrow( |
387 | |||
388 | /** |
||
389 | * /!\ This method does not validate the input |
||
390 | * making sure the email is a real email and phone is an actual phone |
||
391 | * is the responsability of the calling method. |
||
392 | * |
||
393 | * @param string|null $email if null, email not sent |
||
394 | * @param string|null $phone if null, SMS not sent |
||
395 | * @param string $token token to send to the user |
||
396 | */ |
||
397 | 10 | private function sendToken( |
|
410 | |||
411 | /** |
||
412 | * |
||
413 | */ |
||
414 | 6 | private function sendTokenByEmail($email, $token) |
|
424 | |||
425 | /** |
||
426 | * |
||
427 | */ |
||
428 | 7 | private function sendTokenByPhone($phone, $token) |
|
438 | |||
439 | 13 | private function generateToken() |
|
452 | } |
||
453 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: