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 |
||
| 21 | class ForgotYourPasswordController |
||
| 22 | { |
||
| 23 | private $baseUrl = 'forgot'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The logger used by this controller. |
||
| 27 | * |
||
| 28 | * @var LoggerInterface |
||
| 29 | */ |
||
| 30 | private $logger; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The template used by this controller. |
||
| 34 | * |
||
| 35 | * @var TemplateInterface |
||
| 36 | */ |
||
| 37 | private $template; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The main content block of the page. |
||
| 41 | * |
||
| 42 | * @var HtmlBlock |
||
| 43 | */ |
||
| 44 | private $content; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The Twig environment (used to render Twig templates). |
||
| 48 | * |
||
| 49 | * @var Twig_Environment |
||
| 50 | */ |
||
| 51 | private $twig; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The service that will actually check the tokens. |
||
| 55 | * |
||
| 56 | * @var ForgotYourPasswordService |
||
| 57 | */ |
||
| 58 | private $forgotYourPasswordService; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The userservice to log people when a token is found. |
||
| 62 | * |
||
| 63 | * @var UserService |
||
| 64 | */ |
||
| 65 | private $userService; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Enable this option if you want the "forgot your password screen" to NOT show the "email not found message" and always show the "an email has been sent" message. |
||
| 69 | * This improves security because an attacker typing an email address of someone else cannot not if this user is registered or not in the application. |
||
| 70 | * |
||
| 71 | * At the same time, it decreases usability. |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private $noLeak = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var PasswordStrengthCheckInterface |
||
| 79 | */ |
||
| 80 | private $passwordStrengthCheck; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The URL to continue to when password has been changed. |
||
| 84 | * Relative to the root path of the application. |
||
| 85 | * Defaults to '/'. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | private $continueUrl = '/'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Controller's constructor. |
||
| 93 | * |
||
| 94 | * @param LoggerInterface $logger The logger |
||
| 95 | * @param TemplateInterface $template The template used by this controller |
||
| 96 | * @param HtmlBlock $content The main content block of the page |
||
| 97 | * @param Twig_Environment $twig The Twig environment (used to render Twig templates) |
||
| 98 | */ |
||
| 99 | public function __construct(LoggerInterface $logger, TemplateInterface $template, HtmlBlock $content, Twig_Environment $twig, ForgotYourPasswordService $forgotYourPasswordService, UserService $userService, PasswordStrengthCheckInterface $passwordStrengthCheck) |
||
| 100 | { |
||
| 101 | $this->logger = $logger; |
||
| 102 | $this->template = $template; |
||
| 103 | $this->content = $content; |
||
| 104 | $this->twig = $twig; |
||
| 105 | $this->forgotYourPasswordService = $forgotYourPasswordService; |
||
| 106 | $this->userService = $userService; |
||
| 107 | $this->passwordStrengthCheck = $passwordStrengthCheck; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Enable this option if you want the "forgot your password screen" to NOT show the "email not found message" and always show the "an email has been sent" message. |
||
| 112 | * This improves security because an attacker typing an email address of someone else cannot not if this user is registered or not in the application. |
||
| 113 | * |
||
| 114 | * At the same time, it decreases usability. |
||
| 115 | * |
||
| 116 | * @param bool $noLeak |
||
| 117 | */ |
||
| 118 | public function setNoLeak(bool $noLeak) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The URL to continue to when password has been changed. |
||
| 125 | * Relative to the root path of the application. |
||
| 126 | * Defaults to '/'. |
||
| 127 | * |
||
| 128 | * @param string $continueUrl |
||
| 129 | */ |
||
| 130 | public function setContinueUrl(string $continueUrl) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Displays the screen to enter the email. |
||
| 137 | * |
||
| 138 | * @URL("{$this->baseUrl}/password") |
||
| 139 | * @Get |
||
| 140 | * |
||
| 141 | * @param string|null $email |
||
| 142 | * |
||
| 143 | * @return ResponseInterface |
||
| 144 | */ |
||
| 145 | public function index(string $email = null) : ResponseInterface |
||
| 146 | { |
||
| 147 | return $this->displayMainForm($email); |
||
| 148 | } |
||
| 149 | |||
| 150 | private function displayMainForm(string $email = null, bool $notFoundMessage = false) : ResponseInterface |
||
| 166 | |||
| 167 | private function jsonNotFoundResponse() : ResponseInterface |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Displays the screen to enter the email. |
||
| 176 | * |
||
| 177 | * @URL("{$this->baseUrl}/password") |
||
| 178 | * @Post |
||
| 179 | * |
||
| 180 | * @param string $email |
||
| 181 | * |
||
| 182 | * @return ResponseInterface |
||
| 183 | */ |
||
| 184 | public function submit(string $email, ServerRequestInterface $request) : ResponseInterface |
||
| 185 | { |
||
| 186 | try { |
||
| 187 | // Let's get the URL of the reset password |
||
| 188 | $currentUrl = $request->getUri()->getPath(); |
||
| 189 | $url = substr($currentUrl, 0, strrpos($currentUrl, '/password')).'/reset'; |
||
| 190 | |||
| 191 | $this->forgotYourPasswordService->sendMail($email, $request->getUri()->withPath($url)->withQuery('')); |
||
| 192 | } catch (EmailNotFoundException $exception) { |
||
| 193 | if (!$this->noLeak) { |
||
| 194 | if ($this->isJson($request)) { |
||
| 195 | return $this->jsonNotFoundResponse(); |
||
| 196 | } else { |
||
| 197 | return $this->displayMainForm($email, true); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | if ($this->isJson($request)) { |
||
| 203 | return $this->jsonSentResponse(); |
||
| 204 | } else { |
||
| 205 | return new RedirectResponse(ROOT_URL.$this->baseUrl.'/mail_sent?email='.urlencode($email)); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | private function isJson(ServerRequestInterface $request) : bool |
||
| 213 | |||
| 214 | private function jsonSentResponse() : ResponseInterface |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @URL("{$this->baseUrl}/mail_sent") |
||
| 224 | * @Get() |
||
| 225 | * |
||
| 226 | * @param string $email |
||
| 227 | * |
||
| 228 | * @return ResponseInterface |
||
| 229 | */ |
||
| 230 | public function displayMailSentScreen(string $email) : ResponseInterface |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @URL("{$this->baseUrl}/reset") |
||
| 239 | * @Get() |
||
| 240 | * |
||
| 241 | * @param string $token |
||
| 242 | * |
||
| 243 | * @return ResponseInterface |
||
| 244 | */ |
||
| 245 | public function displayResetAccessPage(string $token) : ResponseInterface |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @URL("{$this->baseUrl}/reset") |
||
| 262 | * @Post() |
||
| 263 | * |
||
| 264 | * @param string $token |
||
| 265 | * @param ServerRequestInterface $request |
||
| 266 | * |
||
| 267 | * @return ResponseInterface |
||
| 268 | */ |
||
| 269 | public function resetAccess(string $token, string $password, string $confirmpassword, ServerRequestInterface $request) : ResponseInterface |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @URL("{$this->baseUrl}/reset_confirm") |
||
| 323 | * @Get() |
||
| 324 | * |
||
| 325 | * @param ServerRequestInterface $request |
||
| 326 | * |
||
| 327 | * @return ResponseInterface |
||
| 328 | */ |
||
| 329 | public function confirmResetAccess(ServerRequestInterface $request) : ResponseInterface |
||
| 340 | |||
| 341 | protected function jsonTokenNotFoundResponse() : ResponseInterface |
||
| 347 | } |
||
| 348 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: