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 |
||
| 33 | class User extends \yii\db\ActiveRecord implements IdentityInterface |
||
| 34 | { |
||
| 35 | const STATUS_DELETED = 0; |
||
| 36 | const STATUS_ACTIVE = 1; |
||
| 37 | const STATUS_BLOCKED = 2; |
||
| 38 | |||
| 39 | const ROLE_SUPERUSER = 'SuperUser'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $passwordNew; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritdoc |
||
| 48 | */ |
||
| 49 | 111 | public static function tableName() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritdoc |
||
| 56 | */ |
||
| 57 | 43 | public function attributeLabels() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | */ |
||
| 78 | 74 | public function behaviors() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritdoc |
||
| 92 | * @codeCoverageIgnore |
||
| 93 | */ |
||
| 94 | public function events() |
||
| 100 | |||
| 101 | 9 | public function transactions() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return \yii\db\ActiveQuery |
||
| 112 | */ |
||
| 113 | 9 | public function getProfile() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $attributes |
||
| 120 | */ |
||
| 121 | 3 | public function setProfile($attributes = []) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return \yii\db\ActiveQuery |
||
| 128 | */ |
||
| 129 | 10 | public function getProviders() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $attributes |
||
| 136 | */ |
||
| 137 | public function setProviders($attributes = []) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return \yii\db\ActiveQuery |
||
| 144 | */ |
||
| 145 | 10 | public function getRole() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritdoc |
||
| 152 | * @return \query\UserQuery the active query used by this AR class. |
||
| 153 | */ |
||
| 154 | 80 | public static function find() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritdoc |
||
| 161 | */ |
||
| 162 | 9 | public function beforeSave($insert) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | * @property \entity\UserProfile $profile |
||
| 189 | * @property \entity\UserProvider $providers |
||
| 190 | */ |
||
| 191 | 9 | public function afterSave($insert, $changedAttributes) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @inheritdoc |
||
| 210 | * @param boolean $runValidation |
||
| 211 | * @param array $attributeNames |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | public function save($runValidation = true, $attributeNames = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get all statuses |
||
| 223 | * |
||
| 224 | * @param string[] |
||
| 225 | */ |
||
| 226 | 6 | public static function getStatuses(): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get statuse name |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 6 | public function getStatusName(): string |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Is it deleted? |
||
| 248 | * |
||
| 249 | * @param bool |
||
| 250 | */ |
||
| 251 | public function isDeleted(): bool |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Is it blocked? |
||
| 258 | * |
||
| 259 | * @param bool |
||
| 260 | */ |
||
| 261 | 6 | public function isBlocked(): bool |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Is it active? |
||
| 268 | * |
||
| 269 | * @param bool |
||
| 270 | */ |
||
| 271 | 30 | public function isActive(): bool |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Is it confirmed? |
||
| 278 | * |
||
| 279 | * @param bool |
||
| 280 | */ |
||
| 281 | 10 | public function isConfirmed(): bool |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Is SuperUser? |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 15 | public function isSuperUser(): bool |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Set confirmed |
||
| 298 | */ |
||
| 299 | 2 | public function setConfirmed(): void |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get status description |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 8 | public function getStatusDescription(): string |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @inheritdoc |
||
| 322 | */ |
||
| 323 | 28 | public function getId() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @inheritdoc |
||
| 330 | */ |
||
| 331 | public function getAuthKey() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @inheritdoc |
||
| 338 | */ |
||
| 339 | public function validateAuthKey($authKey) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Generates "remember me" authentication key |
||
| 346 | */ |
||
| 347 | 3 | public function generateAuthKey() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Validates password |
||
| 354 | * |
||
| 355 | * @param string $password password to validate |
||
| 356 | * @return boolean if password provided is valid for current user |
||
| 357 | */ |
||
| 358 | 22 | public function validatePassword(string $password): bool |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Set a new password |
||
| 369 | * |
||
| 370 | * @param string $password |
||
| 371 | */ |
||
| 372 | 6 | public function setPassword(string $password): void |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Set new password reset token |
||
| 379 | * |
||
| 380 | * @param string $token |
||
| 381 | */ |
||
| 382 | 1 | public function setPasswordResetToken(string $token): void |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Removes password reset token |
||
| 389 | */ |
||
| 390 | 2 | public function removePasswordResetToken(): void |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Set new confirm email token |
||
| 397 | * |
||
| 398 | * @param string $token |
||
| 399 | */ |
||
| 400 | 4 | public function setEmailConfirmToken(string $token): void |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @inheritdoc |
||
| 408 | */ |
||
| 409 | 20 | public static function findIdentity($id) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @inheritdoc |
||
| 416 | */ |
||
| 417 | public static function findIdentityByAccessToken($token, $type = null) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Update date login |
||
| 424 | */ |
||
| 425 | 12 | public function updateDateLogin(): void |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function beforeDelete() |
||
| 446 | } |
||
| 447 |