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 |
||
| 17 | class Identification |
||
| 18 | { |
||
| 19 | |||
| 20 | const DEFAULT_COOKIE_LIFESPAN = 14400; // 4 hours |
||
| 21 | |||
| 22 | private $mapperFactory; |
||
| 23 | private $logger; |
||
| 24 | |||
| 25 | private $cookieLifespan; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param Palladium\Contract\CanCreateMapper $mapperFactory Factory for creating persistence layer structures |
||
| 29 | * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger |
||
| 30 | * @param int $cookieLifespan Lifespan of the authentication cookie in seconds |
||
| 31 | */ |
||
| 32 | 10 | public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger, $cookieLifespan = self::DEFAULT_COOKIE_LIFESPAN) |
|
| 33 | { |
||
| 34 | 10 | $this->mapperFactory = $mapperFactory; |
|
| 35 | 10 | $this->logger = $logger; |
|
| 36 | 10 | $this->cookieLifespan = $cookieLifespan; |
|
| 37 | 10 | } |
|
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $password |
||
| 42 | * |
||
| 43 | * @return Palladium\Entity\CookieIdentity |
||
| 44 | */ |
||
| 45 | 2 | public function loginWithPassword(Entity\EmailIdentity $identity, $password) |
|
| 46 | { |
||
| 47 | 2 | if ($identity->matchPassword($password) === false) { |
|
| 48 | 1 | $this->logWrongPasswordNotice($identity, [ |
|
| 49 | 1 | 'email' => $identity->getEmailAddress(), |
|
| 50 | 1 | 'key' => md5($password), |
|
| 51 | ]); |
||
| 52 | |||
| 53 | 1 | throw new PasswordMismatch; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | $this->registerUsageOfIdentity($identity); |
|
| 57 | 1 | $cookie = $this->createCookieIdentity($identity); |
|
| 58 | |||
| 59 | 1 | $this->logger->info('login successful', [ |
|
| 60 | 'input' => [ |
||
| 61 | 1 | 'email' => $identity->getEmailAddress(), |
|
| 62 | ], |
||
| 63 | 'user' => [ |
||
| 64 | 1 | 'account' => $identity->getAccountId(), |
|
| 65 | 1 | 'identity' => $identity->getId(), |
|
| 66 | ], |
||
| 67 | ]); |
||
| 68 | |||
| 69 | 1 | return $cookie; |
|
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | 1 | private function registerUsageOfIdentity(Entity\Identity $identity) |
|
| 80 | |||
| 81 | |||
| 82 | 1 | private function createCookieIdentity(Entity\EmailIdentity $identity) |
|
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string @key |
||
| 111 | * |
||
| 112 | * @throws \Palladium\Exception\CompromisedCookie if key does not match |
||
| 113 | * @throws \Palladium\Exception\IdentityExpired if cookie is too old |
||
| 114 | * |
||
| 115 | * @return Palladium\Entity\CookieIdentity |
||
| 116 | */ |
||
| 117 | 3 | public function loginWithCookie(Entity\CookieIdentity $identity, $key) |
|
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $key |
||
| 137 | */ |
||
| 138 | 1 | public function logout(Entity\CookieIdentity $identity, $key) |
|
| 150 | |||
| 151 | |||
| 152 | 4 | View Code Duplication | private function checkCookieExpireTime(Entity\CookieIdentity $identity) |
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Verify that the cookie based identity matches the key and, |
||
| 169 | * if verification is failed, disable this given identity |
||
| 170 | * |
||
| 171 | * @param string $key |
||
| 172 | * @throws \Palladium\Exception\CompromisedCookie if key does not match |
||
| 173 | */ |
||
| 174 | 3 | View Code Duplication | private function checkCookieKey(Entity\CookieIdentity $identity, $key) |
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 2 | private function assembleCookieLogDetails(Entity\CookieIdentity $identity) |
|
| 208 | |||
| 209 | |||
| 210 | 1 | public function discardIdentityCollection(Entity\IdentityCollection $list) |
|
| 219 | |||
| 220 | |||
| 221 | 1 | public function blockIdentity(Entity\Identity $identity) |
|
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * @codeCoverageIgnore |
||
| 232 | */ |
||
| 233 | public function deleteIdentity(Entity\Identity $identity) |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $oldPassword |
||
| 242 | * @param string $newPassword |
||
| 243 | */ |
||
| 244 | 2 | public function changePassword(Entity\EmailIdentity $identity, $oldPassword, $newPassword) |
|
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * @param array $input |
||
| 267 | */ |
||
| 268 | 2 | private function logWrongPasswordNotice(Entity\EmailIdentity $identity, $input) |
|
| 278 | |||
| 279 | |||
| 280 | 3 | private function logExpectedBehaviour(Entity\Identity $identity, $message) |
|
| 289 | |||
| 290 | |||
| 291 | public function useOneTimeIdentity(Entity\OneTimeIdentity $identity, $key) |
||
| 308 | } |
||
| 309 |
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.