Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 13 | trait UserEntityTrait |
||
| 14 | { |
||
| 15 | protected $id; |
||
| 16 | protected $groupId; |
||
| 17 | /** @var GroupEntityInterface */ |
||
| 18 | protected $group; |
||
| 19 | /** @var SessionEntityInterface[] */ |
||
| 20 | protected $sessions; |
||
| 21 | protected $status; |
||
| 22 | protected $isAdmin; |
||
| 23 | protected $email = null; |
||
| 24 | protected $pendingEmail = null; |
||
| 25 | protected $deletedEmail = null; |
||
| 26 | protected $isEmailConfirmed = false; |
||
| 27 | protected $emailConfirmationToken; |
||
| 28 | protected $emailConfirmationTokenAttempts; |
||
| 29 | protected $emailConfirmationTokenExpirationDatetime; |
||
| 30 | protected $lastEmailTokenGeneratedDatetime; |
||
| 31 | protected $password; |
||
| 32 | protected $passwordResetToken; |
||
| 33 | protected $passwordResetTokenExpirationDatetime; |
||
| 34 | protected $lastPasswordResetTokenGeneratedDatetime; |
||
| 35 | protected $creationDatetime; |
||
| 36 | protected $modificationDatetime; |
||
| 37 | /** @var GroupRepositoryInterface */ |
||
| 38 | protected $groupRepository; |
||
| 39 | /** @var SessionRepositoryInterface */ |
||
| 40 | protected $sessionRepository; |
||
| 41 | |||
| 42 | public function __construct( |
||
| 52 | |||
| 53 | /** @return int */ |
||
| 54 | public function getId() |
||
| 58 | |||
| 59 | public function setId(int $id): UserEntityInterface |
||
| 64 | |||
| 65 | /** @return int|null */ |
||
| 66 | public function getGroupId() |
||
| 70 | |||
| 71 | public function setGroupId(int $groupId = null): UserEntityInterface |
||
| 76 | |||
| 77 | /** @return GroupEntityInterface|null */ |
||
| 78 | public function getGroup() |
||
| 87 | |||
| 88 | public function setGroup(GroupEntityInterface $group = null): UserEntityInterface |
||
| 94 | |||
| 95 | /** @return SessionEntityInterface[] */ |
||
| 96 | public function getSessions() |
||
| 105 | |||
| 106 | /** @return string|null */ |
||
| 107 | public function getStatus() |
||
| 111 | |||
| 112 | public function setStatus(string $status): UserEntityInterface |
||
| 117 | |||
| 118 | public function isAdmin(): bool |
||
| 122 | |||
| 123 | public function setIsAdmin(bool $isAdmin): UserEntityInterface |
||
| 128 | |||
| 129 | public function isActive(): bool |
||
| 133 | |||
| 134 | public function getEmail(): string |
||
| 138 | |||
| 139 | public function setEmail(string $email): UserEntityInterface |
||
| 144 | |||
| 145 | /** @return string */ |
||
| 146 | public function getPendingEmail() |
||
| 150 | |||
| 151 | public function setPendingEmail(string $pendingEmail): UserEntityInterface |
||
| 156 | |||
| 157 | public function isEmailConfirmed(): bool |
||
| 161 | |||
| 162 | public function setIsEmailConfirmed(bool $isEmailConfirmed): UserEntityInterface |
||
| 167 | |||
| 168 | /** @return string */ |
||
| 169 | public function getEmailConfirmationToken() |
||
| 173 | |||
| 174 | public function setEmailConfirmationToken(string $emailConfirmationToken = null): UserEntityInterface |
||
| 179 | |||
| 180 | /** @return int */ |
||
| 181 | public function getEmailConfirmationTokenAttempts() |
||
| 185 | |||
| 186 | public function setEmailConfirmationTokenAttempts(int $emailConfirmationTokenAttempts = 0): UserEntityInterface |
||
| 191 | |||
| 192 | /** @return DateTime */ |
||
| 193 | public function getEmailConfirmationTokenExpirationDatetime() |
||
| 197 | |||
| 198 | public function setEmailConfirmationTokenExpirationDatetime( |
||
| 204 | |||
| 205 | /** @return DateTime */ |
||
| 206 | public function getLastEmailTokenGeneratedDatetime() |
||
| 210 | |||
| 211 | public function setLastEmailTokenGeneratedDatetime( |
||
| 217 | |||
| 218 | public function hasEmailConfirmationTokenCooldownExpired(): bool |
||
| 223 | |||
| 224 | public function hasEmailConfirmationTokenExpired(): bool |
||
| 228 | |||
| 229 | public function hasTooManyEmailConfirmationTokenAttempts(): bool |
||
| 233 | |||
| 234 | View Code Duplication | public function generateEmailConfirmationToken($token = null, DateInterval $expiration = null): UserEntityInterface |
|
| 235 | { |
||
| 236 | if (null === $expiration) { |
||
| 237 | $expiration = new DateInterval('P1D'); |
||
| 238 | } |
||
| 239 | $this->setEmailConfirmationTokenAttempts(0); |
||
| 240 | $this->setEmailConfirmationToken(null === $token ? Token::generate(40) : $token); |
||
| 241 | $this->setEmailConfirmationTokenExpirationDatetime((new DateTime())->add($expiration)); |
||
| 242 | $this->setLastEmailTokenGeneratedDatetime(new DateTime()); |
||
| 243 | return $this; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** @return string */ |
||
| 247 | public function getDeletedEmail() |
||
| 251 | |||
| 252 | public function setDeletedEmail(string $deletedEmail): UserEntityInterface |
||
| 257 | |||
| 258 | /** @return string */ |
||
| 259 | public function getPassword() |
||
| 263 | |||
| 264 | public function setPassword(string $password): UserEntityInterface |
||
| 270 | |||
| 271 | /** @return string */ |
||
| 272 | public function getPasswordResetToken() |
||
| 276 | |||
| 277 | public function setPasswordResetToken(string $passwordResetToken): UserEntityInterface |
||
| 282 | |||
| 283 | /** @return DateTime */ |
||
| 284 | public function getPasswordResetTokenExpirationDatetime() |
||
| 288 | |||
| 289 | public function setPasswordResetTokenExpirationDatetime( |
||
| 295 | |||
| 296 | /** @return DateTime */ |
||
| 297 | public function getLastPasswordResetTokenGeneratedDatetime() |
||
| 301 | |||
| 302 | public function setLastPasswordResetTokenGeneratedDatetime( |
||
| 308 | |||
| 309 | public function hasPasswordResetTokenCooldownExpired(): bool |
||
| 310 | { |
||
| 311 | return (new DateTime())->getTimestamp() - $this->getLastPasswordResetTokenGeneratedDatetime()->getTimestamp() > |
||
| 312 | UserEntityInterface::EMAIL_COOLDOWN; |
||
| 313 | } |
||
| 314 | |||
| 315 | View Code Duplication | public function generatePasswordResetToken($token = null, DateInterval $expiration = null): UserEntityInterface |
|
| 316 | { |
||
| 317 | if (null === $expiration) { |
||
| 318 | $expiration = new DateInterval('P1D'); |
||
| 319 | } |
||
| 320 | |||
| 321 | $this->setPasswordResetToken(null === $token ? Token::generate(40) : $token); |
||
| 322 | $this->setPasswordResetTokenExpirationDatetime((new DateTime())->add($expiration)); |
||
| 323 | $this->setLastPasswordResetTokenGeneratedDatetime(new DateTime()); |
||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | protected function hashPassword(): UserEntityInterface |
||
| 335 | |||
| 336 | public function testPassword(string $password): bool |
||
| 340 | |||
| 341 | public function getCreationDatetime(): DateTime |
||
| 345 | |||
| 346 | public function setCreationDatetime(DateTime $creationDatetime): UserEntityInterface |
||
| 351 | |||
| 352 | public function getModificationDatetime(): DateTime |
||
| 356 | |||
| 357 | public function setModificationDatetime(DateTime $modificationDatetime): UserEntityInterface |
||
| 362 | |||
| 363 | public function setGroupRepository(GroupRepositoryInterface $groupRepository): UserEntityInterface |
||
| 368 | |||
| 369 | public function setSessionRepository(SessionRepositoryInterface $sessionRepository): UserEntityInterface |
||
| 374 | |||
| 375 | public function toArray(): array |
||
| 401 | |||
| 402 | public function jsonSerialize() |
||
| 418 | } |
||
| 419 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: