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 | * @var array |
||
55 | * |
||
56 | * @ORM\Column(name="roles", type="json_array", nullable=true) |
||
57 | */ |
||
58 | protected $roles; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | * |
||
63 | * @ORM\Column(name="password", type="string", length=255) |
||
64 | */ |
||
65 | protected $password; |
||
66 | |||
67 | /** |
||
68 | * Used only when saving the user. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $plainPassword; |
||
73 | |||
74 | /** |
||
75 | * Used only when saving a new password. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $oldPassword; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | * |
||
84 | * @ORM\Column(name="salt", type="string", length=255, nullable=true) |
||
85 | */ |
||
86 | protected $salt; |
||
87 | |||
88 | /** |
||
89 | * Used for emails & co. |
||
90 | * |
||
91 | * @var string |
||
92 | * |
||
93 | * @ORM\Column(name="token", type="string", length=255, nullable=true) |
||
94 | */ |
||
95 | protected $token; |
||
96 | |||
97 | /** |
||
98 | * Used for authentification & co. |
||
99 | * |
||
100 | * @var string |
||
101 | * |
||
102 | * @ORM\Column(name="access_token", type="string", length=255, nullable=true) |
||
103 | */ |
||
104 | protected $accessToken; |
||
105 | |||
106 | /** |
||
107 | * @var bool |
||
108 | * |
||
109 | * @ORM\Column(name="enabled", type="boolean") |
||
110 | */ |
||
111 | protected $enabled = true; |
||
112 | |||
113 | /** |
||
114 | * @var bool |
||
115 | * |
||
116 | * @ORM\Column(name="locked", type="boolean") |
||
117 | */ |
||
118 | protected $locked = false; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | * |
||
123 | * @ORM\Column(name="reset_password_code", type="string", length=255, nullable=true, unique=true) |
||
124 | */ |
||
125 | protected $resetPasswordCode; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | * |
||
130 | * @ORM\Column(name="activation_code", type="string", length=255, nullable=true, unique=true) |
||
131 | */ |
||
132 | protected $activationCode; |
||
133 | |||
134 | /** |
||
135 | * @var \DateTime |
||
136 | * |
||
137 | * @ORM\Column(name="time_last_active", type="datetime", nullable=true) |
||
138 | */ |
||
139 | protected $timeLastActive; |
||
140 | |||
141 | /** |
||
142 | * @var \DateTime |
||
143 | * |
||
144 | * @ORM\Column(name="time_created", type="datetime") |
||
145 | */ |
||
146 | protected $timeCreated; |
||
147 | |||
148 | /** |
||
149 | * @var \DateTime |
||
150 | * |
||
151 | * @ORM\Column(name="time_updated", type="datetime") |
||
152 | */ |
||
153 | protected $timeUpdated; |
||
154 | |||
155 | /** |
||
156 | * @ORM\OneToOne(targetEntity="Application\Entity\ProfileEntity", mappedBy="user", cascade={"all"}) |
||
157 | **/ |
||
158 | protected $profile; |
||
159 | |||
160 | /** |
||
161 | * @var ArrayCollection |
||
162 | * |
||
163 | * @ORM\OneToMany(targetEntity="Application\Entity\PostEntity", mappedBy="user", cascade={"all"}) |
||
164 | */ |
||
165 | protected $posts; |
||
166 | |||
167 | /** |
||
168 | * @var ArrayCollection |
||
169 | * |
||
170 | * @ORM\OneToMany(targetEntity="Application\Entity\UserActionEntity", mappedBy="user", cascade={"all"}) |
||
171 | */ |
||
172 | protected $userActions; |
||
173 | |||
174 | /** |
||
175 | * Otherwise known as: userExpired / accountExpired. |
||
176 | * |
||
177 | * @var bool |
||
178 | */ |
||
179 | protected $expired = false; |
||
180 | |||
181 | /** |
||
182 | * @var bool |
||
183 | */ |
||
184 | protected $credentialsExpired = false; |
||
185 | |||
186 | /** |
||
187 | * The constructor. |
||
188 | */ |
||
189 | public function __construct() |
||
214 | |||
215 | /*** Id ***/ |
||
216 | /** |
||
217 | * @return int |
||
218 | */ |
||
219 | public function getId() |
||
223 | |||
224 | /** |
||
225 | * @param int $id |
||
226 | * |
||
227 | * @return UserEntity |
||
228 | */ |
||
229 | public function setId($id) |
||
235 | |||
236 | /*** Locale ***/ |
||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getLocale() |
||
244 | |||
245 | /** |
||
246 | * @param string $locale |
||
247 | * |
||
248 | * @return UserEntity |
||
249 | */ |
||
250 | public function setLocale($locale) |
||
256 | |||
257 | /*** Username ***/ |
||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getUsername() |
||
265 | |||
266 | /** |
||
267 | * @param string $username |
||
268 | * |
||
269 | * @return UserEntity |
||
270 | */ |
||
271 | public function setUsername($username) |
||
277 | |||
278 | /*** Email ***/ |
||
279 | /** |
||
280 | * @return string |
||
281 | */ |
||
282 | public function getEmail() |
||
286 | |||
287 | /** |
||
288 | * @param string $email |
||
289 | * |
||
290 | * @return UserEntity |
||
291 | */ |
||
292 | public function setEmail($email) |
||
298 | |||
299 | /*** Password ***/ |
||
300 | /** |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getPassword() |
||
307 | |||
308 | /** |
||
309 | * @param string $password |
||
310 | * |
||
311 | * @return UserEntity |
||
312 | */ |
||
313 | public function setPassword($password) |
||
321 | |||
322 | /*** Plain password ***/ |
||
323 | /** |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getPlainPassword() |
||
330 | |||
331 | /** |
||
332 | * @param string $plainPassword |
||
333 | * @param EncoderFactory $encoderFactory |
||
334 | * |
||
335 | * @return UserEntity |
||
336 | */ |
||
337 | public function setPlainPassword($plainPassword, EncoderFactory $encoderFactory = null) |
||
354 | |||
355 | /*** Old password ***/ |
||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getOldPassword() |
||
363 | |||
364 | /** |
||
365 | * @param string $oldPassword |
||
366 | * |
||
367 | * @return UserEntity |
||
368 | */ |
||
369 | public function setOldPassword($oldPassword) |
||
375 | |||
376 | /*** Salt ***/ |
||
377 | /** |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getSalt() |
||
384 | |||
385 | /** |
||
386 | * @param string $salt |
||
387 | * |
||
388 | * @return UserEntity |
||
389 | */ |
||
390 | public function setSalt($salt) |
||
396 | |||
397 | /*** Token ***/ |
||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | public function getToken() |
||
405 | |||
406 | /** |
||
407 | * @param string $token |
||
408 | * |
||
409 | * @return UserEntity |
||
410 | */ |
||
411 | public function setToken($token) |
||
417 | |||
418 | /*** Access Token ***/ |
||
419 | /** |
||
420 | * @return string |
||
421 | */ |
||
422 | public function getAccessToken() |
||
426 | |||
427 | /** |
||
428 | * @param string $accessToken |
||
429 | * |
||
430 | * @return UserEntity |
||
431 | */ |
||
432 | public function setAccessToken($accessToken) |
||
438 | |||
439 | /*** Enabled ***/ |
||
440 | /** |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function getEnabled() |
||
447 | |||
448 | /** |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function isEnabled() |
||
455 | |||
456 | /** |
||
457 | * @param bool $enabled |
||
458 | */ |
||
459 | public function setEnabled($enabled) |
||
465 | |||
466 | /** |
||
467 | * @return UserEntity |
||
468 | */ |
||
469 | public function enable() |
||
475 | |||
476 | /** |
||
477 | * @return UserEntity |
||
478 | */ |
||
479 | public function disable() |
||
485 | |||
486 | /*** Locked ***/ |
||
487 | /** |
||
488 | * @return bool |
||
489 | */ |
||
490 | public function getLocked() |
||
494 | |||
495 | /** |
||
496 | * @return bool |
||
497 | */ |
||
498 | public function isLocked() |
||
502 | |||
503 | /** |
||
504 | * @param bool $locked |
||
505 | * |
||
506 | * @return UserEntity |
||
507 | */ |
||
508 | public function setLocked($locked) |
||
514 | |||
515 | /** |
||
516 | * @return UserEntity |
||
517 | */ |
||
518 | public function lock() |
||
524 | |||
525 | /** |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function isAccountNonLocked() |
||
532 | |||
533 | /*** Reset password code ***/ |
||
534 | /** |
||
535 | * @return string |
||
536 | */ |
||
537 | public function getResetPasswordCode() |
||
541 | |||
542 | /** |
||
543 | * @param string $resetPasswordCode |
||
544 | * |
||
545 | * @return UserEntity |
||
546 | */ |
||
547 | public function setResetPasswordCode($resetPasswordCode) |
||
553 | |||
554 | /*** Activation code ***/ |
||
555 | /** |
||
556 | * @return string |
||
557 | */ |
||
558 | public function getActivationCode() |
||
562 | |||
563 | /** |
||
564 | * @param string $activationCode |
||
565 | * |
||
566 | * @return UserEntity |
||
567 | */ |
||
568 | public function setActivationCode($activationCode) |
||
574 | |||
575 | /*** Time last active ***/ |
||
576 | /** |
||
577 | * @return \DateTime |
||
578 | */ |
||
579 | public function getTimeLastActive() |
||
583 | |||
584 | /** |
||
585 | * @param $timeLastActive |
||
586 | * |
||
587 | * @return UserEntity |
||
588 | */ |
||
589 | public function setTimeLastActive(\Datetime $timeLastActive = null) |
||
595 | |||
596 | /*** Time created ***/ |
||
597 | /** |
||
598 | * @return \DateTime |
||
599 | */ |
||
600 | public function getTimeCreated() |
||
604 | |||
605 | /** |
||
606 | * @param \DateTime $timeCreated |
||
607 | * |
||
608 | * @return UserEntity |
||
609 | */ |
||
610 | public function setTimeCreated(\Datetime $timeCreated) |
||
616 | |||
617 | /*** Time updated ***/ |
||
618 | /** |
||
619 | * @return \DateTime |
||
620 | */ |
||
621 | public function getTimeUpdated() |
||
625 | |||
626 | /** |
||
627 | * @param \DateTime $timeUpdated |
||
628 | * |
||
629 | * @return UserEntity |
||
630 | */ |
||
631 | public function setTimeUpdated(\DateTime $timeUpdated) |
||
637 | |||
638 | /*** Expired ***/ |
||
639 | /** |
||
640 | * @return bool |
||
641 | */ |
||
642 | public function getExpired() |
||
646 | |||
647 | /** |
||
648 | * @return bool |
||
649 | */ |
||
650 | public function isExpired() |
||
654 | |||
655 | /** |
||
656 | * @return bool |
||
657 | */ |
||
658 | public function isAccountNonExpired() |
||
662 | |||
663 | /*** Credentials expired ***/ |
||
664 | /** |
||
665 | * @return bool |
||
666 | */ |
||
667 | public function getCredentialsExpired() |
||
671 | |||
672 | /** |
||
673 | * @return bool |
||
674 | */ |
||
675 | public function isCredentialsExpired() |
||
679 | |||
680 | /** |
||
681 | * @return bool |
||
682 | */ |
||
683 | public function isCredentialsNonExpired() |
||
687 | |||
688 | /*** Roles ***/ |
||
689 | /** |
||
690 | * @return array |
||
691 | */ |
||
692 | public function getRoles() |
||
702 | |||
703 | /** |
||
704 | * @param array $roles |
||
705 | * |
||
706 | * @return UserEntity |
||
707 | */ |
||
708 | public function setRoles(array $roles) |
||
714 | |||
715 | /** |
||
716 | * @param string $role |
||
717 | * |
||
718 | * @return bool |
||
719 | */ |
||
720 | public function hasRole($role) |
||
727 | |||
728 | /*** Profile ***/ |
||
729 | /** |
||
730 | * @return ProfileEntity |
||
731 | */ |
||
732 | public function getProfile() |
||
736 | |||
737 | /** |
||
738 | * @param ProfileEntity $profile |
||
739 | * |
||
740 | * @return UserEntity |
||
741 | */ |
||
742 | public function setProfile(ProfileEntity $profile) |
||
750 | |||
751 | /*** Posts ***/ |
||
752 | /** |
||
753 | * @return array |
||
754 | */ |
||
755 | public function getPosts() |
||
759 | |||
760 | /** |
||
761 | * @param ArrayCollection $posts |
||
762 | * |
||
763 | * @return UserEntity |
||
764 | */ |
||
765 | public function setPosts($posts) |
||
771 | |||
772 | /*** User actions ***/ |
||
773 | /** |
||
774 | * @return array |
||
775 | */ |
||
776 | public function getUserActions() |
||
780 | |||
781 | /** |
||
782 | * @param $userActions |
||
783 | * |
||
784 | * @return UserEntity |
||
785 | */ |
||
786 | public function setUserActions($userActions) |
||
792 | |||
793 | /** |
||
794 | * @param AdvancedUserInterface $user |
||
795 | * |
||
796 | * @return bool |
||
797 | */ |
||
798 | public function isEqualTo(AdvancedUserInterface $user) |
||
818 | |||
819 | /** |
||
820 | */ |
||
821 | public function eraseCredentials() |
||
825 | |||
826 | /** |
||
827 | * @return string |
||
828 | */ |
||
829 | public function serialize() |
||
839 | |||
840 | /** |
||
841 | */ |
||
842 | public function unserialize($serialized) |
||
851 | |||
852 | /** |
||
853 | * @return string |
||
854 | */ |
||
855 | public function __toString() |
||
859 | |||
860 | /** |
||
861 | * @param $allData Show all the data for this user. |
||
862 | * |
||
863 | * @return array |
||
864 | */ |
||
865 | public function toArray($allData = true) |
||
889 | |||
890 | /** |
||
891 | * @ORM\PreUpdate |
||
892 | */ |
||
893 | public function preUpdate() |
||
897 | |||
898 | /** |
||
899 | * @ORM\PrePersist |
||
900 | */ |
||
901 | public function prePersist() |
||
906 | } |
||
907 |