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 User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class User extends ActiveRecord implements yii\web\IdentityInterface |
||
| 46 | { |
||
| 47 | use CommonTrait; |
||
| 48 | |||
| 49 | /** Male gender */ |
||
| 50 | const MALE = 1; |
||
| 51 | /** Female gender */ |
||
| 52 | const FEMALE = 2; |
||
| 53 | |||
| 54 | /** Active user status */ |
||
| 55 | const STATUS_ACTIVE = 1; |
||
| 56 | /** Blocked user status */ |
||
| 57 | const STATUS_BLOCKED = 2; |
||
| 58 | /** User await email confirmation status */ |
||
| 59 | const STATUS_CONFIRM = 3; |
||
| 60 | /** User await access restore status */ |
||
| 61 | const STATUS_RESTORE = 4; |
||
| 62 | |||
| 63 | // TODO where store password between data enter and confirmation email? Password must be send with congratulation email |
||
| 64 | /** @var string password field on registration on restore */ |
||
| 65 | public $password; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | 15 | public static function tableName() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | * @return UserQuery the active query used by this AR class. |
||
| 78 | */ |
||
| 79 | 14 | public static function find() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Finds an identity by the given ID. |
||
| 87 | * @param string|integer $id the ID to be looked for |
||
| 88 | * @return IdentityInterface the identity object that matches the given ID. |
||
| 89 | * Null should be returned if such an identity cannot be found |
||
| 90 | * or the identity is not in an active state (disabled, deleted, etc.) |
||
| 91 | */ |
||
| 92 | 2 | public static function findIdentity($id) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Find user by token |
||
| 99 | * @param string $token for search |
||
| 100 | * @return null|static |
||
| 101 | */ |
||
| 102 | 3 | public static function findByToken($token) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Finds an identity by the given token. |
||
| 109 | * @param mixed $token the token to be looked for |
||
| 110 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
||
| 111 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
||
| 112 | * @return IdentityInterface the identity object that matches the given token. |
||
| 113 | * Null should be returned if such an identity cannot be found |
||
| 114 | * or the identity is not in an active state (disabled, deleted, etc.) |
||
| 115 | */ |
||
| 116 | 1 | public static function findIdentityByAccessToken($token, $type = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Confirm user registration |
||
| 126 | * @return bool return false if cannot confirm. |
||
| 127 | * If in errors list has key `error`, that user already confirmed |
||
| 128 | * If in errors list has key `token`, that confirm token was expired |
||
| 129 | */ |
||
| 130 | 3 | public function confirm() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Check that user was confirmed |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | 6 | public function isConfirmed() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Checks that the token was expired |
||
| 162 | * @param int $timeToExpire time |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 3 | public function isTokenExpired($timeToExpire) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Checks that the confirmation token was expired |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | 3 | public function isConfirmTokenExpired() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Checks that the restore token was expired |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | 1 | public function isRestoreTokenExpired() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | 10 | public function rules() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | 1 | public function attributeLabels() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Get user profile |
||
| 256 | * @return yii\db\ActiveQuery |
||
| 257 | */ |
||
| 258 | 1 | public function getProfile() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritdoc |
||
| 265 | */ |
||
| 266 | 10 | public function beforeSave($insert) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @inheritdoc |
||
| 279 | */ |
||
| 280 | 9 | public function afterSave($insert, $changedAttributes) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | 10 | public function transactions() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Returns an ID that can uniquely identify a user identity. |
||
| 304 | * @return string|integer an ID that uniquely identifies a user identity. |
||
| 305 | */ |
||
| 306 | 2 | public function getId() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Validates the given auth key. |
||
| 313 | * |
||
| 314 | * This is required if [[User::enableAutoLogin]] is enabled. |
||
| 315 | * @param string $authKey the given auth key |
||
| 316 | * @return boolean whether the given auth key is valid. |
||
| 317 | * @see getAuthKey() |
||
| 318 | */ |
||
| 319 | 1 | public function validateAuthKey($authKey) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Returns a key that can be used to check the validity of a given identity ID. |
||
| 327 | * |
||
| 328 | * The key should be unique for each individual user, and should be persistent |
||
| 329 | * so that it can be used to check the validity of the user identity. |
||
| 330 | * |
||
| 331 | * The space of such keys should be big enough to defeat potential identity attacks. |
||
| 332 | * |
||
| 333 | * This is required if [[User::enableAutoLogin]] is enabled. |
||
| 334 | * @return string a key that is used to check the validity of a given identity ID. |
||
| 335 | * @see validateAuthKey() |
||
| 336 | */ |
||
| 337 | 1 | public function getAuthKey() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Check active user |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | 2 | public function isActive() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * User creation |
||
| 353 | * For create the user you always must set attributes `email`, `password` and `name` |
||
| 354 | * @param bool $sendEmail whether to send email about registration |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | 1 | public function create($sendEmail = false) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * User registration |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 8 | public function register() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Password generator |
||
| 425 | * @return mixed |
||
| 426 | */ |
||
| 427 | 7 | public function generatePassword() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Generate special hash |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | 8 | public function generateToken() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Start password restore procedure |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | 1 | public function restore() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Check blocked user |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | 4 | public function isBlocked() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Block user |
||
| 480 | * @param bool $sendMail whether to send confirmation email about blocking. |
||
| 481 | * If null, use global setting Module::$enableBlockingEmail |
||
| 482 | * @return bool return false if user already blocked or not confirmed |
||
| 483 | */ |
||
| 484 | 1 | View Code Duplication | public function block($sendMail = null) |
| 502 | |||
| 503 | /** |
||
| 504 | * Unblock user |
||
| 505 | * @param bool $sendMail whether to send confirmation email about unblocking. |
||
| 506 | * If null, use global setting Module::$enableUnblockingEmail |
||
| 507 | * @return bool return false if user not blocked |
||
| 508 | */ |
||
| 509 | 1 | View Code Duplication | public function unblock($sendMail = null) |
| 527 | |||
| 528 | /** |
||
| 529 | * Change the user password |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | 1 | public function changePassword() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Set new password |
||
| 553 | * @param bool $sendEmail whether to send email about password change |
||
| 554 | * @throws yii\base\Exception |
||
| 555 | * @throws yii\base\InvalidConfigException |
||
| 556 | */ |
||
| 557 | 1 | public function newPassword($sendEmail = null) |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Check that user request restore |
||
| 580 | * @return bool |
||
| 581 | */ |
||
| 582 | 2 | public function isRestore() |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Get user status as text |
||
| 589 | * @param string $status status value. If not set, get from model |
||
| 590 | * @return null|string |
||
| 591 | */ |
||
| 592 | public function getStatusText($status = null) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get status list as array |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function statusesList() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Get user gender as text |
||
| 632 | * @param int $gender gender value. If not set, get from model |
||
| 633 | * @return null|string |
||
| 634 | */ |
||
| 635 | public function getGenderText($gender = null) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Get gender list as array |
||
| 655 | * @return array |
||
| 656 | */ |
||
| 657 | public function gendersList() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Resend confirmation message |
||
| 667 | */ |
||
| 668 | public function resend() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @inheritdoc |
||
| 680 | */ |
||
| 681 | 14 | public function behaviors() |
|
| 692 | } |
||
| 693 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.