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 |
||
18 | class PhpbbPassword implements IPassword { |
||
19 | const ITOA64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
||
20 | |||
21 | /** |
||
22 | * Check for a correct password. |
||
23 | * |
||
24 | * @param string $password The password in plain text. |
||
25 | * @param string $hash The stored password hash. |
||
26 | * @return bool Returns true if the password is correct, false if not. |
||
27 | */ |
||
28 | 3 | public function verify($password, $hash) { |
|
35 | |||
36 | /** |
||
37 | * The crypt function/replacement. |
||
38 | * |
||
39 | * @param string $password The password to encrypt. |
||
40 | * @param string $setting The hash prefix setting. It should start with $H$. |
||
41 | * @return string The encypted password. |
||
42 | */ |
||
43 | 3 | private function cryptPrivate($password, $setting) { |
|
90 | |||
91 | /** |
||
92 | * Encode hash. |
||
93 | * |
||
94 | * @param string $input The input to encode. |
||
95 | * @param int $count The number of characters to encode. |
||
96 | * @return string The encoded string. |
||
97 | */ |
||
98 | 3 | protected function encode64($input, $count) { |
|
132 | |||
133 | /** |
||
134 | * Hashes a plaintext password. |
||
135 | * |
||
136 | * @param string $password The password to hash. |
||
137 | * @return string Returns the hashed password. |
||
138 | */ |
||
139 | 3 | public function hash($password) { |
|
149 | |||
150 | /** |
||
151 | * Generate a password salt based on the given input string. |
||
152 | * |
||
153 | * @param string $input The input string to generate the salt from. |
||
154 | * @return string Returns the password salt prefixed with `$P$`. |
||
155 | */ |
||
156 | 3 | private function gensaltPrivate($input) { |
|
165 | |||
166 | /** |
||
167 | * Checks if a given password hash needs to be re-hashed to to a stronger algorithm. |
||
168 | * |
||
169 | * @param string $hash The hash to check. |
||
170 | * @return bool Returns `true` |
||
171 | */ |
||
172 | 1 | public function needsRehash($hash) { |
|
175 | } |
||
176 |
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.