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 |
||
9 | abstract class ResetPassword extends \yii\base\Model |
||
10 | { |
||
11 | const SCENARIO_INIT = 'init'; |
||
12 | const SCENARIO_RESET = 'reset'; |
||
13 | const SCENARIO_RESET_AUTHENTICATED = 'reset_authenticated'; |
||
14 | |||
15 | // The expiration time of the token |
||
16 | const EXPIRY_TIME = '+4 hours'; |
||
17 | |||
18 | /** |
||
19 | * The email |
||
20 | * @var string $email |
||
21 | */ |
||
22 | public $email; |
||
23 | |||
24 | /** |
||
25 | * The reset token |
||
26 | * @var string $reset_token |
||
27 | */ |
||
28 | public $reset_token; |
||
29 | |||
30 | /** |
||
31 | * The user ID for credentialless password resets |
||
32 | * @var int $user_id |
||
33 | */ |
||
34 | public $user_id; |
||
35 | |||
36 | /** |
||
37 | * The old password |
||
38 | * @var string $old_password |
||
39 | */ |
||
40 | public $old_password; |
||
41 | |||
42 | /** |
||
43 | * The new password |
||
44 | * @var string $password |
||
45 | */ |
||
46 | public $password; |
||
47 | |||
48 | /** |
||
49 | * The new password (again) |
||
50 | * @var string $password_verify |
||
51 | */ |
||
52 | public $password_verify; |
||
53 | |||
54 | /** |
||
55 | * The OTP code (optional) |
||
56 | * @var string $otp |
||
57 | */ |
||
58 | public $otp; |
||
59 | |||
60 | /** |
||
61 | * The user associated to the email |
||
62 | * @var User $user |
||
63 | */ |
||
64 | protected $user = null; |
||
65 | |||
66 | /** |
||
67 | * Validation rules |
||
68 | * @return array |
||
69 | */ |
||
70 | public function rules() |
||
93 | |||
94 | /** |
||
95 | * Validates the users OTP code, if they provided |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | View Code Duplication | public function validateOTP($attributes, $params) |
|
109 | |||
110 | /** |
||
111 | * Validates the users old password |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | View Code Duplication | public function validateOldPassword($attributes, $params) |
|
125 | |||
126 | /** |
||
127 | * Validates the authenticated users state |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | View Code Duplication | public function validateAuthUser($attributes, $params) |
|
139 | |||
140 | /** |
||
141 | * Validates the users email |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | View Code Duplication | public function validateUser($attributes, $params) |
|
153 | |||
154 | /** |
||
155 | * Reset token validator |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | public function validateResetToken($attributes, $params) |
||
182 | |||
183 | /** |
||
184 | * Sets the user object |
||
185 | * @param User $user |
||
186 | */ |
||
187 | public function setUser($user) |
||
192 | |||
193 | /** |
||
194 | * Helper method to get the current user |
||
195 | * @return User |
||
196 | */ |
||
197 | public function getUser() |
||
211 | |||
212 | /** |
||
213 | * Changes the password for the user |
||
214 | * @return boolean |
||
215 | */ |
||
216 | public function reset() |
||
280 | } |
||
281 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.