Complex classes like UserEntityTrait 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 UserEntityTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait UserEntityTrait |
||
| 13 | { |
||
| 14 | protected $id; |
||
| 15 | protected $groupId; |
||
| 16 | /** @var GroupEntityInterface */ |
||
| 17 | protected $group; |
||
| 18 | /** @var SessionEntityInterface[] */ |
||
| 19 | protected $sessions; |
||
| 20 | protected $status; |
||
| 21 | protected $isAdmin; |
||
| 22 | protected $email = null; |
||
| 23 | protected $pendingEmail = null; |
||
| 24 | protected $deletedEmail = null; |
||
| 25 | protected $isEmailConfirmed = false; |
||
| 26 | protected $emailConfirmationToken; |
||
| 27 | protected $emailConfirmationTokenExpirationDatetime; |
||
| 28 | protected $lastEmailTokenGeneratedDatetime; |
||
| 29 | protected $password; |
||
| 30 | protected $passwordResetToken; |
||
| 31 | protected $passwordResetTokenExpirationDatetime; |
||
| 32 | protected $lastPasswordResetTokenGeneratedDatetime; |
||
| 33 | protected $creationDatetime; |
||
| 34 | protected $modificationDatetime; |
||
| 35 | /** @var GroupRepositoryInterface */ |
||
| 36 | protected $groupRepository; |
||
| 37 | /** @var SessionRepositoryInterface */ |
||
| 38 | protected $sessionRepository; |
||
| 39 | |||
| 40 | public function __construct( |
||
| 50 | |||
| 51 | /** @return int */ |
||
| 52 | public function getId() |
||
| 56 | |||
| 57 | public function setId(int $id): UserEntityInterface |
||
| 62 | |||
| 63 | /** @return int|null */ |
||
| 64 | public function getGroupId() |
||
| 68 | |||
| 69 | public function setGroupId(int $groupId = null): UserEntityInterface |
||
| 74 | |||
| 75 | /** @return GroupEntityInterface|null */ |
||
| 76 | public function getGroup() |
||
| 85 | |||
| 86 | public function setGroup(GroupEntityInterface $group = null): UserEntityInterface |
||
| 92 | |||
| 93 | /** @return SessionEntityInterface[] */ |
||
| 94 | public function getSessions() |
||
| 103 | |||
| 104 | /** @return string|null */ |
||
| 105 | public function getStatus() |
||
| 109 | |||
| 110 | public function setStatus(string $status): UserEntityInterface |
||
| 115 | |||
| 116 | public function isAdmin(): bool |
||
| 120 | |||
| 121 | public function setIsAdmin(bool $isAdmin): UserEntityInterface |
||
| 126 | |||
| 127 | public function isActive(): bool |
||
| 131 | |||
| 132 | public function getEmail(): string |
||
| 136 | |||
| 137 | public function setEmail(string $email): UserEntityInterface |
||
| 142 | |||
| 143 | /** @return string */ |
||
| 144 | public function getPendingEmail() |
||
| 148 | |||
| 149 | public function setPendingEmail(string $pendingEmail): UserEntityInterface |
||
| 154 | |||
| 155 | public function isEmailConfirmed(): bool |
||
| 159 | |||
| 160 | public function setIsEmailConfirmed(bool $isEmailConfirmed): UserEntityInterface |
||
| 165 | |||
| 166 | /** @return string */ |
||
| 167 | public function getEmailConfirmationToken() |
||
| 171 | |||
| 172 | public function setEmailConfirmationToken(string $emailConfirmationToken): UserEntityInterface |
||
| 177 | |||
| 178 | /** @return DateTime */ |
||
| 179 | public function getEmailConfirmationTokenExpirationDatetime() |
||
| 183 | |||
| 184 | public function setEmailConfirmationTokenExpirationDatetime( |
||
| 190 | |||
| 191 | /** @return DateTime */ |
||
| 192 | public function getLastEmailTokenGeneratedDatetime() |
||
| 196 | |||
| 197 | public function setLastEmailTokenGeneratedDatetime( |
||
| 203 | |||
| 204 | public function canGenerateNewEmailConfirmationToken(): bool |
||
| 212 | |||
| 213 | public function generateEmailConfirmationToken(): UserEntityInterface |
||
| 221 | |||
| 222 | /** @return string */ |
||
| 223 | public function getDeletedEmail() |
||
| 227 | |||
| 228 | public function setDeletedEmail(string $deletedEmail): UserEntityInterface |
||
| 233 | |||
| 234 | /** @return string */ |
||
| 235 | public function getPassword() |
||
| 239 | |||
| 240 | public function setPassword(string $password): UserEntityInterface |
||
| 246 | |||
| 247 | /** @return string */ |
||
| 248 | public function getPasswordResetToken() |
||
| 252 | |||
| 253 | public function setPasswordResetToken(string $passwordResetToken): UserEntityInterface |
||
| 258 | |||
| 259 | /** @return DateTime */ |
||
| 260 | public function getPasswordResetTokenExpirationDatetime() |
||
| 264 | |||
| 265 | public function setPasswordResetTokenExpirationDatetime( |
||
| 271 | |||
| 272 | /** @return DateTime */ |
||
| 273 | public function getLastPasswordResetTokenGeneratedDatetime() |
||
| 277 | |||
| 278 | public function setLastPasswordResetTokenGeneratedDatetime( |
||
| 284 | |||
| 285 | public function canGenerateNewResetPasswordToken(): bool |
||
| 293 | |||
| 294 | public function generatePasswordResetToken(): UserEntityInterface |
||
| 302 | |||
| 303 | protected function hashPassword(): UserEntityInterface |
||
| 311 | |||
| 312 | public function testPassword(string $password): bool |
||
| 316 | |||
| 317 | public function getCreationDatetime(): DateTime |
||
| 321 | |||
| 322 | public function setCreationDatetime(DateTime $creationDatetime): UserEntityInterface |
||
| 327 | |||
| 328 | public function getModificationDatetime(): DateTime |
||
| 332 | |||
| 333 | public function setModificationDatetime(DateTime $modificationDatetime): UserEntityInterface |
||
| 338 | |||
| 339 | public function setGroupRepository(GroupRepositoryInterface $groupRepository): UserEntityInterface |
||
| 344 | |||
| 345 | public function setSessionRepository(SessionRepositoryInterface $sessionRepository): UserEntityInterface |
||
| 350 | |||
| 351 | public function toArray(): array |
||
| 377 | |||
| 378 | public function jsonSerialize() |
||
| 394 | } |
||
| 395 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: