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:
Complex classes like PhpassPassword often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhpassPassword, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class PhpassPassword implements IPassword { |
||
17 | const HASH_PHPASS = 0x00; |
||
18 | const HASH_BLOWFISH = 0x01; |
||
19 | const HASH_EXTDES = 0x02; |
||
20 | const HASH_BEST = 0x03; |
||
21 | |||
22 | protected $itoa64; |
||
23 | protected $iteration_count_log2; |
||
24 | protected $portable; |
||
25 | protected $random_state; |
||
26 | |||
27 | protected $hashMethod; |
||
28 | |||
29 | /** |
||
30 | * Initializes an instance of the of the {@link PhpPass} class. |
||
31 | * |
||
32 | * @param int $hashMethod The hash method to use when hashing passwords. |
||
33 | * @param int $iteration_count_log2 The number of times to iterate when generating the passwords. |
||
34 | */ |
||
35 | 6 | public function __construct($hashMethod = PhpassPassword::HASH_PHPASS, $iteration_count_log2 = 8) { |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 1 | public function needsRehash($hash) { |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 12 | public function hash($password) { |
|
102 | |||
103 | /** |
||
104 | * Get a string of random bytes. |
||
105 | * |
||
106 | * @param int $count The number of bytes to get. |
||
107 | * @return string Returns a string of the generated random bytes. |
||
108 | */ |
||
109 | 12 | protected function getRandomBytes($count) { |
|
131 | |||
132 | /** |
||
133 | * Generate a password salt appropriate for blowfish. |
||
134 | * |
||
135 | * @param string $input The random input to generate the salt from. |
||
136 | * @return string The generated salt. |
||
137 | */ |
||
138 | 7 | protected function gensaltBlowfish($input) { |
|
177 | |||
178 | /** |
||
179 | * Generate a password salt based on the input. |
||
180 | * |
||
181 | * @param string $input The string to generate the salt from. |
||
182 | * @return string The generated salt. |
||
183 | */ |
||
184 | 2 | private function gensaltExtended($input) { |
|
200 | |||
201 | /** |
||
202 | * A custom base64 encoding function. |
||
203 | * |
||
204 | * @param string $input The string to encode. |
||
205 | * @param int $count The number of characters to encode. |
||
206 | * @return string Returns the encoded string. |
||
207 | */ |
||
208 | 6 | protected function encode64($input, $count) { |
|
233 | |||
234 | /** |
||
235 | * A portable version of a crypt-like algorithm. |
||
236 | * |
||
237 | * @param string $password The plaintext password to encrypt. |
||
238 | * @param string $setting The hash prefix that defines what kind of algorithm to use. |
||
239 | * @return string Returns the encrypted string. |
||
240 | */ |
||
241 | 11 | private function cryptPrivate($password, $setting) { |
|
288 | |||
289 | /** |
||
290 | * Generate a password salt based on the given input string. |
||
291 | * |
||
292 | * @param string $input The input string to generate the salt from. |
||
293 | * @return string Returns the password salt prefixed with `$P$`. |
||
294 | */ |
||
295 | 5 | private function gensaltPrivate($input) { |
|
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | 11 | public function verify($password, $hash) { |
|
317 | |||
318 | /** |
||
319 | * Get the current hash method. |
||
320 | * |
||
321 | * @return int Returns the current hash method. |
||
322 | */ |
||
323 | public function getHashMethod() { |
||
326 | |||
327 | /** |
||
328 | * Set the current hash method. |
||
329 | * |
||
330 | * @param int $hashMethod The new hash mathod. |
||
331 | * @return PhpassPassword Returns $this for fluent calls. |
||
332 | */ |
||
333 | 1 | public function setHashMethod($hashMethod) { |
|
337 | } |
||
338 |