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 |
||
13 | class XenforoPassword implements IPassword { |
||
14 | |||
15 | /** |
||
16 | * @var string The name of the hashing function to use. |
||
17 | */ |
||
18 | protected $hashFunction; |
||
19 | |||
20 | /** |
||
21 | * Initialize an instance of this class. |
||
22 | * |
||
23 | * @param string $hashFunction The name of the hash function to use. |
||
24 | * This is an function name that can be passed to {@link hash()}. |
||
25 | * @see hash() |
||
26 | */ |
||
27 | public function __construct($hashFunction = '') { |
||
28 | if (!$hashFunction) { |
||
29 | $hashFunction = 'sha256'; |
||
30 | } |
||
31 | $this->hashFunction = $hashFunction; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 3 | public function hash($password) { |
|
47 | |||
48 | /** |
||
49 | * Hashes a password with a given salt. |
||
50 | * |
||
51 | * @param string $password The password to hash. |
||
52 | * @param string $salt The password salt. |
||
53 | * @param string $function The hashing function to use. |
||
54 | * @return string Returns the password hash. |
||
55 | */ |
||
56 | 4 | protected function hashRaw($password, $salt, $function = null) { |
|
57 | 4 | if (!$function) { |
|
|
|||
58 | 1 | $function = $this->hashFunction; |
|
59 | } |
||
60 | |||
61 | 4 | $calc_hash = hash($function, hash($function, $password).$salt); |
|
62 | |||
63 | 4 | return $calc_hash; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 1 | View Code Duplication | public function needsRehash($hash) { |
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 3 | public function verify($password, $hash) { |
|
91 | |||
92 | /** |
||
93 | * Split the hash into its calculated hash and salt. |
||
94 | * |
||
95 | * @param string $hash The hash to split. |
||
96 | * @return array An array in the form [$hash, $hashFunc, $salt]. |
||
97 | */ |
||
98 | 4 | protected function splitHash($hash) { |
|
121 | } |
||
122 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: