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 |
||
| 8 | trait UserEntityTrait |
||
| 9 | { |
||
| 10 | protected $id; |
||
| 11 | protected $status = UserEntityInterface::STATUS_ACTIVE; |
||
| 12 | protected $isAdmin; |
||
| 13 | protected $email = null; |
||
| 14 | protected $pendingEmail = null; |
||
| 15 | protected $deletedEmail = null; |
||
| 16 | protected $isEmailConfirmed = false; |
||
| 17 | protected $emailConfirmationToken; |
||
| 18 | protected $emailConfirmationTokenExpirationDatetime; |
||
| 19 | protected $lastEmailTokenGeneratedDatetime; |
||
| 20 | protected $password; |
||
| 21 | protected $passwordResetToken; |
||
| 22 | protected $passwordResetTokenExpirationDatetime; |
||
| 23 | protected $lastPasswordResetTokenGeneratedDatetime; |
||
| 24 | protected $creationDatetime; |
||
| 25 | protected $modificationDatetime; |
||
| 26 | |||
| 27 | public function __construct() |
||
| 32 | |||
| 33 | public function getId(): int |
||
| 37 | |||
| 38 | public function setId(int $id): UserEntityInterface |
||
| 43 | |||
| 44 | public function getStatus(): string |
||
| 48 | |||
| 49 | public function setStatus(string $status): UserEntityInterface |
||
| 54 | |||
| 55 | public function isAdmin(): boolean |
||
| 59 | |||
| 60 | public function setIsAdmin(boolean $isAdmin): UserEntityInterface |
||
| 65 | |||
| 66 | public function isActive(): boolean |
||
| 70 | |||
| 71 | public function getEmail(): string |
||
| 75 | |||
| 76 | public function setEmail(string $email): UserEntityInterface |
||
| 81 | |||
| 82 | public function getPendingEmail(): string |
||
| 86 | |||
| 87 | public function setPendingEmail(string $pendingEmail): UserEntityInterface |
||
| 92 | |||
| 93 | public function isEmailConfirmed(): boolean |
||
| 97 | |||
| 98 | public function setIsEmailConfirmed(boolean $isEmailConfirmed): UserEntityInterface |
||
| 103 | |||
| 104 | public function getEmailConfirmationToken(): string |
||
| 108 | |||
| 109 | public function setEmailConfirmationToken(string $emailConfirmationToken): UserEntityInterface |
||
| 114 | |||
| 115 | public function getEmailConfirmationTokenExpirationDatetime(): DateTime |
||
| 119 | |||
| 120 | public function setEmailConfirmationTokenExpirationDatetime( |
||
| 126 | |||
| 127 | public function getLastEmailTokenGeneratedDatetime(): DateTime |
||
| 131 | |||
| 132 | public function setLastEmailTokenGeneratedDatetime( |
||
| 138 | |||
| 139 | View Code Duplication | public function canGenerateNewEmailConfirmationToken(): boolean |
|
| 147 | |||
| 148 | public function generateEmailConfirmationToken(): UserEntityInterface |
||
| 156 | |||
| 157 | public function getDeletedEmail(): string |
||
| 161 | |||
| 162 | public function setDeletedEmail(string $deletedEmail): UserEntityInterface |
||
| 167 | |||
| 168 | public function getPassword(): string |
||
| 172 | |||
| 173 | public function setPassword(string $password): UserEntityInterface |
||
| 179 | |||
| 180 | public function getPasswordResetToken(): string |
||
| 184 | |||
| 185 | public function setPasswordResetToken(string $passwordResetToken): UserEntityInterface |
||
| 190 | |||
| 191 | public function getPasswordResetTokenExpirationDatetime(): DateTime |
||
| 195 | |||
| 196 | public function setPasswordResetTokenExpirationDatetime( |
||
| 202 | |||
| 203 | public function getLastPasswordResetTokenGeneratedDatetime(): DateTime |
||
| 207 | |||
| 208 | public function setLastPasswordResetTokenGeneratedDatetime( |
||
| 214 | |||
| 215 | View Code Duplication | public function canGenerateNewResetPasswordToken(): boolean |
|
| 223 | |||
| 224 | public function generatePasswordResetToken(): UserEntityInterface |
||
| 232 | |||
| 233 | protected function hashPassword(): UserEntityInterface |
||
| 241 | |||
| 242 | public function testPassword(string $password): boolean |
||
| 246 | |||
| 247 | public function getCreationDatetime(): DateTime |
||
| 251 | |||
| 252 | public function setCreationDatetime(DateTime $creationDatetime): UserEntityInterface |
||
| 257 | |||
| 258 | public function getModificationDatetime(): DateTime |
||
| 262 | |||
| 263 | public function setModificationDatetime(DateTime $modificationDatetime): UserEntityInterface |
||
| 268 | |||
| 269 | public function toArray(): array |
||
| 294 | |||
| 295 | public function jsonSerialize(): array |
||
| 299 | } |
||
| 300 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..