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 |
||
35 | class User extends ActiveRecord implements IdentityInterface |
||
36 | { |
||
37 | const STATUS_BLOCKED = 0; |
||
38 | const STATUS_ACTIVE = 1; |
||
39 | const STATUS_WAIT = 2; |
||
40 | const STATUS_DELETED = 3; |
||
41 | |||
42 | const LENGTH_STRING_PASSWORD_MIN = 6; |
||
43 | const LENGTH_STRING_PASSWORD_MAX = 16; |
||
44 | |||
45 | const RBAC_DEFAULT_ROLE = \modules\rbac\models\Role::ROLE_DEFAULT; |
||
46 | |||
47 | public $role; |
||
48 | public $imageFile; |
||
49 | public $isDel; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | public static function tableName() |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | public function behaviors() |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function rules() |
||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | public function attributeLabels() |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getAvatarPath() |
||
134 | |||
135 | /** |
||
136 | * @return array |
||
137 | */ |
||
138 | public static function getStatusesArray() |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getRegistrationType() |
||
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | public static function getLabelsArray() |
||
173 | |||
174 | /** |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function getStatusName() |
||
181 | |||
182 | /** |
||
183 | * Return <span class="label label-success">Active</span> |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getStatusLabelName() |
||
191 | |||
192 | /** |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getRolesArray() |
||
199 | |||
200 | /** |
||
201 | * @return mixed|null |
||
202 | */ |
||
203 | public function getUserRoleName() |
||
214 | |||
215 | /** |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function getRoleName() |
||
228 | |||
229 | /** |
||
230 | * Получаем роль пользователя |
||
231 | */ |
||
232 | public function getRoleUser() |
||
243 | |||
244 | /** |
||
245 | * @param null $user_id |
||
246 | * @return mixed|null |
||
247 | */ |
||
248 | public function getUserRoleValue($user_id = null) |
||
269 | |||
270 | /** |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getUserFullName() |
||
289 | |||
290 | /** |
||
291 | * @inheritdoc |
||
292 | */ |
||
293 | public static function findIdentity($id) |
||
297 | |||
298 | /** |
||
299 | * @param mixed $token |
||
300 | * @param null $type |
||
301 | * @return null|static |
||
302 | */ |
||
303 | public static function findIdentityByAccessToken($token, $type = null) |
||
307 | |||
308 | /** |
||
309 | * Finds user by username |
||
310 | * |
||
311 | * @param string $username |
||
312 | * @return static|null |
||
313 | */ |
||
314 | public static function findByUsername($username) |
||
318 | |||
319 | /** |
||
320 | * Finds user by email |
||
321 | * |
||
322 | * @param string $email |
||
323 | * @return static|null |
||
324 | */ |
||
325 | public static function findByUsernameEmail($email) |
||
329 | |||
330 | /** |
||
331 | * Finds user by username or email |
||
332 | * |
||
333 | * @param string $string |
||
334 | * @return static|null |
||
335 | */ |
||
336 | public static function findByUsernameOrEmail($string) |
||
343 | |||
344 | /** |
||
345 | * Finds user by password reset token |
||
346 | * |
||
347 | * @param string $token password reset token |
||
348 | * @return static|null |
||
349 | */ |
||
350 | public static function findByPasswordResetToken($token) |
||
361 | |||
362 | /** |
||
363 | * Finds out if password reset token is valid |
||
364 | * |
||
365 | * @param string $token password reset token |
||
366 | * @return boolean |
||
367 | */ |
||
368 | public static function isPasswordResetTokenValid($token) |
||
379 | |||
380 | /** |
||
381 | * @inheritdoc |
||
382 | */ |
||
383 | public function getId() |
||
387 | |||
388 | /** |
||
389 | * @inheritdoc |
||
390 | */ |
||
391 | public function getAuthKey() |
||
395 | |||
396 | /** |
||
397 | * @inheritdoc |
||
398 | */ |
||
399 | public function validateAuthKey($authKey) |
||
403 | |||
404 | /** |
||
405 | * Validates password |
||
406 | * |
||
407 | * @param string $password password to validate |
||
408 | * @return boolean if password provided is valid for current user |
||
409 | */ |
||
410 | public function validatePassword($password) |
||
414 | |||
415 | /** |
||
416 | * Generates password hash from password and sets it to the model |
||
417 | * |
||
418 | * @param string $password |
||
419 | */ |
||
420 | public function setPassword($password) |
||
424 | |||
425 | /** |
||
426 | * Generates "remember me" authentication key |
||
427 | */ |
||
428 | public function generateAuthKey() |
||
432 | |||
433 | /** |
||
434 | * Generates new password reset token |
||
435 | */ |
||
436 | public function generatePasswordResetToken() |
||
440 | |||
441 | /** |
||
442 | * Removes password reset token |
||
443 | */ |
||
444 | public function removePasswordResetToken() |
||
448 | |||
449 | /** |
||
450 | * @param string $email_confirm_token |
||
451 | * @return static|null |
||
452 | */ |
||
453 | public static function findByEmailConfirmToken($email_confirm_token) |
||
457 | |||
458 | /** |
||
459 | * Generates email confirmation token |
||
460 | */ |
||
461 | public function generateEmailConfirmToken() |
||
465 | |||
466 | /** |
||
467 | * Removes email confirmation token |
||
468 | */ |
||
469 | public function removeEmailConfirmToken() |
||
473 | |||
474 | /** |
||
475 | * @return bool |
||
476 | */ |
||
477 | public function isDeleted() |
||
481 | |||
482 | /** |
||
483 | * Gravatar Service |
||
484 | * @url https://www.gravatar.com |
||
485 | * |
||
486 | * @param $email |
||
487 | * @param int $s |
||
488 | * @param string $d |
||
489 | * @param string $r |
||
490 | * @param bool $img |
||
491 | * @param array $attr |
||
492 | * @return string |
||
493 | */ |
||
494 | public function getGravatar($email = null, $s = 80, $d = 'mm', $r = 'g', $img = false, $attr = []) |
||
508 | |||
509 | /** |
||
510 | * Действия перед сохранением |
||
511 | * @inheritdoc |
||
512 | */ |
||
513 | public function beforeSave($insert) |
||
523 | |||
524 | /** |
||
525 | * Действия после сохранения |
||
526 | * @param bool $insert |
||
527 | * @param array $changedAttributes |
||
528 | */ |
||
529 | /*public function afterSave($insert, $changedAttributes) |
||
530 | { |
||
531 | parent::afterSave($insert, $changedAttributes); |
||
532 | if ($insert) { |
||
533 | |||
534 | } |
||
535 | }*/ |
||
536 | |||
537 | /** |
||
538 | * Действия перед удалением |
||
539 | * @return bool |
||
540 | */ |
||
541 | public function beforeDelete() |
||
548 | |||
549 | /** |
||
550 | * Отвязываем пользователя от всех ролей |
||
551 | */ |
||
552 | public function revokeRoles() |
||
559 | |||
560 | /** |
||
561 | * Удаляем аватарку |
||
562 | */ |
||
563 | public function removeAvatar() |
||
578 | } |
||
579 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.