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 |
||
| 11 | class User extends AbstractEntity implements AdvancedUserInterface, EncoderAwareInterface, \Serializable |
||
| 12 | { |
||
| 13 | const ENCODER_BCRYPT = 'bcrypt'; |
||
| 14 | const ENCODER_MD5 = 'md5'; |
||
| 15 | const ENCODER_SHA = 'salted_sha'; |
||
| 16 | |||
| 17 | //region Column properties |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var integer |
||
| 21 | */ |
||
| 22 | protected $id; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $login; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $password; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $email; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var DateTime |
||
| 41 | */ |
||
| 42 | protected $lastAction = 'CURRENT_TIMESTAMP'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $userAction; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | protected $userActionId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $locationVector; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | protected $mailCount = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | protected $userMailId; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var integer |
||
| 71 | */ |
||
| 72 | protected $k; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var integer |
||
| 76 | */ |
||
| 77 | protected $kWallet = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | protected $listingAmount = 32; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $listingOrder = 'desc'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var integer |
||
| 91 | */ |
||
| 92 | protected $headerId; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $hash; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var DateTime |
||
| 101 | */ |
||
| 102 | protected $accLockout; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var integer |
||
| 106 | */ |
||
| 107 | protected $passwordChangePeriod; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var integer |
||
| 111 | */ |
||
| 112 | protected $loginRetry = 0; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var DateTime |
||
| 116 | */ |
||
| 117 | protected $dateLastLogin = '0000-00-00 00:00:00'; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var DateTime |
||
| 121 | */ |
||
| 122 | protected $datePasswordChanged; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var DateTime |
||
| 126 | */ |
||
| 127 | protected $dateLoginFailed; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var integer |
||
| 131 | */ |
||
| 132 | protected $mailNotify = 0; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var integer |
||
| 136 | */ |
||
| 137 | protected $bookstyle = 0; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var string |
||
| 141 | */ |
||
| 142 | protected $settingMetadata; |
||
| 143 | |||
| 144 | //endregion |
||
| 145 | |||
| 146 | //region Association properties |
||
| 147 | |||
| 148 | protected $mails; |
||
| 149 | |||
| 150 | //endregion |
||
| 151 | |||
| 152 | //region Column methods |
||
| 153 | |||
| 154 | public function getId() |
||
| 155 | { |
||
| 156 | return $this->id; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getLogin() |
||
| 160 | { |
||
| 161 | return $this->login; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function setLogin($login) |
||
| 165 | { |
||
| 166 | $this->login = $login; |
||
| 167 | |||
| 168 | return $this; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the password used to authenticate the user. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getPassword() |
||
| 180 | |||
| 181 | public function setPassword($password) |
||
| 187 | |||
| 188 | public function getEmail() |
||
| 192 | |||
| 193 | public function setEmail($email) |
||
| 194 | { |
||
| 195 | $this->email = $email; |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getLastAction() |
||
| 204 | |||
| 205 | public function setLastAction($lastAction) |
||
| 211 | |||
| 212 | public function getUserAction() |
||
| 216 | |||
| 217 | public function setUserAction($userAction) |
||
| 223 | |||
| 224 | public function getUserActionId() |
||
| 228 | |||
| 229 | public function setUserActionId($userActionId) |
||
| 235 | |||
| 236 | public function getLocationVector() |
||
| 240 | |||
| 241 | public function setUserLocationVector($locationVector) |
||
| 247 | |||
| 248 | public function getMailCount() |
||
| 252 | |||
| 253 | public function setMailCount($mailCount) |
||
| 259 | |||
| 260 | public function getUserMailId() |
||
| 264 | |||
| 265 | public function setUserMailId($userMailId) |
||
| 271 | |||
| 272 | public function getK() |
||
| 276 | |||
| 277 | public function setK($k) |
||
| 283 | |||
| 284 | public function getKWallet() |
||
| 288 | |||
| 289 | public function setKWallet($kWallet) |
||
| 295 | |||
| 296 | public function getListingAmount() |
||
| 300 | |||
| 301 | public function setListingAmount($listingAmount) |
||
| 307 | |||
| 308 | public function getListingOrder() |
||
| 312 | |||
| 313 | public function setListingOrder($listingOrder) |
||
| 319 | |||
| 320 | public function getHeaderId() |
||
| 324 | |||
| 325 | public function setHeaderId($headerId) |
||
| 331 | |||
| 332 | public function getHash() |
||
| 336 | |||
| 337 | public function setHash($hash) |
||
| 343 | |||
| 344 | public function getAccLockout() |
||
| 348 | |||
| 349 | public function setAccLockout($accLockout) |
||
| 355 | |||
| 356 | public function getPasswordChangePeriod() |
||
| 360 | |||
| 361 | public function setPasswordChangePeriod($passwordChangePeriod) |
||
| 367 | |||
| 368 | public function getLoginRetry() |
||
| 372 | |||
| 373 | public function setLoginRetry($loginRetry) |
||
| 379 | |||
| 380 | public function getDateLastLogin() |
||
| 384 | |||
| 385 | public function setDateLastLogin(DateTime $dateLastLogin) |
||
| 391 | |||
| 392 | public function getDatePasswordChanged() |
||
| 396 | |||
| 397 | public function setDatePasswordChanged($datePasswordChanged) |
||
| 403 | |||
| 404 | public function getDateLoginFailed() |
||
| 408 | |||
| 409 | public function setDateLoginFailed($dateLoginFailed) |
||
| 415 | |||
| 416 | public function getMailNotify() |
||
| 420 | |||
| 421 | public function setMailNotify($mailNotify) |
||
| 427 | |||
| 428 | //endregion |
||
| 429 | |||
| 430 | //region Association methods |
||
| 431 | |||
| 432 | public function getMails() |
||
| 436 | |||
| 437 | public function setMails($mails) |
||
| 443 | |||
| 444 | //endregion |
||
| 445 | |||
| 446 | //region UserInterface |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the username used to authenticate the user. |
||
| 450 | * |
||
| 451 | * @return string The username |
||
| 452 | */ |
||
| 453 | public function getUsername() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Returns the roles granted to the user. |
||
| 460 | * |
||
| 461 | * Alternatively, the roles might be stored on a ``roles`` property, |
||
| 462 | * and populated in any number of different ways when the user object |
||
| 463 | * is created. |
||
| 464 | * |
||
| 465 | * @return (Role|string)[] The user roles |
||
| 466 | */ |
||
| 467 | public function getRoles() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the salt that was originally used to encode the password. |
||
| 476 | * |
||
| 477 | * This can return null if the password was not encoded using a salt. |
||
| 478 | * |
||
| 479 | * @return string|null The salt |
||
| 480 | */ |
||
| 481 | public function getSalt() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Removes sensitive data from the user. |
||
| 488 | * |
||
| 489 | * This is important if, at any given point, sensitive information like |
||
| 490 | * the plain-text password is stored on this object. |
||
| 491 | */ |
||
| 492 | public function eraseCredentials() |
||
| 496 | |||
| 497 | //endregion |
||
| 498 | |||
| 499 | //region AdvancedUserInterface |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Checks whether the user's account has expired. |
||
| 503 | * |
||
| 504 | * Internally, if this method returns false, the authentication system |
||
| 505 | * will throw an AccountExpiredException and prevent login. |
||
| 506 | * |
||
| 507 | * @return bool true if the user's account is non expired, false otherwise |
||
| 508 | * |
||
| 509 | * @see AccountExpiredException |
||
| 510 | */ |
||
| 511 | public function isAccountNonExpired() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Checks whether the user is locked. |
||
| 518 | * |
||
| 519 | * Internally, if this method returns false, the authentication system |
||
| 520 | * will throw a LockedException and prevent login. |
||
| 521 | * |
||
| 522 | * @return bool true if the user is not locked, false otherwise |
||
| 523 | * |
||
| 524 | * @see LockedException |
||
| 525 | */ |
||
| 526 | public function isAccountNonLocked() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Checks whether the user's credentials (password) has expired. |
||
| 535 | * |
||
| 536 | * Internally, if this method returns false, the authentication system |
||
| 537 | * will throw a CredentialsExpiredException and prevent login. |
||
| 538 | * |
||
| 539 | * @return bool true if the user's credentials are non expired, false otherwise |
||
| 540 | * |
||
| 541 | * @see CredentialsExpiredException |
||
| 542 | */ |
||
| 543 | public function isCredentialsNonExpired() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Checks whether the user is enabled. |
||
| 550 | * |
||
| 551 | * Internally, if this method returns false, the authentication system |
||
| 552 | * will throw a DisabledException and prevent login. |
||
| 553 | * |
||
| 554 | * @return bool true if the user is enabled, false otherwise |
||
| 555 | * |
||
| 556 | * @see DisabledException |
||
| 557 | */ |
||
| 558 | public function isEnabled() |
||
| 563 | |||
| 564 | //endregion |
||
| 565 | |||
| 566 | //region EncoderAwareInterface |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Gets the name of the encoder used to encode the password. |
||
| 570 | * |
||
| 571 | * If the method returns null, the standard way to retrieve the encoder |
||
| 572 | * will be used instead. |
||
| 573 | * |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | public function getEncoderName() |
||
| 586 | |||
| 587 | //endregion |
||
| 588 | |||
| 589 | //region Serializable |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @see \Serializable::serialize() |
||
| 593 | */ |
||
| 594 | public function serialize() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param string $serialized |
||
| 606 | * @see \Serializable::unserialize() |
||
| 607 | */ |
||
| 608 | public function unserialize($serialized) |
||
| 617 | |||
| 618 | //endregion |
||
| 619 | } |
||
| 620 |