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 |
||
| 5 | class OneTimeIdentity extends Identity |
||
| 6 | { |
||
| 7 | |||
| 8 | const HASH_ALGO = PASSWORD_BCRYPT; |
||
| 9 | const HASH_COST = 12; |
||
| 10 | |||
| 11 | const NONCE_SIZE = 16; |
||
| 12 | const KEY_SIZE = 32; |
||
| 13 | |||
| 14 | private $nonce; |
||
| 15 | private $key; |
||
| 16 | private $hash; |
||
| 17 | |||
| 18 | protected $type = Identity::TYPE_ONETIME; |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * @codeCoverageIgnore |
||
| 23 | */ |
||
| 24 | public function setNonce($nonce) |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @codeCoverageIgnore |
||
| 32 | */ |
||
| 33 | public function getNonce() |
||
| 37 | |||
| 38 | |||
| 39 | 1 | public function generateNewNonce() |
|
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * @codeCoverageIgnore |
||
| 47 | */ |
||
| 48 | public function getFingerprint() |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Sets a new key and resets the hash. |
||
| 56 | */ |
||
| 57 | 1 | public function generateNewKey() |
|
| 62 | |||
| 63 | |||
| 64 | 3 | private function makeHash($key) |
|
| 70 | |||
| 71 | |||
| 72 | 1 | public function matchKey($key) |
|
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Assignes a new identification key and resets a the hash. |
||
| 80 | * |
||
| 81 | * @param string $key |
||
| 82 | */ |
||
| 83 | 4 | View Code Duplication | public function setKey($key) |
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @codeCoverageIgnore |
||
| 99 | */ |
||
| 100 | public function getKey() |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * @codeCoverageIgnore |
||
| 108 | */ |
||
| 109 | public function setHash($hash) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @codeCoverageIgnore |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getHash() |
||
| 122 | } |
||
| 123 |
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.