Complex classes like PasswordTrait 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 PasswordTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 37 | trait PasswordTrait | ||
| 38 | { | ||
| 39 | |||
| 40 | public static $eventAfterSetPassword = "afterSetPassword"; | ||
| 41 | public static $eventBeforeValidatePassword = "beforeValidatePassword"; | ||
| 42 | public static $eventValidatePasswordSucceeded = "validatePasswordSucceeded"; | ||
| 43 | public static $eventValidatePasswordFailed = "validatePasswordFailed"; | ||
| 44 | public static $eventBeforeResetPassword = "beforeResetPassword"; | ||
| 45 | public static $eventAfterResetPassword = "afterResetPassword"; | ||
| 46 | public static $eventResetPasswordFailed = "resetPasswordFailed"; | ||
| 47 | public static $eventNewPasswordAppliedFor = "newPasswordAppliedFor"; | ||
| 48 | public static $eventPasswordResetTokenGenerated = "passwordResetTokenGenerated"; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * @var string The name of attribute used for storing password hash. | ||
| 52 | * We strongly recommend you not to change `pass_hash` property directly, | ||
| 53 | * please use setPassword() magic property instead. | ||
| 54 | */ | ||
| 55 | public $passwordHashAttribute = 'pass_hash'; | ||
| 56 | |||
| 57 | /** | ||
| 58 | * @var string The name of attribute used for storing password reset token. | ||
| 59 | * If you do not want to provide password reset feature, please set `false`. | ||
| 60 | */ | ||
| 61 | public $passwordResetTokenAttribute = 'password_reset_token'; | ||
| 62 | |||
| 63 | /** | ||
| 64 | * @var integer Cost parameter used by the Blowfish hash algorithm. | ||
| 65 | */ | ||
| 66 | public $passwordCost = 13; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @var integer if $passwordHashStrategy equals 'crypt', this value statically | ||
| 70 | * equals 60. | ||
| 71 | */ | ||
| 72 | public $passwordHashAttributeLength = 60; | ||
| 73 | private $passwordHashRules = []; | ||
| 74 | private $passwordResetTokenRules = []; | ||
| 75 | |||
| 76 | /** | ||
| 77 | * Return the empty password specialty. | ||
| 78 | * NOTE: PLEASE OVERRIDE THIS METHOD TO SPECIFY YOUR OWN EMPTY PASSWORD SPECIALTY. | ||
| 79 | * - The length of specialty should be greater than 18. | ||
| 80 | * - Uppercase and lowercase letters, punctuation marks, numbers, and underscores are required. | ||
| 81 | * @return string The string regarded as empty password. | ||
| 82 | */ | ||
| 83 | 7 | protected function getEmptyPasswordSpecialty() | |
| 87 | |||
| 88 | protected $_password; | ||
| 89 | |||
| 90 | /** | ||
| 91 | * Get rules of password hash. | ||
| 92 | * @return array password hash rules. | ||
| 93 | */ | ||
| 94 | 288 | public function getPasswordHashRules() | |
| 106 | |||
| 107 | /** | ||
| 108 | * Set rules of password hash. | ||
| 109 | * @param array $rules password hash rules. | ||
| 110 | */ | ||
| 111 | 2 | public function setPasswordHashRules($rules) | |
| 117 | |||
| 118 | /** | ||
| 119 | * Get the rules associated with password reset token attribute. | ||
| 120 | * If password reset feature is not enabled, the empty array will be given. | ||
| 121 | * @return mixed | ||
| 122 | */ | ||
| 123 | 288 | public function getPasswordResetTokenRules() | |
| 136 | |||
| 137 | /** | ||
| 138 | * Set the rules associated with password reset token attribute. | ||
| 139 | * @param mixed $rules | ||
| 140 | */ | ||
| 141 | 3 | public function setPasswordResetTokenRules($rules) | |
| 147 | |||
| 148 | /** | ||
| 149 | * Generates a secure hash from a password and a random salt. | ||
| 150 | * | ||
| 151 | * The generated hash can be stored in database. | ||
| 152 | * Later when a password needs to be validated, the hash can be fetched and passed | ||
| 153 | * to [[validatePassword()]]. For example, | ||
| 154 | * | ||
| 155 | * ~~~ | ||
| 156 | * // generates the hash (usually done during user registration or when the password is changed) | ||
| 157 | * $hash = Yii::$app->getSecurity()->generatePasswordHash($password); | ||
| 158 | * // ...save $hash in database... | ||
| 159 | * | ||
| 160 | * // during login, validate if the password entered is correct using $hash fetched from database | ||
| 161 |      * if (Yii::$app->getSecurity()->validatePassword($password, $hash) { | ||
| 162 | * // password is good | ||
| 163 |      * } else { | ||
| 164 | * // password is bad | ||
| 165 | * } | ||
| 166 | * ~~~ | ||
| 167 | * | ||
| 168 | * @param string $password The password to be hashed. | ||
| 169 | * @return string The password hash string. When [[passwordHashStrategy]] is set to 'crypt', | ||
| 170 | * the output is always 60 ASCII characters, when set to 'password_hash' the output length | ||
| 171 | * might increase in future versions of PHP (http://php.net/manual/en/function.password-hash.php) | ||
| 172 | */ | ||
| 173 | 191 | public function generatePasswordHash($password) | |
| 177 | |||
| 178 | /** | ||
| 179 | * Verifies a password against a hash. | ||
| 180 | * @param string $password The password to verify. | ||
| 181 | * @return boolean whether the password is correct. | ||
| 182 | */ | ||
| 183 | 9 | public function validatePassword($password) | |
| 194 | |||
| 195 | /** | ||
| 196 | * Set new password. | ||
| 197 | * If $password is empty, the specilty which represents the empty will be taken. | ||
| 198 | * Finally, it will trigger `static::$eventAfterSetPassword` event. | ||
| 199 | * @param string $password the new password to be set. | ||
| 200 | */ | ||
| 201 | 191 | public function setPassword($password = null) | |
| 211 | |||
| 212 | /** | ||
| 213 | * Set empty password. | ||
| 214 | */ | ||
| 215 | 4 | public function setEmptyPassword() | |
| 219 | |||
| 220 | /** | ||
| 221 | * Check whether password is empty. | ||
| 222 | * @return boolean | ||
| 223 | */ | ||
| 224 | 4 | public function getIsEmptyPassword() | |
| 230 | |||
| 231 | /** | ||
| 232 | * Apply for new password. | ||
| 233 | * If this model is new one, false will be given, and no events will be triggered. | ||
| 234 | * If password reset feature is not enabled, `$eventNewPasswordAppliedFor` | ||
| 235 | * will be triggered and return true directly. | ||
| 236 | * Otherwise, the new password reset token will be regenerated and saved. Then | ||
| 237 | * trigger the `$eventNewPasswordAppliedFor` and | ||
| 238 | * `$eventPasswordResetTokenGenerated` events and return true. | ||
| 239 | * @return boolean | ||
| 240 | */ | ||
| 241 | 1 | public function applyForNewPassword() | |
| 260 | |||
| 261 | /** | ||
| 262 | * Reset password with password reset token. | ||
| 263 | * It will validate password reset token, before reseting password. | ||
| 264 | * @param string $password New password to be reset. | ||
| 265 | * @param string $token Password reset token. | ||
| 266 | * @return boolean whether reset password successfully or not. | ||
| 267 | */ | ||
| 268 | 1 | public function resetPassword($password, $token) | |
| 285 | |||
| 286 | /** | ||
| 287 | * Generate password reset token. | ||
| 288 | * @return string | ||
| 289 | */ | ||
| 290 | 1 | public static function generatePasswordResetToken() | |
| 294 | |||
| 295 | /** | ||
| 296 | * The event triggered after new password set. | ||
| 297 | * The auth key and access token should be regenerated if new password has applied. | ||
| 298 | * @param ModelEvent $event | ||
| 299 | */ | ||
| 300 | 9 | public function onAfterSetNewPassword($event) | |
| 305 | |||
| 306 | /** | ||
| 307 | * Validate whether the $token is the valid password reset token. | ||
| 308 | * If password reset feature is not enabled, true will be given. | ||
| 309 | * @param string $token | ||
| 310 | * @return boolean whether the token is correct. | ||
| 311 | */ | ||
| 312 | 1 | protected function validatePasswordResetToken($token) | |
| 319 | |||
| 320 | /** | ||
| 321 | * Initialize password reset token attribute. | ||
| 322 | * @param ModelEvent $event | ||
| 323 | */ | ||
| 324 | 300 | public function onInitPasswordResetToken($event) | |
| 332 | |||
| 333 | /** | ||
| 334 | * Set password reset token. | ||
| 335 | * @param string $token | ||
| 336 | * @return string | ||
| 337 | */ | ||
| 338 | 300 | public function setPasswordResetToken($token = '') | |
| 342 | |||
| 343 | /** | ||
| 344 | * Get password reset token. | ||
| 345 | * @return string | ||
| 346 | */ | ||
| 347 | 1 | public function getPasswordResetToken() | |
| 351 | } | ||
| 352 | 
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.