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 |
||
| 19 | class PageForgotPassword extends InternalPageBase |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Main function for this page, when no specific actions are called. |
||
| 23 | * |
||
| 24 | * This is the forgotten password reset form |
||
| 25 | * @category Security-Critical |
||
| 26 | */ |
||
| 27 | protected function main() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Sends a reset email if the user is authenticated |
||
| 54 | * |
||
| 55 | * @param User|boolean $user The user located from the database, or false. Doesn't really matter, since we do the |
||
| 56 | * check anyway within this method and silently skip if we don't have a user. |
||
| 57 | * @param string $email The provided email address |
||
| 58 | */ |
||
| 59 | private function sendResetMail($user, $email) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Entry point for the reset action |
||
| 82 | * |
||
| 83 | * This is the reset password part of the form. |
||
| 84 | * @category Security-Critical |
||
| 85 | */ |
||
| 86 | protected function reset() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the user resetting their password from the database, or throwing an exception if that is not possible. |
||
| 120 | * |
||
| 121 | * @param integer $id The ID of the user to retrieve |
||
| 122 | * @param PdoDatabase $database The database object to use |
||
| 123 | * @param string $si The reset hash provided |
||
| 124 | * |
||
| 125 | * @return User |
||
| 126 | * @throws ApplicationLogicException |
||
| 127 | */ |
||
| 128 | private function getResettingUser($id, $database, $si) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Performs the setting of the new password |
||
| 141 | * |
||
| 142 | * @param User $user The user to set the password for |
||
| 143 | * |
||
| 144 | * @throws ApplicationLogicException |
||
| 145 | */ |
||
| 146 | private function doReset(User $user) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets up the security for this page. If certain actions have different permissions, this should be reflected in |
||
| 164 | * the return value from this function. |
||
| 165 | * |
||
| 166 | * If this page even supports actions, you will need to check the route |
||
| 167 | * |
||
| 168 | * @return SecurityConfiguration |
||
| 169 | * @category Security-Critical |
||
| 170 | */ |
||
| 171 | protected function getSecurityConfiguration() |
||
| 175 | } |
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.