Complex classes like UserEntity 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 UserEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class UserEntity implements AdvancedUserInterface, \Serializable |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | * |
||
| 24 | * @ORM\Column(name="id", type="integer") |
||
| 25 | * @ORM\Id |
||
| 26 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
| 27 | */ |
||
| 28 | protected $id; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * What is the locale for this user? |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | * |
||
| 35 | * @ORM\Column(name="locale", type="string", length=8, nullable=true) |
||
| 36 | */ |
||
| 37 | protected $locale = 'en_US'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | * |
||
| 42 | * @ORM\Column(name="username", type="string", length=64, unique=true) |
||
| 43 | */ |
||
| 44 | protected $username; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | * |
||
| 49 | * @ORM\Column(name="email", type="string", length=64, unique=true) |
||
| 50 | */ |
||
| 51 | protected $email; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * We must confirm the new password, so temporary save it inside this field. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | * |
||
| 58 | * @ORM\Column(name="new_email", type="string", length=64) |
||
| 59 | */ |
||
| 60 | protected $newEmail; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | * |
||
| 65 | * @ORM\Column(name="roles", type="json_array", nullable=true) |
||
| 66 | */ |
||
| 67 | protected $roles; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | * |
||
| 72 | * @ORM\Column(name="password", type="string", length=255) |
||
| 73 | */ |
||
| 74 | protected $password; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Used only when saving the user. |
||
| 78 | * |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $plainPassword; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Used only when saving a new password. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $oldPassword; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | * |
||
| 93 | * @ORM\Column(name="salt", type="string", length=255, nullable=true) |
||
| 94 | */ |
||
| 95 | protected $salt; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Used for emails & co. |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | * |
||
| 102 | * @ORM\Column(name="token", type="string", length=255, nullable=true) |
||
| 103 | */ |
||
| 104 | protected $token; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Used for authentication & co. |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | * |
||
| 111 | * @ORM\Column(name="access_token", type="string", length=255, nullable=true) |
||
| 112 | */ |
||
| 113 | protected $accessToken; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var bool |
||
| 117 | * |
||
| 118 | * @ORM\Column(name="enabled", type="boolean") |
||
| 119 | */ |
||
| 120 | protected $enabled = true; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool |
||
| 124 | * |
||
| 125 | * @ORM\Column(name="locked", type="boolean") |
||
| 126 | */ |
||
| 127 | protected $locked = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | * |
||
| 132 | * @ORM\Column(name="reset_password_code", type="string", length=255, nullable=true, unique=true) |
||
| 133 | */ |
||
| 134 | protected $resetPasswordCode; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | * |
||
| 139 | * @ORM\Column(name="activation_code", type="string", length=255, nullable=true, unique=true) |
||
| 140 | */ |
||
| 141 | protected $activationCode; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | * |
||
| 146 | * @ORM\Column(name="new_email_code", type="string", length=255, nullable=true, unique=true) |
||
| 147 | */ |
||
| 148 | protected $newEmailCode; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var \DateTime |
||
| 152 | * |
||
| 153 | * @ORM\Column(name="time_last_active", type="datetime", nullable=true) |
||
| 154 | */ |
||
| 155 | protected $timeLastActive; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var \DateTime |
||
| 159 | * |
||
| 160 | * @ORM\Column(name="time_reset_password_code_expires", type="datetime", nullable=true) |
||
| 161 | */ |
||
| 162 | protected $timeResetPasswordCodeExpires; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var \DateTime |
||
| 166 | * |
||
| 167 | * @ORM\Column(name="time_created", type="datetime") |
||
| 168 | */ |
||
| 169 | protected $timeCreated; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var \DateTime |
||
| 173 | * |
||
| 174 | * @ORM\Column(name="time_updated", type="datetime") |
||
| 175 | */ |
||
| 176 | protected $timeUpdated; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @ORM\OneToOne(targetEntity="Application\Entity\ProfileEntity", mappedBy="user", cascade={"all"}) |
||
| 180 | **/ |
||
| 181 | protected $profile; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var ArrayCollection |
||
| 185 | * |
||
| 186 | * @ORM\OneToMany(targetEntity="Application\Entity\PostEntity", mappedBy="user", cascade={"all"}) |
||
| 187 | */ |
||
| 188 | protected $posts; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var ArrayCollection |
||
| 192 | * |
||
| 193 | * @ORM\OneToMany(targetEntity="Application\Entity\UserActionEntity", mappedBy="user", cascade={"all"}) |
||
| 194 | */ |
||
| 195 | protected $userActions; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Otherwise known as: userExpired / accountExpired. |
||
| 199 | * |
||
| 200 | * @var bool |
||
| 201 | */ |
||
| 202 | protected $expired = false; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var bool |
||
| 206 | */ |
||
| 207 | protected $credentialsExpired = false; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * The constructor. |
||
| 211 | */ |
||
| 212 | public function __construct() |
||
| 241 | |||
| 242 | /*** Id ***/ |
||
| 243 | /** |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function getId() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param int $id |
||
| 253 | * |
||
| 254 | * @return UserEntity |
||
| 255 | */ |
||
| 256 | public function setId($id) |
||
| 262 | |||
| 263 | /*** Locale ***/ |
||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getLocale() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $locale |
||
| 274 | * |
||
| 275 | * @return UserEntity |
||
| 276 | */ |
||
| 277 | public function setLocale($locale) |
||
| 283 | |||
| 284 | /*** Username ***/ |
||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getUsername() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $username |
||
| 295 | * |
||
| 296 | * @return UserEntity |
||
| 297 | */ |
||
| 298 | public function setUsername($username) |
||
| 304 | |||
| 305 | /*** Email ***/ |
||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getEmail() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $email |
||
| 316 | * |
||
| 317 | * @return UserEntity |
||
| 318 | */ |
||
| 319 | public function setEmail($email) |
||
| 325 | |||
| 326 | /*** New email ***/ |
||
| 327 | /** |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getNewEmail() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $newEmail |
||
| 337 | * |
||
| 338 | * @return UserEntity |
||
| 339 | */ |
||
| 340 | public function setNewEmail($newEmail) |
||
| 346 | |||
| 347 | /*** Password ***/ |
||
| 348 | /** |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getPassword() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $password |
||
| 358 | * |
||
| 359 | * @return UserEntity |
||
| 360 | */ |
||
| 361 | public function setPassword($password) |
||
| 369 | |||
| 370 | /*** Plain password ***/ |
||
| 371 | /** |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function getPlainPassword() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $plainPassword |
||
| 381 | * @param EncoderFactory $encoderFactory |
||
| 382 | * |
||
| 383 | * @return UserEntity |
||
| 384 | */ |
||
| 385 | public function setPlainPassword($plainPassword, EncoderFactory $encoderFactory = null) |
||
| 402 | |||
| 403 | /*** Old password ***/ |
||
| 404 | /** |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function getOldPassword() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $oldPassword |
||
| 414 | * |
||
| 415 | * @return UserEntity |
||
| 416 | */ |
||
| 417 | public function setOldPassword($oldPassword) |
||
| 423 | |||
| 424 | /*** Salt ***/ |
||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getSalt() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param string $salt |
||
| 435 | * |
||
| 436 | * @return UserEntity |
||
| 437 | */ |
||
| 438 | public function setSalt($salt) |
||
| 444 | |||
| 445 | /*** Token ***/ |
||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getToken() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $token |
||
| 456 | * |
||
| 457 | * @return UserEntity |
||
| 458 | */ |
||
| 459 | public function setToken($token) |
||
| 465 | |||
| 466 | /*** Access Token ***/ |
||
| 467 | /** |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getAccessToken() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param string $accessToken |
||
| 477 | * |
||
| 478 | * @return UserEntity |
||
| 479 | */ |
||
| 480 | public function setAccessToken($accessToken) |
||
| 486 | |||
| 487 | /*** Enabled ***/ |
||
| 488 | /** |
||
| 489 | * @return bool |
||
| 490 | */ |
||
| 491 | public function getEnabled() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return bool |
||
| 498 | */ |
||
| 499 | public function isEnabled() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param bool $enabled |
||
| 506 | */ |
||
| 507 | public function setEnabled($enabled) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @return UserEntity |
||
| 516 | */ |
||
| 517 | public function enable() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return UserEntity |
||
| 526 | */ |
||
| 527 | public function disable() |
||
| 533 | |||
| 534 | /*** Locked ***/ |
||
| 535 | /** |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | public function getLocked() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | public function isLocked() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param bool $locked |
||
| 553 | * |
||
| 554 | * @return UserEntity |
||
| 555 | */ |
||
| 556 | public function setLocked($locked) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return UserEntity |
||
| 565 | */ |
||
| 566 | public function lock() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @return bool |
||
| 575 | */ |
||
| 576 | public function isAccountNonLocked() |
||
| 580 | |||
| 581 | /*** Reset password code ***/ |
||
| 582 | /** |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getResetPasswordCode() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param string $resetPasswordCode |
||
| 592 | * |
||
| 593 | * @return UserEntity |
||
| 594 | */ |
||
| 595 | public function setResetPasswordCode($resetPasswordCode) |
||
| 601 | |||
| 602 | /*** Activation code ***/ |
||
| 603 | /** |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function getActivationCode() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param string $activationCode |
||
| 613 | * |
||
| 614 | * @return UserEntity |
||
| 615 | */ |
||
| 616 | public function setActivationCode($activationCode) |
||
| 622 | |||
| 623 | /*** New email code ***/ |
||
| 624 | /** |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function getNewEmailCode() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param string $newEmailCode |
||
| 634 | * |
||
| 635 | * @return UserEntity |
||
| 636 | */ |
||
| 637 | public function setNewEmailCode($newEmailCode) |
||
| 643 | |||
| 644 | /*** Time last active ***/ |
||
| 645 | /** |
||
| 646 | * @return \DateTime |
||
| 647 | */ |
||
| 648 | public function getTimeLastActive() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @param $timeLastActive |
||
| 655 | * |
||
| 656 | * @return UserEntity |
||
| 657 | */ |
||
| 658 | public function setTimeLastActive(\Datetime $timeLastActive = null) |
||
| 664 | |||
| 665 | /*** Time reset password code expires ***/ |
||
| 666 | /** |
||
| 667 | * @return \DateTime |
||
| 668 | */ |
||
| 669 | public function getTimeResetPasswordCodeExpires() |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @param $timeResetPasswordCodeExpires |
||
| 676 | * |
||
| 677 | * @return UserEntity |
||
| 678 | */ |
||
| 679 | public function setTimeResetPasswordCodeExpires(\Datetime $timeResetPasswordCodeExpires = null) |
||
| 685 | |||
| 686 | /*** Time created ***/ |
||
| 687 | /** |
||
| 688 | * @return \DateTime |
||
| 689 | */ |
||
| 690 | public function getTimeCreated() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param \DateTime $timeCreated |
||
| 697 | * |
||
| 698 | * @return UserEntity |
||
| 699 | */ |
||
| 700 | public function setTimeCreated(\Datetime $timeCreated) |
||
| 706 | |||
| 707 | /*** Time updated ***/ |
||
| 708 | /** |
||
| 709 | * @return \DateTime |
||
| 710 | */ |
||
| 711 | public function getTimeUpdated() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @param \DateTime $timeUpdated |
||
| 718 | * |
||
| 719 | * @return UserEntity |
||
| 720 | */ |
||
| 721 | public function setTimeUpdated(\DateTime $timeUpdated) |
||
| 727 | |||
| 728 | /*** Expired ***/ |
||
| 729 | /** |
||
| 730 | * @return bool |
||
| 731 | */ |
||
| 732 | public function getExpired() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | public function isExpired() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @return bool |
||
| 747 | */ |
||
| 748 | public function isAccountNonExpired() |
||
| 752 | |||
| 753 | /*** Credentials expired ***/ |
||
| 754 | /** |
||
| 755 | * @return bool |
||
| 756 | */ |
||
| 757 | public function getCredentialsExpired() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @return bool |
||
| 764 | */ |
||
| 765 | public function isCredentialsExpired() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @return bool |
||
| 772 | */ |
||
| 773 | public function isCredentialsNonExpired() |
||
| 777 | |||
| 778 | /*** Roles ***/ |
||
| 779 | /** |
||
| 780 | * @return array |
||
| 781 | */ |
||
| 782 | public function getRoles() |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @param array $roles |
||
| 795 | * |
||
| 796 | * @return UserEntity |
||
| 797 | */ |
||
| 798 | public function setRoles(array $roles) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @param string $role |
||
| 807 | * |
||
| 808 | * @return bool |
||
| 809 | */ |
||
| 810 | public function hasRole($role) |
||
| 817 | |||
| 818 | /*** Profile ***/ |
||
| 819 | /** |
||
| 820 | * @return ProfileEntity |
||
| 821 | */ |
||
| 822 | public function getProfile() |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @param ProfileEntity $profile |
||
| 829 | * |
||
| 830 | * @return UserEntity |
||
| 831 | */ |
||
| 832 | public function setProfile(ProfileEntity $profile) |
||
| 840 | |||
| 841 | /*** Posts ***/ |
||
| 842 | /** |
||
| 843 | * @return array |
||
| 844 | */ |
||
| 845 | public function getPosts() |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @param ArrayCollection $posts |
||
| 852 | * |
||
| 853 | * @return UserEntity |
||
| 854 | */ |
||
| 855 | public function setPosts($posts) |
||
| 861 | |||
| 862 | /*** User actions ***/ |
||
| 863 | /** |
||
| 864 | * @return array |
||
| 865 | */ |
||
| 866 | public function getUserActions() |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @param $userActions |
||
| 873 | * |
||
| 874 | * @return UserEntity |
||
| 875 | */ |
||
| 876 | public function setUserActions($userActions) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param AdvancedUserInterface $user |
||
| 885 | * |
||
| 886 | * @return bool |
||
| 887 | */ |
||
| 888 | public function isEqualTo(AdvancedUserInterface $user) |
||
| 908 | |||
| 909 | /** |
||
| 910 | */ |
||
| 911 | public function eraseCredentials() |
||
| 915 | |||
| 916 | /** |
||
| 917 | * @return string |
||
| 918 | */ |
||
| 919 | public function serialize() |
||
| 929 | |||
| 930 | /** |
||
| 931 | */ |
||
| 932 | public function unserialize($serialized) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * @return string |
||
| 944 | */ |
||
| 945 | public function __toString() |
||
| 949 | |||
| 950 | /** |
||
| 951 | * @param $allData Show all the data for this user. |
||
| 952 | * |
||
| 953 | * @return array |
||
| 954 | */ |
||
| 955 | public function toArray($allData = true) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * @ORM\PreUpdate |
||
| 982 | */ |
||
| 983 | public function preUpdate() |
||
| 987 | |||
| 988 | /** |
||
| 989 | * @ORM\PrePersist |
||
| 990 | */ |
||
| 991 | public function prePersist() |
||
| 996 | } |
||
| 997 |