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 AbstractUser 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 AbstractUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class AbstractUser extends Content implements |
||
| 31 | UserInterface, |
||
| 32 | AuthenticatableInterface, |
||
| 33 | AuthorizableInterface, |
||
| 34 | GroupableInterface, |
||
| 35 | ConfigurableInterface |
||
| 36 | { |
||
| 37 | use AuthorizableTrait; |
||
| 38 | use GroupableTrait; |
||
| 39 | use ConfigurableTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The username should be unique and mandatory. |
||
| 43 | * @var string $username |
||
| 44 | */ |
||
| 45 | private $username = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The password is stored encrypted in the database. |
||
| 49 | * @var string $password |
||
| 50 | */ |
||
| 51 | private $password; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string $email |
||
| 55 | */ |
||
| 56 | private $email; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var boolean $active |
||
| 60 | */ |
||
| 61 | private $active = true; |
||
|
|
|||
| 62 | |||
| 63 | /** |
||
| 64 | * The date of the latest (successful) login |
||
| 65 | * @var DateTime|null $lastLoginDate |
||
| 66 | */ |
||
| 67 | private $lastLoginDate; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string $lastLoginIp |
||
| 71 | */ |
||
| 72 | private $lastLoginIp; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The date of the latest password change |
||
| 76 | * @var DateTime|null |
||
| 77 | */ |
||
| 78 | private $lastPasswordDate; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string $lastPasswordIp |
||
| 82 | */ |
||
| 83 | private $lastPasswordIp; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * If the login token is set (not empty), then the user should be prompted to |
||
| 87 | * reset his password after login / enter the token to continue |
||
| 88 | * @var string $loginToken |
||
| 89 | */ |
||
| 90 | private $loginToken = ''; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * IndexableTrait > key() |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function key() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Force a lowercase username |
||
| 104 | * |
||
| 105 | * @param string $username The username (also the login name). |
||
| 106 | * @throws InvalidArgumentException If the username is not a string. |
||
| 107 | * @return User Chainable |
||
| 108 | */ |
||
| 109 | public function setUsername($username) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function username() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $email The user email. |
||
| 130 | * @throws InvalidArgumentException If the email is not a string. |
||
| 131 | * @return User Chainable |
||
| 132 | */ |
||
| 133 | public function setEmail($email) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function email() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string|null $password The user password. Encrypted in storage. |
||
| 154 | * @throws InvalidArgumentException If the password is not a string (or null, to reset). |
||
| 155 | * @return UserInterface Chainable |
||
| 156 | */ |
||
| 157 | public function setPassword($password) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function password() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param boolean $active The active flag. |
||
| 182 | * @return UserInterface Chainable |
||
| 183 | */ |
||
| 184 | public function setActive($active) |
||
| 189 | /** |
||
| 190 | * @return boolean |
||
| 191 | */ |
||
| 192 | public function active() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string|DateTime|null $lastLoginDate The last login date. |
||
| 199 | * @throws InvalidArgumentException If the ts is not a valid date/time. |
||
| 200 | * @return AbstractUser Chainable |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function setLastLoginDate($lastLoginDate) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return DateTime|null |
||
| 228 | */ |
||
| 229 | public function lastLoginDate() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string|integer|null $ip The last login IP address. |
||
| 236 | * @throws InvalidArgumentException If the IP is not an IP string, an integer, or null. |
||
| 237 | * @return UserInterface Chainable |
||
| 238 | */ |
||
| 239 | View Code Duplication | public function setLastLoginIp($ip) |
|
| 256 | /** |
||
| 257 | * Get the last login IP in x.x.x.x format |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function lastLoginIp() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string|DateTime|null $lastPasswordDate The last password date. |
||
| 267 | * @throws InvalidArgumentException If the passsword date is not a valid DateTime. |
||
| 268 | * @return UserInterface Chainable |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function setLastPasswordDate($lastPasswordDate) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @return DateTime |
||
| 296 | */ |
||
| 297 | public function lastPasswordDate() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param integer|string|null $ip The last password IP. |
||
| 304 | * @throws InvalidArgumentException If the IP is not null, an integer or an IP string. |
||
| 305 | * @return UserInterface Chainable |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function setLastPasswordIp($ip) |
|
| 324 | /** |
||
| 325 | * Get the last password change IP in x.x.x.x format |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function lastPasswordIp() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $token The login token. |
||
| 336 | * @throws InvalidArgumentException If the token is not a string. |
||
| 337 | * @return UserInterface Chainable |
||
| 338 | */ |
||
| 339 | public function setLoginToken($token) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function loginToken() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Attempt to log in a user with a username + password. |
||
| 360 | * |
||
| 361 | * @param string $username Username. |
||
| 362 | * @param string $password Password. |
||
| 363 | * @throws InvalidArgumentException If username or password is not a string. |
||
| 364 | * @return boolean Login success / failure. |
||
| 365 | */ |
||
| 366 | public function authenticate($username, $password) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @throws Exception If trying to save a user to session without a ID. |
||
| 407 | * @return UserInterface Chainable |
||
| 408 | */ |
||
| 409 | public function saveToSession() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Log in the user (in session) |
||
| 422 | * |
||
| 423 | * Called when the authentication is successful. |
||
| 424 | * |
||
| 425 | * @return boolean Success / Failure |
||
| 426 | */ |
||
| 427 | public function login() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public function logLogin() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Failed authentication callback |
||
| 456 | * |
||
| 457 | * @param string $username The failed username. |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | public function loginFailed($username) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $username The username to log failure. |
||
| 471 | * @return boolean |
||
| 472 | */ |
||
| 473 | public function logLoginFailed($username) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Empties the session var associated to the session key. |
||
| 481 | * |
||
| 482 | * @return boolean Logged out or not. |
||
| 483 | */ |
||
| 484 | public function logout() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Reset the password. |
||
| 499 | * |
||
| 500 | * Encrypt the password and re-save the object in the database. |
||
| 501 | * Also updates the last password date & ip. |
||
| 502 | * |
||
| 503 | * @param string $plainPassword The plain (non-encrypted) password to reset to. |
||
| 504 | * @throws InvalidArgumentException If the plain password is not a string. |
||
| 505 | * @return UserInterface Chainable |
||
| 506 | */ |
||
| 507 | public function resetPassword($plainPassword) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get the currently authenticated user (from session) |
||
| 533 | * |
||
| 534 | * Return null if there is no current user in logged into |
||
| 535 | * |
||
| 536 | * @param boolean $reinit Optional. Whether to reload user data from source. |
||
| 537 | * @throws Exception If the user from session is invalid. |
||
| 538 | * @return UserInterface|null |
||
| 539 | */ |
||
| 540 | public static function getAuthenticated($reinit = true) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * ConfigurableInterface > create_config() |
||
| 568 | * |
||
| 569 | * @param array $data Optional. Configuration data. |
||
| 570 | * @return UserConfig |
||
| 571 | */ |
||
| 572 | public function createConfig(array $data = null) |
||
| 580 | } |
||
| 581 |