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 |
||
41 | class User extends ActiveRecord implements yii\web\IdentityInterface |
||
42 | { |
||
43 | use CommonTrait; |
||
44 | |||
45 | /** Male gender */ |
||
46 | const MALE = 1; |
||
47 | /** Female gender */ |
||
48 | const FEMALE = 2; |
||
49 | |||
50 | /** Active user status */ |
||
51 | const STATUS_ACTIVE = 1; |
||
52 | /** Blocked user status */ |
||
53 | const STATUS_BLOCKED = 2; |
||
54 | /** User await email confirmation status */ |
||
55 | const STATUS_CONFIRM = 3; |
||
56 | /** User await access restore status */ |
||
57 | const STATUS_RESTORE = 4; |
||
58 | |||
59 | // TODO where store password between data enter and confirmation email? Password must be send with congratulation email |
||
60 | /** @var string password field on registration on restore */ |
||
61 | public $password; |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 15 | public static function tableName() |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | * @return UserQuery the active query used by this AR class. |
||
74 | */ |
||
75 | 14 | public static function find() |
|
80 | |||
81 | /** |
||
82 | * Finds an identity by the given ID. |
||
83 | * @param string|integer $id the ID to be looked for |
||
84 | * @return IdentityInterface the identity object that matches the given ID. |
||
85 | * Null should be returned if such an identity cannot be found |
||
86 | * or the identity is not in an active state (disabled, deleted, etc.) |
||
87 | */ |
||
88 | 2 | public static function findIdentity($id) |
|
92 | |||
93 | /** |
||
94 | * Find user by token |
||
95 | * @param string $token for search |
||
96 | * @return null|static |
||
97 | */ |
||
98 | 3 | public static function findByToken($token) |
|
102 | |||
103 | /** |
||
104 | * Finds an identity by the given token. |
||
105 | * @param mixed $token the token to be looked for |
||
106 | * @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
||
107 | * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
||
108 | * @return IdentityInterface the identity object that matches the given token. |
||
109 | * Null should be returned if such an identity cannot be found |
||
110 | * or the identity is not in an active state (disabled, deleted, etc.) |
||
111 | */ |
||
112 | 1 | public static function findIdentityByAccessToken($token, $type = null) |
|
119 | |||
120 | /** |
||
121 | * Confirm user registration |
||
122 | * @return bool return false if cannot confirm. |
||
123 | * If in errors list has key `error`, that user already confirmed |
||
124 | * If in errors list has key `token`, that confirm token was expired |
||
125 | */ |
||
126 | 3 | public function confirm() |
|
146 | |||
147 | /** |
||
148 | * Check that user was confirmed |
||
149 | * @return bool |
||
150 | */ |
||
151 | 6 | public function isConfirmed() |
|
155 | |||
156 | /** |
||
157 | * Checks that the token was expired |
||
158 | * @param int $timeToExpire time |
||
159 | * @return bool |
||
160 | */ |
||
161 | 3 | public function isTokenExpired($timeToExpire) |
|
165 | |||
166 | /** |
||
167 | * Checks that the confirmation token was expired |
||
168 | * @return bool |
||
169 | */ |
||
170 | 3 | public function isConfirmTokenExpired() |
|
174 | |||
175 | /** |
||
176 | * Checks that the restore token was expired |
||
177 | * @return bool |
||
178 | */ |
||
179 | 1 | public function isRestoreTokenExpired() |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | 10 | public function rules() |
|
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | 1 | public function attributeLabels() |
|
249 | |||
250 | /** |
||
251 | * Get user profile |
||
252 | * @return yii\db\ActiveQuery |
||
253 | */ |
||
254 | 1 | public function getProfile() |
|
258 | |||
259 | /** |
||
260 | * @inheritdoc |
||
261 | */ |
||
262 | 10 | public function beforeSave($insert) |
|
272 | |||
273 | /** |
||
274 | * @inheritdoc |
||
275 | */ |
||
276 | 9 | public function afterSave($insert, $changedAttributes) |
|
287 | |||
288 | /** |
||
289 | * @inheritdoc |
||
290 | */ |
||
291 | 10 | public function transactions() |
|
297 | |||
298 | /** |
||
299 | * Returns an ID that can uniquely identify a user identity. |
||
300 | * @return string|integer an ID that uniquely identifies a user identity. |
||
301 | */ |
||
302 | 2 | public function getId() |
|
306 | |||
307 | /** |
||
308 | * Validates the given auth key. |
||
309 | * |
||
310 | * This is required if [[User::enableAutoLogin]] is enabled. |
||
311 | * @param string $authKey the given auth key |
||
312 | * @return boolean whether the given auth key is valid. |
||
313 | * @see getAuthKey() |
||
314 | */ |
||
315 | 1 | public function validateAuthKey($authKey) |
|
320 | |||
321 | /** |
||
322 | * Returns a key that can be used to check the validity of a given identity ID. |
||
323 | * |
||
324 | * The key should be unique for each individual user, and should be persistent |
||
325 | * so that it can be used to check the validity of the user identity. |
||
326 | * |
||
327 | * The space of such keys should be big enough to defeat potential identity attacks. |
||
328 | * |
||
329 | * This is required if [[User::enableAutoLogin]] is enabled. |
||
330 | * @return string a key that is used to check the validity of a given identity ID. |
||
331 | * @see validateAuthKey() |
||
332 | */ |
||
333 | 1 | public function getAuthKey() |
|
337 | |||
338 | /** |
||
339 | * Check active user |
||
340 | * @return bool |
||
341 | */ |
||
342 | 2 | public function isActive() |
|
346 | |||
347 | /** |
||
348 | * User creation |
||
349 | * For create the user you always must set attributes `email`, `password` and `name` |
||
350 | * @param bool $sendEmail whether to send email about registration |
||
351 | * @return bool |
||
352 | */ |
||
353 | 1 | public function create($sendEmail = false) |
|
383 | |||
384 | /** |
||
385 | * User registration |
||
386 | * @return bool |
||
387 | */ |
||
388 | 8 | public function register() |
|
418 | |||
419 | /** |
||
420 | * Password generator |
||
421 | * @return mixed |
||
422 | */ |
||
423 | 7 | public function generatePassword() |
|
427 | |||
428 | /** |
||
429 | * Generate special hash |
||
430 | * @return string |
||
431 | */ |
||
432 | 8 | public function generateToken() |
|
440 | |||
441 | /** |
||
442 | * Start password restore procedure |
||
443 | * @return bool |
||
444 | */ |
||
445 | 1 | public function restore() |
|
464 | |||
465 | /** |
||
466 | * Check blocked user |
||
467 | * @return bool |
||
468 | */ |
||
469 | 4 | public function isBlocked() |
|
473 | |||
474 | /** |
||
475 | * Block user |
||
476 | * @param bool $sendMail whether to send confirmation email about blocking. |
||
477 | * If null, use global setting Module::$enableBlockingEmail |
||
478 | * @return bool return false if user already blocked or not confirmed |
||
479 | */ |
||
480 | 1 | View Code Duplication | public function block($sendMail = null) |
498 | |||
499 | /** |
||
500 | * Unblock user |
||
501 | * @param bool $sendMail whether to send confirmation email about unblocking. |
||
502 | * If null, use global setting Module::$enableUnblockingEmail |
||
503 | * @return bool return false if user not blocked |
||
504 | */ |
||
505 | 1 | View Code Duplication | public function unblock($sendMail = null) |
523 | |||
524 | /** |
||
525 | * Change the user password |
||
526 | * @return bool |
||
527 | */ |
||
528 | 1 | public function changePassword() |
|
546 | |||
547 | /** |
||
548 | * Set new password |
||
549 | * @param bool $sendEmail whether to send email about password change |
||
550 | * @throws yii\base\Exception |
||
551 | * @throws yii\base\InvalidConfigException |
||
552 | */ |
||
553 | 1 | public function newPassword($sendEmail = null) |
|
573 | |||
574 | /** |
||
575 | * Check that user request restore |
||
576 | * @return bool |
||
577 | */ |
||
578 | 2 | public function isRestore() |
|
582 | |||
583 | /** |
||
584 | * Get user status as text |
||
585 | * @param string $status status value. If not set, get from model |
||
586 | * @return null|string |
||
587 | */ |
||
588 | public function getStatusText($status = null) |
||
611 | |||
612 | /** |
||
613 | * Get status list as array |
||
614 | * @return array |
||
615 | */ |
||
616 | public function statusesList() |
||
625 | |||
626 | /** |
||
627 | * Get user gender as text |
||
628 | * @param int $gender gender value. If not set, get from model |
||
629 | * @return null|string |
||
630 | */ |
||
631 | public function getGenderText($gender = null) |
||
648 | |||
649 | /** |
||
650 | * Get gender list as array |
||
651 | * @return array |
||
652 | */ |
||
653 | public function gendersList() |
||
660 | |||
661 | /** |
||
662 | * Resend confirmation message |
||
663 | */ |
||
664 | public function resend() |
||
673 | } |
||
674 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.