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 |
||
| 22 | class ForgotYourPasswordController |
||
| 23 | { |
||
| 24 | private $baseUrl = 'forgot'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The logger used by this controller. |
||
| 28 | * |
||
| 29 | * @var LoggerInterface |
||
| 30 | */ |
||
| 31 | private $logger; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The template used by this controller. |
||
| 35 | * |
||
| 36 | * @var TemplateInterface |
||
| 37 | */ |
||
| 38 | private $template; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The main content block of the page. |
||
| 42 | * |
||
| 43 | * @var HtmlBlock |
||
| 44 | */ |
||
| 45 | private $content; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The Twig environment (used to render Twig templates). |
||
| 49 | * |
||
| 50 | * @var Twig_Environment |
||
| 51 | */ |
||
| 52 | private $twig; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The service that will actually check the tokens. |
||
| 56 | * |
||
| 57 | * @var ForgotYourPasswordDao |
||
| 58 | */ |
||
| 59 | private $forgotYourPasswordService; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The userservice to log people when a token is found. |
||
| 63 | * |
||
| 64 | * @var UserService |
||
| 65 | */ |
||
| 66 | private $userService; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * 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. |
||
| 70 | * 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. |
||
| 71 | * |
||
| 72 | * At the same time, it decreases usability. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | private $noLeak = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var PasswordStrengthCheck |
||
| 80 | */ |
||
| 81 | private $passwordStrengthCheck; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The URL to continue to when password has been changed. |
||
| 85 | * Relative to the root path of the application. |
||
| 86 | * Defaults to '/'. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $continueUrl = '/'; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Controller's constructor. |
||
| 94 | * |
||
| 95 | * @param LoggerInterface $logger The logger |
||
| 96 | * @param TemplateInterface $template The template used by this controller |
||
| 97 | * @param HtmlBlock $content The main content block of the page |
||
| 98 | * @param Twig_Environment $twig The Twig environment (used to render Twig templates) |
||
| 99 | */ |
||
| 100 | public function __construct(LoggerInterface $logger, TemplateInterface $template, HtmlBlock $content, Twig_Environment $twig, ForgotYourPasswordService $forgotYourPasswordService, UserService $userService, PasswordStrengthCheck $passwordStrengthCheck) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * 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. |
||
| 113 | * 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. |
||
| 114 | * |
||
| 115 | * At the same time, it decreases usability. |
||
| 116 | * |
||
| 117 | * @param bool $noLeak |
||
| 118 | */ |
||
| 119 | public function setNoLeak(bool $noLeak) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The URL to continue to when password has been changed. |
||
| 126 | * Relative to the root path of the application. |
||
| 127 | * Defaults to '/'. |
||
| 128 | * |
||
| 129 | * @param string $continueUrl |
||
| 130 | */ |
||
| 131 | public function setContinueUrl(string $continueUrl) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Displays the screen to enter the email. |
||
| 138 | * |
||
| 139 | * @URL("{$this->baseUrl}/password") |
||
| 140 | * @Get |
||
| 141 | * |
||
| 142 | * @param string|null $email |
||
| 143 | * |
||
| 144 | * @return ResponseInterface |
||
| 145 | */ |
||
| 146 | public function index(string $email = null) : ResponseInterface |
||
| 150 | |||
| 151 | private function displayMainForm(string $email = null, bool $notFoundMessage = false) : ResponseInterface |
||
| 167 | |||
| 168 | private function jsonNotFoundResponse() : ResponseInterface |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Displays the screen to enter the email. |
||
| 177 | * |
||
| 178 | * @URL("{$this->baseUrl}/password") |
||
| 179 | * @Post |
||
| 180 | * |
||
| 181 | * @param string $email |
||
| 182 | * |
||
| 183 | * @return ResponseInterface |
||
| 184 | */ |
||
| 185 | public function submit(string $email, ServerRequestInterface $request) : ResponseInterface |
||
| 209 | |||
| 210 | private function isJson(ServerRequestInterface $request) : bool |
||
| 214 | |||
| 215 | private function jsonSentResponse() : ResponseInterface |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @URL("{$this->baseUrl}/mail_sent") |
||
| 225 | * @Get() |
||
| 226 | * |
||
| 227 | * @param string $email |
||
| 228 | * |
||
| 229 | * @return ResponseInterface |
||
| 230 | */ |
||
| 231 | public function displayMailSentScreen(string $email) : ResponseInterface |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @URL("{$this->baseUrl}/reset") |
||
| 240 | * @Get() |
||
| 241 | * |
||
| 242 | * @param string $token |
||
| 243 | * |
||
| 244 | * @return ResponseInterface |
||
| 245 | */ |
||
| 246 | public function displayResetAccessPage(string $token) : ResponseInterface |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @URL("{$this->baseUrl}/reset") |
||
| 263 | * @Post() |
||
| 264 | * |
||
| 265 | * @param string $token |
||
| 266 | * @param ServerRequestInterface $request |
||
| 267 | * |
||
| 268 | * @return ResponseInterface |
||
| 269 | */ |
||
| 270 | public function resetAccess(string $token, string $password, string $confirmpassword, ServerRequestInterface $request) : ResponseInterface |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @URL("{$this->baseUrl}/reset_confirm") |
||
| 324 | * @Get() |
||
| 325 | * |
||
| 326 | * @param ServerRequestInterface $request |
||
| 327 | * |
||
| 328 | * @return ResponseInterface |
||
| 329 | */ |
||
| 330 | public function confirmResetAccess(ServerRequestInterface $request) : ResponseInterface |
||
| 341 | |||
| 342 | protected function jsonTokenNotFoundResponse() : ResponseInterface |
||
| 348 | } |
||
| 349 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: