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 |
||
| 10 | trait RegistersUsers |
||
| 11 | { |
||
| 12 | use \Illuminate\Foundation\Auth\RegistersUsers { |
||
| 13 | register as baseRegister; |
||
| 14 | } |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Get redirect path after a successful confirmation. |
||
| 18 | * |
||
| 19 | * @return string |
||
| 20 | */ |
||
| 21 | View Code Duplication | public function redirectAfterConfirmationPath() |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Get redirect path after a registration that still needs to be confirmed. |
||
| 32 | * |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function redirectAfterRegistrationPath() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Get redirect path after the confirmation was sent. |
||
| 46 | * |
||
| 47 | * @return string |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function redirectAfterResendConfirmationPath() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Confirm a user with a given confirmation code. |
||
| 60 | * |
||
| 61 | * @param $confirmation_code |
||
| 62 | * @return \Illuminate\Http\RedirectResponse |
||
| 63 | */ |
||
| 64 | public function confirm($confirmation_code) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Resend a confirmation code to a user. |
||
| 82 | * |
||
| 83 | * @param \Illuminate\Http\Request $request |
||
| 84 | * @return \Illuminate\Http\RedirectResponse |
||
| 85 | */ |
||
| 86 | public function resendConfirmation(Request $request) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Handle a registration request for the application. |
||
| 98 | * |
||
| 99 | * @param \Illuminate\Http\Request $request |
||
| 100 | * @return \Illuminate\Http\Response |
||
| 101 | */ |
||
| 102 | public function register(Request $request) |
||
| 103 | { |
||
| 104 | $this->validator($request->all())->validate(); |
||
| 105 | |||
| 106 | event(new Registered($user = $this->create($request->all()))); |
||
| 107 | |||
| 108 | $this->sendConfirmationToUser($user); |
||
| 109 | |||
| 110 | return $this->registered($request, $user) |
||
| 111 | ?: redirect($this->redirectAfterRegistrationPath())->with('confirmation', __('confirmation::confirmation.confirmation_info')); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Send the confirmation code to a user. |
||
| 116 | * |
||
| 117 | * @param $user |
||
| 118 | */ |
||
| 119 | protected function sendConfirmationToUser($user) |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * The users email address has been confirmed. |
||
| 133 | * |
||
| 134 | * @param mixed $user |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | protected function confirmed($user) { |
||
| 140 | } |
||
| 141 |
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.