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, nullable=true) |
||
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 | * @var bool |
||
99 | * |
||
100 | * @ORM\Column(name="enabled", type="boolean") |
||
101 | */ |
||
102 | protected $enabled = true; |
||
103 | |||
104 | /** |
||
105 | * @var bool |
||
106 | * |
||
107 | * @ORM\Column(name="locked", type="boolean") |
||
108 | */ |
||
109 | protected $locked = false; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | * |
||
114 | * @ORM\Column(name="reset_password_code", type="string", length=255, nullable=true, unique=true) |
||
115 | */ |
||
116 | protected $resetPasswordCode; |
||
117 | |||
118 | /** |
||
119 | * @var string |
||
120 | * |
||
121 | * @ORM\Column(name="activation_code", type="string", length=255, nullable=true, unique=true) |
||
122 | */ |
||
123 | protected $activationCode; |
||
124 | |||
125 | /** |
||
126 | * @var string |
||
127 | * |
||
128 | * @ORM\Column(name="new_email_code", type="string", length=255, nullable=true, unique=true) |
||
129 | */ |
||
130 | protected $newEmailCode; |
||
131 | |||
132 | /** |
||
133 | * @var \DateTime |
||
134 | * |
||
135 | * @ORM\Column(name="time_last_active", type="datetime", nullable=true) |
||
136 | */ |
||
137 | protected $timeLastActive; |
||
138 | |||
139 | /** |
||
140 | * @var \DateTime |
||
141 | * |
||
142 | * @ORM\Column(name="time_reset_password_code_expires", type="datetime", nullable=true) |
||
143 | */ |
||
144 | protected $timeResetPasswordCodeExpires; |
||
145 | |||
146 | /** |
||
147 | * @var \DateTime |
||
148 | * |
||
149 | * @ORM\Column(name="time_created", type="datetime") |
||
150 | */ |
||
151 | protected $timeCreated; |
||
152 | |||
153 | /** |
||
154 | * @var \DateTime |
||
155 | * |
||
156 | * @ORM\Column(name="time_updated", type="datetime") |
||
157 | */ |
||
158 | protected $timeUpdated; |
||
159 | |||
160 | /** |
||
161 | * @ORM\OneToOne(targetEntity="Application\Entity\ProfileEntity", mappedBy="user", cascade={"all"}) |
||
162 | **/ |
||
163 | protected $profile; |
||
164 | |||
165 | /** |
||
166 | * @var ArrayCollection |
||
167 | * |
||
168 | * @ORM\OneToMany(targetEntity="Application\Entity\PostEntity", mappedBy="user", cascade={"all"}) |
||
169 | */ |
||
170 | protected $posts; |
||
171 | |||
172 | /** |
||
173 | * @var ArrayCollection |
||
174 | * |
||
175 | * @ORM\OneToMany(targetEntity="Application\Entity\UserActionEntity", mappedBy="user", cascade={"all"}) |
||
176 | */ |
||
177 | protected $userActions; |
||
178 | |||
179 | /** |
||
180 | * Otherwise known as: userExpired / accountExpired. |
||
181 | * |
||
182 | * @var bool |
||
183 | */ |
||
184 | protected $expired = false; |
||
185 | |||
186 | /** |
||
187 | * @var bool |
||
188 | */ |
||
189 | protected $credentialsExpired = false; |
||
190 | |||
191 | /** |
||
192 | * The constructor. |
||
193 | */ |
||
194 | public function __construct() |
||
195 | { |
||
196 | $this->setSalt( |
||
197 | md5(uniqid(null, true)) |
||
198 | ); |
||
199 | |||
200 | $this->setActivationCode( |
||
201 | md5(uniqid(null, true)) |
||
202 | ); |
||
203 | |||
204 | $this->posts = new ArrayCollection(); |
||
205 | $this->userActions = new ArrayCollection(); |
||
206 | } |
||
207 | |||
208 | /*** Id ***/ |
||
209 | /** |
||
210 | * @return int |
||
211 | */ |
||
212 | public function getId() |
||
213 | { |
||
214 | return $this->id; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param int $id |
||
219 | * |
||
220 | * @return UserEntity |
||
221 | */ |
||
222 | public function setId($id) |
||
228 | |||
229 | /*** Locale ***/ |
||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getLocale() |
||
237 | |||
238 | /** |
||
239 | * @param string $locale |
||
240 | * |
||
241 | * @return UserEntity |
||
242 | */ |
||
243 | public function setLocale($locale) |
||
249 | |||
250 | /*** Username ***/ |
||
251 | /** |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getUsername() |
||
258 | |||
259 | /** |
||
260 | * @param string $username |
||
261 | * |
||
262 | * @return UserEntity |
||
263 | */ |
||
264 | public function setUsername($username) |
||
270 | |||
271 | /*** Email ***/ |
||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getEmail() |
||
279 | |||
280 | /** |
||
281 | * @param string $email |
||
282 | * |
||
283 | * @return UserEntity |
||
284 | */ |
||
285 | public function setEmail($email) |
||
291 | |||
292 | /*** New email ***/ |
||
293 | /** |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getNewEmail() |
||
297 | { |
||
298 | return $this->newEmail; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param string $newEmail |
||
303 | * |
||
304 | * @return UserEntity |
||
305 | */ |
||
306 | public function setNewEmail($newEmail) |
||
307 | { |
||
308 | $this->newEmail = $newEmail; |
||
309 | |||
310 | return $this; |
||
311 | } |
||
312 | |||
313 | /*** Password ***/ |
||
314 | /** |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getPassword() |
||
321 | |||
322 | /** |
||
323 | * @param string $password |
||
324 | * |
||
325 | * @return UserEntity |
||
326 | */ |
||
327 | public function setPassword($password) |
||
335 | |||
336 | /*** Plain password ***/ |
||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getPlainPassword() |
||
344 | |||
345 | /** |
||
346 | * @param string $plainPassword |
||
347 | * @param EncoderFactory $encoderFactory |
||
348 | * |
||
349 | * @return UserEntity |
||
350 | */ |
||
351 | public function setPlainPassword($plainPassword, EncoderFactory $encoderFactory = null) |
||
368 | |||
369 | /*** Old password ***/ |
||
370 | /** |
||
371 | * @return string |
||
372 | */ |
||
373 | public function getOldPassword() |
||
377 | |||
378 | /** |
||
379 | * @param string $oldPassword |
||
380 | * |
||
381 | * @return UserEntity |
||
382 | */ |
||
383 | public function setOldPassword($oldPassword) |
||
389 | |||
390 | /*** Salt ***/ |
||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | public function getSalt() |
||
398 | |||
399 | /** |
||
400 | * @param string $salt |
||
401 | * |
||
402 | * @return UserEntity |
||
403 | */ |
||
404 | public function setSalt($salt) |
||
410 | |||
411 | /*** Enabled ***/ |
||
412 | /** |
||
413 | * @return bool |
||
414 | */ |
||
415 | public function getEnabled() |
||
419 | |||
420 | /** |
||
421 | * @return bool |
||
422 | */ |
||
423 | public function isEnabled() |
||
427 | |||
428 | /** |
||
429 | * @param bool $enabled |
||
430 | */ |
||
431 | public function setEnabled($enabled) |
||
437 | |||
438 | /** |
||
439 | * @return UserEntity |
||
440 | */ |
||
441 | public function enable() |
||
442 | { |
||
443 | $this->setEnabled(true); |
||
444 | |||
445 | return $this; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @return UserEntity |
||
450 | */ |
||
451 | public function disable() |
||
452 | { |
||
453 | $this->setEnabled(false); |
||
454 | |||
455 | return $this; |
||
456 | } |
||
457 | |||
458 | /*** Locked ***/ |
||
459 | /** |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function getLocked() |
||
463 | { |
||
464 | return $this->locked; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @return bool |
||
469 | */ |
||
470 | public function isLocked() |
||
471 | { |
||
472 | return $this->getLocked(); |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * @param bool $locked |
||
477 | * |
||
478 | * @return UserEntity |
||
479 | */ |
||
480 | public function setLocked($locked) |
||
486 | |||
487 | /** |
||
488 | * @return UserEntity |
||
489 | */ |
||
490 | public function lock() |
||
496 | |||
497 | /** |
||
498 | * @return bool |
||
499 | */ |
||
500 | public function isAccountNonLocked() |
||
504 | |||
505 | /*** Reset password code ***/ |
||
506 | /** |
||
507 | * @return string |
||
508 | */ |
||
509 | public function getResetPasswordCode() |
||
510 | { |
||
511 | return $this->resetPasswordCode; |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * @param string $resetPasswordCode |
||
516 | * |
||
517 | * @return UserEntity |
||
518 | */ |
||
519 | public function setResetPasswordCode($resetPasswordCode) |
||
525 | |||
526 | /*** Activation code ***/ |
||
527 | /** |
||
528 | * @return string |
||
529 | */ |
||
530 | public function getActivationCode() |
||
531 | { |
||
532 | return $this->activationCode; |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * @param string $activationCode |
||
537 | * |
||
538 | * @return UserEntity |
||
539 | */ |
||
540 | public function setActivationCode($activationCode) |
||
546 | |||
547 | /*** New email code ***/ |
||
548 | /** |
||
549 | * @return string |
||
550 | */ |
||
551 | public function getNewEmailCode() |
||
552 | { |
||
553 | return $this->newEmailCode; |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * @param string $newEmailCode |
||
558 | * |
||
559 | * @return UserEntity |
||
560 | */ |
||
561 | public function setNewEmailCode($newEmailCode) |
||
562 | { |
||
563 | $this->newEmailCode = $newEmailCode; |
||
564 | |||
565 | return $this; |
||
566 | } |
||
567 | |||
568 | /*** Time last active ***/ |
||
569 | /** |
||
570 | * @return \DateTime |
||
571 | */ |
||
572 | public function getTimeLastActive() |
||
576 | |||
577 | /** |
||
578 | * @param $timeLastActive |
||
579 | * |
||
580 | * @return UserEntity |
||
581 | */ |
||
582 | public function setTimeLastActive(\Datetime $timeLastActive = null) |
||
588 | |||
589 | /*** Time reset password code expires ***/ |
||
590 | /** |
||
591 | * @return \DateTime |
||
592 | */ |
||
593 | public function getTimeResetPasswordCodeExpires() |
||
594 | { |
||
595 | return $this->timeResetPasswordCodeExpires; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * @param $timeResetPasswordCodeExpires |
||
600 | * |
||
601 | * @return UserEntity |
||
602 | */ |
||
603 | public function setTimeResetPasswordCodeExpires(\Datetime $timeResetPasswordCodeExpires = null) |
||
604 | { |
||
605 | $this->timeResetPasswordCodeExpires = $timeResetPasswordCodeExpires; |
||
606 | |||
607 | return $this; |
||
608 | } |
||
609 | |||
610 | /*** Time created ***/ |
||
611 | /** |
||
612 | * @return \DateTime |
||
613 | */ |
||
614 | public function getTimeCreated() |
||
618 | |||
619 | /** |
||
620 | * @param \DateTime $timeCreated |
||
621 | * |
||
622 | * @return UserEntity |
||
623 | */ |
||
624 | public function setTimeCreated(\Datetime $timeCreated) |
||
630 | |||
631 | /*** Time updated ***/ |
||
632 | /** |
||
633 | * @return \DateTime |
||
634 | */ |
||
635 | public function getTimeUpdated() |
||
639 | |||
640 | /** |
||
641 | * @param \DateTime $timeUpdated |
||
642 | * |
||
643 | * @return UserEntity |
||
644 | */ |
||
645 | public function setTimeUpdated(\DateTime $timeUpdated) |
||
651 | |||
652 | /*** Expired ***/ |
||
653 | /** |
||
654 | * @return bool |
||
655 | */ |
||
656 | public function getExpired() |
||
660 | |||
661 | /** |
||
662 | * @return bool |
||
663 | */ |
||
664 | public function isExpired() |
||
668 | |||
669 | /** |
||
670 | * @return bool |
||
671 | */ |
||
672 | public function isAccountNonExpired() |
||
676 | |||
677 | /*** Credentials expired ***/ |
||
678 | /** |
||
679 | * @return bool |
||
680 | */ |
||
681 | public function getCredentialsExpired() |
||
685 | |||
686 | /** |
||
687 | * @return bool |
||
688 | */ |
||
689 | public function isCredentialsExpired() |
||
693 | |||
694 | /** |
||
695 | * @return bool |
||
696 | */ |
||
697 | public function isCredentialsNonExpired() |
||
698 | { |
||
699 | return !$this->getExpired(); |
||
700 | } |
||
701 | |||
702 | /*** Roles ***/ |
||
703 | /** |
||
704 | * @return array |
||
705 | */ |
||
706 | public function getRoles() |
||
707 | { |
||
708 | $roles = is_array($this->roles) |
||
709 | ? $this->roles |
||
710 | : array() |
||
711 | ; |
||
712 | $roles[] = 'ROLE_USER'; |
||
713 | |||
714 | return (array) array_unique($roles, SORT_REGULAR); |
||
715 | } |
||
716 | |||
717 | /** |
||
718 | * @param array $roles |
||
719 | * |
||
720 | * @return UserEntity |
||
721 | */ |
||
722 | public function setRoles(array $roles) |
||
723 | { |
||
724 | $this->roles = $roles; |
||
725 | |||
726 | return $this; |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * @param string $role |
||
731 | * |
||
732 | * @return bool |
||
733 | */ |
||
734 | public function hasRole($role) |
||
735 | { |
||
736 | return in_array( |
||
737 | $role, |
||
738 | $this->getRoles() |
||
739 | ); |
||
740 | } |
||
741 | |||
742 | /*** Profile ***/ |
||
743 | /** |
||
744 | * @return ProfileEntity |
||
745 | */ |
||
746 | public function getProfile() |
||
747 | { |
||
748 | return $this->profile; |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * @param ProfileEntity $profile |
||
753 | * |
||
754 | * @return UserEntity |
||
755 | */ |
||
756 | public function setProfile(ProfileEntity $profile) |
||
757 | { |
||
758 | $this->profile = $profile; |
||
759 | |||
760 | $this->getProfile()->setUser($this); |
||
761 | |||
762 | return $this; |
||
763 | } |
||
764 | |||
765 | /*** Posts ***/ |
||
766 | /** |
||
767 | * @return array |
||
768 | */ |
||
769 | public function getPosts() |
||
770 | { |
||
771 | return $this->posts->toArray(); |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * @param ArrayCollection $posts |
||
776 | * |
||
777 | * @return UserEntity |
||
778 | */ |
||
779 | public function setPosts($posts) |
||
780 | { |
||
781 | $this->posts = $posts; |
||
782 | |||
783 | return $this; |
||
784 | } |
||
785 | |||
786 | /*** User actions ***/ |
||
787 | /** |
||
788 | * @return array |
||
789 | */ |
||
790 | public function getUserActions() |
||
791 | { |
||
792 | return $this->userActions->toArray(); |
||
793 | } |
||
794 | |||
795 | /** |
||
796 | * @param $userActions |
||
797 | * |
||
798 | * @return UserEntity |
||
799 | */ |
||
800 | public function setUserActions($userActions) |
||
801 | { |
||
802 | $this->userActions = $userActions; |
||
803 | |||
804 | return $this; |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * @param AdvancedUserInterface $user |
||
809 | * |
||
810 | * @return bool |
||
811 | */ |
||
812 | public function isEqualTo(AdvancedUserInterface $user) |
||
813 | { |
||
814 | if (!($user instanceof AdvancedUserInterface)) { |
||
815 | return false; |
||
816 | } |
||
817 | |||
818 | if ($this->getPassword() !== $user->getPassword()) { |
||
819 | return false; |
||
820 | } |
||
821 | |||
822 | if ($this->getSalt() !== $user->getSalt()) { |
||
823 | return false; |
||
824 | } |
||
825 | |||
826 | if ($this->getUsername() !== $user->getUsername()) { |
||
827 | return false; |
||
828 | } |
||
829 | |||
830 | if (serialize($this->getRoles()) !== serialize($user->getRoles())) { |
||
831 | return false; |
||
832 | } |
||
833 | |||
834 | return true; |
||
835 | } |
||
836 | |||
837 | /** |
||
838 | * {@inheritdoc} |
||
839 | */ |
||
840 | public function eraseCredentials() |
||
841 | { |
||
842 | $this->setPlainPassword(null); |
||
843 | } |
||
844 | |||
845 | /** |
||
846 | * @return string |
||
847 | */ |
||
848 | public function serialize() |
||
858 | |||
859 | /** |
||
860 | * {@inheritdoc} |
||
861 | */ |
||
862 | public function unserialize($serialized) |
||
863 | { |
||
864 | list( |
||
865 | $this->id, |
||
866 | $this->username, |
||
867 | $this->email, |
||
868 | $this->password, |
||
869 | $this->salt) = unserialize($serialized); |
||
870 | } |
||
871 | |||
872 | /** |
||
873 | * @return string |
||
874 | */ |
||
875 | public function __toString() |
||
876 | { |
||
877 | return $this->getUsername(); |
||
878 | } |
||
879 | |||
880 | /** |
||
881 | * @param $allData Show all the data for this user. |
||
882 | * |
||
883 | * @return array |
||
884 | */ |
||
885 | public function toArray($allData = true) |
||
886 | { |
||
887 | $data = array( |
||
888 | 'username' => $this->getUsername(), |
||
889 | 'email' => $this->getEmail(), |
||
890 | 'title' => $this->getProfile()->getTitle(), |
||
891 | 'first_name' => $this->getProfile()->getFirstName(), |
||
892 | 'middle_name' => $this->getProfile()->getMiddleName(), |
||
893 | 'last_name' => $this->getProfile()->getLastName(), |
||
894 | 'full_name' => $this->getProfile()->getFullName(), |
||
895 | 'gender' => $this->getProfile()->getGender(), |
||
896 | 'birthdate' => $this->getProfile()->getBirthdate()->format('Y-m-d'), |
||
897 | 'image_url' => $this->getProfile()->getImageUrl(), |
||
898 | ); |
||
899 | |||
900 | if ($allData) { |
||
901 | $data = array_merge($data, array( |
||
902 | 'id' => $this->getId(), |
||
903 | 'time_created' => $this->getTimeCreated()->format(DATE_ATOM), |
||
904 | )); |
||
905 | } |
||
906 | |||
907 | return $data; |
||
908 | } |
||
909 | |||
910 | /** |
||
911 | * @ORM\PreUpdate |
||
912 | */ |
||
913 | public function preUpdate() |
||
917 | |||
918 | /** |
||
919 | * @ORM\PrePersist |
||
920 | */ |
||
921 | public function prePersist() |
||
926 | } |
||
927 |