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 Registration |
||
| 18 | { |
||
| 19 | |||
| 20 | const DEFAULT_TOKEN_LIFESPAN = 28800; // 8 hours |
||
| 21 | const DEFAULT_NONCE_LIFESPAN = 7200; // 2 hours |
||
| 22 | const DEFAULT_HASH_COST = 12; |
||
| 23 | |||
| 24 | private $repository; |
||
| 25 | private $logger; |
||
| 26 | private $hashCost; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Palladium\Repository\Identity $repository Repository for abstracting persistence layer structures |
||
| 30 | * @param Psr\Log\LoggerInterface $logger PSR-3 compatible logger |
||
| 31 | * @param int $hashCost Optional value for setting the cost of hashing algorythm (default: 12) |
||
| 32 | */ |
||
| 33 | 5 | View Code Duplication | public function __construct(Repository $repository, DataMapper $accountMapper, LoggerInterface $logger, $hashCost = self::DEFAULT_HASH_COST) |
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $identifier |
||
| 44 | * @param string $password |
||
| 45 | * @param int $tokenLifespan |
||
| 46 | * |
||
| 47 | * @return Palladium\Entity\StandardIdentity |
||
| 48 | */ |
||
| 49 | 2 | public function createStandardIdentity(string $identifier, string $password, $tokenLifespan = self::DEFAULT_TOKEN_LIFESPAN) |
|
| 73 | |||
| 74 | |||
| 75 | 1 | public function createNonceIdentity($accountId, $identityLifespan = self::DEFAULT_NONCE_LIFESPAN) |
|
| 96 | |||
| 97 | |||
| 98 | 2 | private function prepareNewIdentity(Entity\StandardIdentity $identity) |
|
| 104 | |||
| 105 | |||
| 106 | 1 | public function bindAccountToIdentity(int $accountId, Entity\Identity $identity) |
|
| 118 | |||
| 119 | |||
| 120 | 1 | public function verifyStandardIdentity(Entity\StandardIdentity $identity) |
|
| 137 | } |
||
| 138 |
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.