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 |
||
| 21 | class User implements AdvancedUserInterface, \Serializable, \JsonSerializable, FootprintUserInterface { |
||
| 22 | const STATUS_UNKNOWN = 0; |
||
| 23 | const STATUS_ONLINE = 10; |
||
| 24 | const STATUS_AWAY = 11; |
||
| 25 | const DEFAULT_GRAVATAR = 'wavatar'; |
||
| 26 | |||
| 27 | public static function getAllStatus() { |
||
| 34 | |||
| 35 | public function getStatusName() { |
||
| 43 | |||
| 44 | public function jsonSerialize() { |
||
| 54 | |||
| 55 | public function __toString() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(name="id", type="guid") |
||
| 61 | * @ORM\Id |
||
| 62 | * @ORM\GeneratedValue(strategy="UUID") |
||
| 63 | * @Serializer\Groups({"basic"}) |
||
| 64 | */ |
||
| 65 | private $id; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @ORM\Column(type="string", length=25, unique=true) |
||
| 69 | * @Assert\Type(type="string") |
||
| 70 | * @Assert\NotBlank() |
||
| 71 | * @Assert\Length(min = "3", max="25"); |
||
| 72 | * @Serializer\Groups({"basic"}) |
||
| 73 | */ |
||
| 74 | private $username; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @ORM\Column(type="string", length=32) |
||
| 78 | */ |
||
| 79 | private $salt; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @ORM\Column(type="string", length=88) |
||
| 83 | */ |
||
| 84 | private $password; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @ORM\Column(type="string", length=60, unique=true) |
||
| 88 | * @Assert\Type(type="string") |
||
| 89 | * @Assert\NotBlank() |
||
| 90 | * @Assert\Email |
||
| 91 | * @Assert\Length(min = "3", max="60"); |
||
| 92 | * @Serializer\Groups({"basic"}) |
||
| 93 | */ |
||
| 94 | private $email; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @ORM\Column(type="string", length=60) |
||
| 98 | * @Assert\NotBlank() |
||
| 99 | * @Assert\Length(min = "3", max="60"); |
||
| 100 | * @Assert\Type(type="string") |
||
| 101 | * @Serializer\Groups({"basic"}) |
||
| 102 | */ |
||
| 103 | private $fullname; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @ORM\Column(type="string", length=10, nullable=true) |
||
| 107 | * @Assert\NotBlank() |
||
| 108 | * @Assert\Length(min = "2", max="10"); |
||
| 109 | * @Assert\Type(type="string") |
||
| 110 | * @Serializer\Groups({"basic"}) |
||
| 111 | */ |
||
| 112 | private $initials; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @ORM\Column(type="string", length=60, nullable=true) |
||
| 116 | * @Assert\Length(max="60"); |
||
| 117 | * @Assert\Type(type="string") |
||
| 118 | * @Serializer\Groups({"basic"}) |
||
| 119 | */ |
||
| 120 | private $jobtitle; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @ORM\Column(type="string", length=60, nullable=true) |
||
| 124 | * @Assert\Length(max="60"); |
||
| 125 | * @Assert\Type(type="string") |
||
| 126 | * @Serializer\Groups({"basic"}) |
||
| 127 | */ |
||
| 128 | private $department; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @ORM\Column(type="string", length=60, nullable=true) |
||
| 132 | * @Assert\Length(max="60"); |
||
| 133 | * @Assert\Type(type="string") |
||
| 134 | * @Serializer\Groups({"basic"}) |
||
| 135 | */ |
||
| 136 | private $location; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @ORM\Column(type="string", length=15, nullable=true) |
||
| 140 | * @Assert\Regex("^\+?[0-9]{11,15}*$") |
||
| 141 | * @Serializer\Groups({"basic"}) |
||
| 142 | */ |
||
| 143 | private $telephone; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var Group[]|ArrayCollection |
||
| 147 | * @ORM\ManyToMany(targetEntity="Vivait\AuthBundle\Entity\Group", inversedBy="users") |
||
| 148 | */ |
||
| 149 | private $groups; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @ORM\ManyToMany(targetEntity="Vivait\AuthBundle\Entity\Tenant", inversedBy="users") |
||
| 153 | * @ORM\OrderBy({"priority" = "ASC", "tenant" = "ASC"}) |
||
| 154 | */ |
||
| 155 | private $tenants; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var Tenant |
||
| 159 | * @ORM\ManyToOne(targetEntity="Vivait\AuthBundle\Entity\Tenant") |
||
| 160 | * @ORM\JoinColumn(name="current_tenant", referencedColumnName="id") |
||
| 161 | **/ |
||
| 162 | private $current_tenant; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @ORM\Column(name="active", type="boolean") |
||
| 166 | */ |
||
| 167 | private $active; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var \DateTime |
||
| 171 | * @ORM\Column(name="lastactivity", type="datetime", nullable=true) |
||
| 172 | */ |
||
| 173 | private $lastactivity; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var \DateTime |
||
| 177 | * @ORM\Column(name="lastresponse", type="datetime", nullable=true) |
||
| 178 | */ |
||
| 179 | private $lastresponse; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var string |
||
| 183 | * @ORM\Column(name="lastip", type="string", length=46, nullable=true) |
||
| 184 | */ |
||
| 185 | private $lastip; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var string |
||
| 189 | * @ORM\Column(name="lasturl", type="string", length=255, nullable=true) |
||
| 190 | */ |
||
| 191 | private $lasturl; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var string |
||
| 195 | * @ORM\Column(name="lastua", type="string", length=255, nullable=true) |
||
| 196 | */ |
||
| 197 | private $lastua; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var integer |
||
| 201 | * @ORM\Column(name="status", type="integer", nullable=true) |
||
| 202 | */ |
||
| 203 | private $status; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @ORM\Column(type="string", length=10, nullable=true) |
||
| 207 | */ |
||
| 208 | private $tfkey; |
||
| 209 | |||
| 210 | private $gravatarhash; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * This is called once Doctrine has loaded the entity |
||
| 214 | * @ORM\PostLoad |
||
| 215 | */ |
||
| 216 | |||
| 217 | ############################################# |
||
| 218 | |||
| 219 | public function __construct() { |
||
| 225 | |||
| 226 | public function isAccountNonExpired() { |
||
| 229 | |||
| 230 | public function isAccountNonLocked() { |
||
| 233 | |||
| 234 | public function isCredentialsNonExpired() { |
||
| 237 | |||
| 238 | public function isEnabled() { |
||
| 241 | |||
| 242 | |||
| 243 | private function isTenantActive() { |
||
| 251 | |||
| 252 | private function isTenantLicensed() { |
||
| 260 | |||
| 261 | public function getLicensedUntil() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritDoc |
||
| 273 | */ |
||
| 274 | public function getUsername() { |
||
| 277 | |||
| 278 | public function newSalt() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritDoc |
||
| 284 | */ |
||
| 285 | public function getSalt() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @inheritDoc |
||
| 291 | */ |
||
| 292 | public function getPassword() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @inheritDoc |
||
| 298 | */ |
||
| 299 | public function getRoles() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @inheritDoc |
||
| 315 | */ |
||
| 316 | public function eraseCredentials() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @see \Serializable::serialize() |
||
| 321 | */ |
||
| 322 | public function serialize() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @see \Serializable::unserialize() |
||
| 332 | */ |
||
| 333 | public function unserialize($serialized) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get id |
||
| 343 | * @return integer |
||
| 344 | */ |
||
| 345 | public function getId() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set username |
||
| 351 | * @param string $username |
||
| 352 | * @return User |
||
| 353 | */ |
||
| 354 | public function setUsername($username) { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Set salt |
||
| 363 | * @param string $salt |
||
| 364 | * @return User |
||
| 365 | */ |
||
| 366 | public function setSalt($salt) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set password |
||
| 374 | * @param string $password |
||
| 375 | * @return User |
||
| 376 | */ |
||
| 377 | public function setPassword($password) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set email |
||
| 385 | * @param string $email |
||
| 386 | * @return User |
||
| 387 | */ |
||
| 388 | public function setEmail($email) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get email |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getEmail() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set fullname |
||
| 404 | * @param string $fullname |
||
| 405 | * @return User |
||
| 406 | */ |
||
| 407 | public function setFullname($fullname) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * A helper method to get the tenanted full name |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getTenantedFullname() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get fullname |
||
| 423 | * @param $include_tenant boolean |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getFullname($include_tenant = false) { |
||
| 440 | |||
| 441 | |||
| 442 | public function getForename() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set initials |
||
| 449 | * @param string $initials |
||
| 450 | * @return User |
||
| 451 | */ |
||
| 452 | public function setInitials($initials) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Get initials |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getInitials() { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set jobtitle |
||
| 468 | * @param string $jobtitle |
||
| 469 | * @return User |
||
| 470 | */ |
||
| 471 | public function setJobtitle($jobtitle) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get jobtitle |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getJobtitle() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set department |
||
| 487 | * @param string $department |
||
| 488 | * @return User |
||
| 489 | */ |
||
| 490 | public function setDepartment($department) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get department |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public function getDepartment() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set location |
||
| 506 | * @param string $location |
||
| 507 | * @return User |
||
| 508 | */ |
||
| 509 | public function setLocation($location) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get location |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | public function getLocation() { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Set telephone |
||
| 525 | * @param string $telephone |
||
| 526 | * @return User |
||
| 527 | */ |
||
| 528 | public function setTelephone($telephone) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get telephone |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getTelephone() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Set active |
||
| 544 | * @param boolean $active |
||
| 545 | * @return User |
||
| 546 | */ |
||
| 547 | public function setActive($active) { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Get active |
||
| 555 | * @return boolean |
||
| 556 | */ |
||
| 557 | public function getActive() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Add groups |
||
| 563 | * @param Group $groups |
||
| 564 | * @return User |
||
| 565 | */ |
||
| 566 | public function addGroup(Group $groups) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Remove groups |
||
| 574 | * @param Group $groups |
||
| 575 | */ |
||
| 576 | public function removeGroup(Group $groups) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get groups |
||
| 582 | * @return Group[]|ArrayCollection |
||
| 583 | */ |
||
| 584 | public function getGroups() { |
||
| 587 | |||
| 588 | |||
| 589 | /** |
||
| 590 | * Add tenants |
||
| 591 | * @param Tenant $tenants |
||
| 592 | * @return User |
||
| 593 | */ |
||
| 594 | public function addTenant(Tenant $tenants) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Remove tenants |
||
| 602 | * @param Tenant $tenants |
||
| 603 | */ |
||
| 604 | public function removeTenant(Tenant $tenants) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get tenants |
||
| 610 | * @return Tenant[]|ArrayCollection |
||
| 611 | */ |
||
| 612 | public function getTenants() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Sets current_tenant |
||
| 618 | * @param Tenant $current_tenant |
||
| 619 | * @return $this |
||
| 620 | */ |
||
| 621 | public function setCurrentTenant(Tenant $current_tenant) { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @return Tenant |
||
| 628 | */ |
||
| 629 | public function getCurrentTenant() { |
||
| 632 | |||
| 633 | public function getAllowedTenants() { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Set lastactivity |
||
| 652 | * @param \DateTime $lastactivity |
||
| 653 | * @return User |
||
| 654 | */ |
||
| 655 | public function setLastactivity($lastactivity) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Get lastactivity |
||
| 663 | * @return \DateTime |
||
| 664 | */ |
||
| 665 | public function getLastactivity() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Set status |
||
| 671 | * @param integer $status |
||
| 672 | * @return User |
||
| 673 | */ |
||
| 674 | public function setStatus($status) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get status |
||
| 682 | * @return integer |
||
| 683 | */ |
||
| 684 | public function getStatus() { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Set lastresponse |
||
| 690 | * @param \DateTime $lastresponse |
||
| 691 | * @return User |
||
| 692 | */ |
||
| 693 | public function setLastresponse($lastresponse) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Get lastresponse |
||
| 701 | * @return \DateTime |
||
| 702 | */ |
||
| 703 | public function getLastresponse() { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Set Lastip |
||
| 709 | * @param string $lastip |
||
| 710 | * @return $this |
||
| 711 | */ |
||
| 712 | public function setLastip($lastip) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Get Lastip |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | public function getLastip() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Set tfkey |
||
| 727 | * @param string $tfkey |
||
| 728 | * @return User |
||
| 729 | */ |
||
| 730 | public function setTfkey($tfkey) { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Get tfkey |
||
| 738 | * @return string |
||
| 739 | */ |
||
| 740 | public function getTfkey() { |
||
| 743 | |||
| 744 | public function getGravatarHash() { |
||
| 751 | |||
| 752 | public function getGravatar() { |
||
| 755 | /** |
||
| 756 | * Get Lastua |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | public function getLastua() { |
||
| 762 | /** |
||
| 763 | * Set Lastua |
||
| 764 | * @param string $lastua |
||
| 765 | * @return $this |
||
| 766 | */ |
||
| 767 | public function setLastua($lastua) { |
||
| 771 | /** |
||
| 772 | * Get Lasturl |
||
| 773 | * @return string |
||
| 774 | */ |
||
| 775 | public function getLasturl() { |
||
| 778 | /** |
||
| 779 | * Set Lasturl |
||
| 780 | * @param string $lasturl |
||
| 781 | * @return $this |
||
| 782 | */ |
||
| 783 | public function setLasturl($lasturl) { |
||
| 787 | |||
| 788 | public function hashPassword(EncoderFactoryInterface $encoder_factory){ |
||
| 795 | } |
||
| 796 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.