Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class User implements UserInterface |
||
| 22 | { |
||
| 23 | use TimestampableTrait, ToggleableTrait; |
||
| 24 | |||
| 25 | /** @var mixed */ |
||
| 26 | protected $id; |
||
| 27 | |||
| 28 | /** @var string|null */ |
||
| 29 | protected $username; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Normalized representation of a username. |
||
| 33 | * |
||
| 34 | * @var string|null |
||
| 35 | */ |
||
| 36 | protected $usernameCanonical; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Random data that is used as an additional input to a function that hashes a password. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $salt; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Encrypted password. Must be persisted. |
||
| 47 | * |
||
| 48 | * @var string|null |
||
| 49 | */ |
||
| 50 | protected $password; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Password before encryption. Used for model validation. Must not be persisted. |
||
| 54 | * |
||
| 55 | * @var string|null |
||
| 56 | */ |
||
| 57 | protected $plainPassword; |
||
| 58 | |||
| 59 | /** @var \DateTimeInterface|null */ |
||
| 60 | protected $lastLogin; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Random string sent to the user email address in order to verify it |
||
| 64 | * |
||
| 65 | * @var string|null |
||
| 66 | */ |
||
| 67 | protected $emailVerificationToken; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Random string sent to the user email address in order to verify the password resetting request |
||
| 71 | * |
||
| 72 | * @var string|null |
||
| 73 | */ |
||
| 74 | protected $passwordResetToken; |
||
| 75 | |||
| 76 | /** @var \DateTimeInterface|null */ |
||
| 77 | protected $passwordRequestedAt; |
||
| 78 | |||
| 79 | /** @var \DateTimeInterface|null */ |
||
| 80 | protected $verifiedAt; |
||
| 81 | |||
| 82 | /** @var bool */ |
||
| 83 | protected $locked = false; |
||
| 84 | |||
| 85 | /** @var \DateTimeInterface|null */ |
||
| 86 | protected $expiresAt; |
||
| 87 | |||
| 88 | /** @var \DateTimeInterface|null */ |
||
| 89 | protected $credentialsExpireAt; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * We need at least one role to be able to authenticate |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $roles = [UserInterface::DEFAULT_ROLE]; |
||
| 97 | |||
| 98 | /** @var Collection|UserOAuth[] */ |
||
| 99 | protected $oauthAccounts; |
||
| 100 | |||
| 101 | /** @var string|null */ |
||
| 102 | protected $email; |
||
| 103 | |||
| 104 | /** @var string|null */ |
||
| 105 | protected $emailCanonical; |
||
| 106 | |||
| 107 | /** @var string|null */ |
||
| 108 | protected $encoderName; |
||
| 109 | |||
| 110 | public function __construct() |
||
| 119 | |||
| 120 | public function __toString(): string |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function getId() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function getEmail(): ?string |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function setEmail(?string $email): void |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function getEmailCanonical(): ?string |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function setEmailCanonical(?string $emailCanonical): void |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function getUsername(): ?string |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function setUsername(?string $username): void |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function getUsernameCanonical(): ?string |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function setUsernameCanonical(?string $usernameCanonical): void |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function getSalt(): string |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritdoc} |
||
| 207 | */ |
||
| 208 | public function getPlainPassword(): ?string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function setPlainPassword(?string $password): void |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function getPassword(): ?string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | public function setPassword(?string $password): void |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function getExpiresAt(): ?\DateTimeInterface |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function setExpiresAt(?\DateTimeInterface $date): void |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function getCredentialsExpireAt(): ?\DateTimeInterface |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function setCredentialsExpireAt(?\DateTimeInterface $date): void |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function getLastLogin(): ?\DateTimeInterface |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function setLastLogin(?\DateTimeInterface $time): void |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getEmailVerificationToken(): ?string |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function setEmailVerificationToken(?string $verificationToken): void |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | public function getPasswordResetToken(): ?string |
||
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function setPasswordResetToken(?string $passwordResetToken): void |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function isCredentialsNonExpired(): bool |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function isAccountNonExpired(): bool |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function setLocked(bool $locked): void |
||
| 340 | |||
| 341 | /** |
||
| 342 | * {@inheritdoc} |
||
| 343 | */ |
||
| 344 | public function isAccountNonLocked(): bool |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function hasRole(string $role): bool |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritdoc} |
||
| 359 | */ |
||
| 360 | public function addRole(string $role): void |
||
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | public function removeRole(string $role): void |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | public function getRoles(): array |
||
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc} |
||
| 389 | */ |
||
| 390 | public function isPasswordRequestNonExpired(\DateInterval $ttl): bool |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritdoc} |
||
| 404 | */ |
||
| 405 | public function getPasswordRequestedAt(): ?\DateTimeInterface |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritdoc} |
||
| 412 | */ |
||
| 413 | public function setPasswordRequestedAt(?\DateTimeInterface $date): void |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function isVerified(): bool |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | public function getVerifiedAt(): ?\DateTimeInterface |
||
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | public function setVerifiedAt(?\DateTimeInterface $verifiedAt): void |
||
| 441 | |||
| 442 | /** |
||
| 443 | * {@inheritdoc} |
||
| 444 | */ |
||
| 445 | public function eraseCredentials(): void |
||
| 449 | |||
| 450 | /** |
||
| 451 | * {@inheritdoc} |
||
| 452 | */ |
||
| 453 | public function getOAuthAccounts(): Collection |
||
| 457 | |||
| 458 | /** |
||
| 459 | * {@inheritdoc} |
||
| 460 | */ |
||
| 461 | public function getOAuthAccount(string $provider): ?UserOAuthInterface |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc} |
||
| 480 | */ |
||
| 481 | public function addOAuthAccount(UserOAuthInterface $oauth): void |
||
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | public function getEncoderName(): ?string |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | public function setEncoderName(?string $encoderName): void |
||
| 504 | |||
| 505 | /** |
||
| 506 | * The serialized data have to contain the fields used by the equals method and the username. |
||
| 507 | */ |
||
| 508 | public function serialize(): string |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $serialized |
||
| 524 | */ |
||
| 525 | public function unserialize($serialized): void |
||
| 543 | |||
| 544 | protected function hasExpired(?\DateTimeInterface $date): bool |
||
| 548 | } |
||
| 549 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.