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 | /** @var Node */ |
||
| 149 | protected $node; |
||
| 150 | |||
| 151 | /** @var Mail[] */ |
||
| 152 | protected $mails; |
||
| 153 | |||
| 154 | //endregion |
||
| 155 | |||
| 156 | //region Column methods |
||
| 157 | |||
| 158 | public function getId() |
||
| 159 | { |
||
| 160 | return $this->id; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function getLogin() |
||
| 164 | { |
||
| 165 | return $this->login; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function setLogin($login) |
||
| 169 | { |
||
| 170 | $this->login = $login; |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the password used to authenticate the user. |
||
| 177 | * |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getPassword() |
||
| 184 | |||
| 185 | public function setPassword($password) |
||
| 191 | |||
| 192 | public function getEmail() |
||
| 196 | |||
| 197 | public function setEmail($email) |
||
| 198 | { |
||
| 199 | $this->email = $email; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getLastAction() |
||
| 208 | |||
| 209 | public function setLastAction($lastAction) |
||
| 215 | |||
| 216 | public function getUserAction() |
||
| 220 | |||
| 221 | public function setUserAction($userAction) |
||
| 227 | |||
| 228 | public function getUserActionId() |
||
| 232 | |||
| 233 | public function setUserActionId($userActionId) |
||
| 239 | |||
| 240 | public function getLocationVector() |
||
| 244 | |||
| 245 | public function setUserLocationVector($locationVector) |
||
| 251 | |||
| 252 | public function getMailCount() |
||
| 256 | |||
| 257 | public function setMailCount($mailCount) |
||
| 263 | |||
| 264 | public function getUserMailId() |
||
| 268 | |||
| 269 | public function setUserMailId($userMailId) |
||
| 275 | |||
| 276 | public function getK() |
||
| 280 | |||
| 281 | public function setK($k) |
||
| 287 | |||
| 288 | public function getKWallet() |
||
| 292 | |||
| 293 | public function setKWallet($kWallet) |
||
| 299 | |||
| 300 | public function getListingAmount() |
||
| 304 | |||
| 305 | public function setListingAmount($listingAmount) |
||
| 311 | |||
| 312 | public function getListingOrder() |
||
| 316 | |||
| 317 | public function setListingOrder($listingOrder) |
||
| 323 | |||
| 324 | public function getHeaderId() |
||
| 328 | |||
| 329 | public function setHeaderId($headerId) |
||
| 335 | |||
| 336 | public function getHash() |
||
| 340 | |||
| 341 | public function setHash($hash) |
||
| 347 | |||
| 348 | public function getAccLockout() |
||
| 352 | |||
| 353 | public function setAccLockout($accLockout) |
||
| 359 | |||
| 360 | public function getPasswordChangePeriod() |
||
| 364 | |||
| 365 | public function setPasswordChangePeriod($passwordChangePeriod) |
||
| 371 | |||
| 372 | public function getLoginRetry() |
||
| 376 | |||
| 377 | public function setLoginRetry($loginRetry) |
||
| 383 | |||
| 384 | public function getDateLastLogin() |
||
| 388 | |||
| 389 | public function setDateLastLogin(DateTime $dateLastLogin) |
||
| 395 | |||
| 396 | public function getDatePasswordChanged() |
||
| 400 | |||
| 401 | public function setDatePasswordChanged($datePasswordChanged) |
||
| 407 | |||
| 408 | public function getDateLoginFailed() |
||
| 412 | |||
| 413 | public function setDateLoginFailed($dateLoginFailed) |
||
| 419 | |||
| 420 | public function getMailNotify() |
||
| 424 | |||
| 425 | public function setMailNotify($mailNotify) |
||
| 431 | |||
| 432 | //endregion |
||
| 433 | |||
| 434 | //region Association methods |
||
| 435 | |||
| 436 | public function getNode() |
||
| 440 | |||
| 441 | public function getMails() |
||
| 445 | |||
| 446 | public function setMails($mails) |
||
| 452 | |||
| 453 | //endregion |
||
| 454 | |||
| 455 | //region UserInterface |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns the username used to authenticate the user. |
||
| 459 | * |
||
| 460 | * @return string The username |
||
| 461 | */ |
||
| 462 | public function getUsername() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Returns the roles granted to the user. |
||
| 469 | * |
||
| 470 | * Alternatively, the roles might be stored on a ``roles`` property, |
||
| 471 | * and populated in any number of different ways when the user object |
||
| 472 | * is created. |
||
| 473 | * |
||
| 474 | * @return (Role|string)[] The user roles |
||
| 475 | */ |
||
| 476 | public function getRoles() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns the salt that was originally used to encode the password. |
||
| 485 | * |
||
| 486 | * This can return null if the password was not encoded using a salt. |
||
| 487 | * |
||
| 488 | * @return string|null The salt |
||
| 489 | */ |
||
| 490 | public function getSalt() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Removes sensitive data from the user. |
||
| 497 | * |
||
| 498 | * This is important if, at any given point, sensitive information like |
||
| 499 | * the plain-text password is stored on this object. |
||
| 500 | */ |
||
| 501 | public function eraseCredentials() |
||
| 505 | |||
| 506 | //endregion |
||
| 507 | |||
| 508 | //region AdvancedUserInterface |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Checks whether the user's account has expired. |
||
| 512 | * |
||
| 513 | * Internally, if this method returns false, the authentication system |
||
| 514 | * will throw an AccountExpiredException and prevent login. |
||
| 515 | * |
||
| 516 | * @return bool true if the user's account is non expired, false otherwise |
||
| 517 | * |
||
| 518 | * @see AccountExpiredException |
||
| 519 | */ |
||
| 520 | public function isAccountNonExpired() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Checks whether the user is locked. |
||
| 527 | * |
||
| 528 | * Internally, if this method returns false, the authentication system |
||
| 529 | * will throw a LockedException and prevent login. |
||
| 530 | * |
||
| 531 | * @return bool true if the user is not locked, false otherwise |
||
| 532 | * |
||
| 533 | * @see LockedException |
||
| 534 | */ |
||
| 535 | public function isAccountNonLocked() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Checks whether the user's credentials (password) has expired. |
||
| 544 | * |
||
| 545 | * Internally, if this method returns false, the authentication system |
||
| 546 | * will throw a CredentialsExpiredException and prevent login. |
||
| 547 | * |
||
| 548 | * @return bool true if the user's credentials are non expired, false otherwise |
||
| 549 | * |
||
| 550 | * @see CredentialsExpiredException |
||
| 551 | */ |
||
| 552 | public function isCredentialsNonExpired() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Checks whether the user is enabled. |
||
| 559 | * |
||
| 560 | * Internally, if this method returns false, the authentication system |
||
| 561 | * will throw a DisabledException and prevent login. |
||
| 562 | * |
||
| 563 | * @return bool true if the user is enabled, false otherwise |
||
| 564 | * |
||
| 565 | * @see DisabledException |
||
| 566 | */ |
||
| 567 | public function isEnabled() |
||
| 572 | |||
| 573 | //endregion |
||
| 574 | |||
| 575 | //region EncoderAwareInterface |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Gets the name of the encoder used to encode the password. |
||
| 579 | * |
||
| 580 | * If the method returns null, the standard way to retrieve the encoder |
||
| 581 | * will be used instead. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getEncoderName() |
||
| 595 | |||
| 596 | //endregion |
||
| 597 | |||
| 598 | //region Serializable |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @see \Serializable::serialize() |
||
| 602 | */ |
||
| 603 | public function serialize() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $serialized |
||
| 615 | * @see \Serializable::unserialize() |
||
| 616 | */ |
||
| 617 | public function unserialize($serialized) |
||
| 626 | |||
| 627 | //endregion |
||
| 628 | } |
||
| 629 |