Complex classes like AdvancedUserAccount 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 AdvancedUserAccount, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class AdvancedUserAccount implements AdvancedUserAccountInterface |
||
| 8 | { |
||
| 9 | /** @var int */ |
||
| 10 | protected $id; |
||
| 11 | |||
| 12 | /** @var string */ |
||
| 13 | protected $username; |
||
| 14 | |||
| 15 | /** @var string */ |
||
| 16 | protected $email; |
||
| 17 | |||
| 18 | /** @var string */ |
||
| 19 | protected $name; |
||
| 20 | |||
| 21 | /** @var boolean */ |
||
| 22 | protected $enabled; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | protected $salt; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Encrypted password. Must be persisted. |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $password; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Plain password. Used for model validation. Must not be persisted. |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $plainPassword; |
||
| 38 | |||
| 39 | /** @var \DateTime */ |
||
| 40 | protected $lastLogin; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Random string sent to the user email address in order to verify it |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $confirmationToken; |
||
| 48 | |||
| 49 | /** @var \DateTime */ |
||
| 50 | protected $passwordRequestedAt; |
||
| 51 | |||
| 52 | /** @var boolean */ |
||
| 53 | protected $locked; |
||
| 54 | |||
| 55 | /** @var boolean */ |
||
| 56 | protected $expired; |
||
| 57 | |||
| 58 | /** @var \DateTime */ |
||
| 59 | protected $expiresAt; |
||
| 60 | |||
| 61 | /** @var array */ |
||
| 62 | protected $roles; |
||
| 63 | |||
| 64 | /** @var boolean */ |
||
| 65 | protected $credentialsExpired; |
||
| 66 | |||
| 67 | /** @var \DateTime */ |
||
| 68 | protected $credentialsExpireAt; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | protected $locale; |
||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | public function __construct() |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $name |
||
| 91 | * @return AdvancedUserAccountInterface|$this |
||
| 92 | */ |
||
| 93 | public function setName($name) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getName() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string|Role $role |
||
| 110 | * @return $this|AdvancedUserAccountInterface |
||
| 111 | */ |
||
| 112 | public function addRole($role) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Serializes the user. |
||
| 128 | * |
||
| 129 | * The serialized data have to contain the fields used by the equals method and the username. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function serialize() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Unserializes the user. |
||
| 152 | * |
||
| 153 | * @param string $serialized |
||
| 154 | */ |
||
| 155 | public function unserialize($serialized) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Removes sensitive data from the user. |
||
| 177 | */ |
||
| 178 | public function eraseCredentials() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return mixed |
||
| 185 | */ |
||
| 186 | public function getId() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getSalt() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getEmail() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $username |
||
| 209 | * @return AdvancedUserAccountInterface |
||
| 210 | */ |
||
| 211 | public function setUsername($username) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the username used to authenticate the user. |
||
| 220 | * |
||
| 221 | * @return string The username |
||
| 222 | */ |
||
| 223 | public function getUsername() |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Gets the encrypted password. |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getPassword() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getPlainPassword() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return \DateTime |
||
| 248 | */ |
||
| 249 | public function getLastLogin() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getConfirmationToken() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return array The roles |
||
| 264 | */ |
||
| 265 | public function getRoles() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Never use this to check if this user has access to anything! |
||
| 277 | * |
||
| 278 | * Use the SecurityContext, or an implementation of AccessDecisionManager |
||
| 279 | * instead, e.g. |
||
| 280 | * |
||
| 281 | * $securityContext->isGranted('ROLE_USER'); |
||
| 282 | * |
||
| 283 | * @param string $role |
||
| 284 | * |
||
| 285 | * @return boolean |
||
| 286 | */ |
||
| 287 | public function hasRole($role) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function isAccountNonExpired() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function isAccountNonLocked() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function isCredentialsNonExpired() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function isCredentialsExpired() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function isEnabled() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function isExpired() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function isLocked() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function isSuperAdmin() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param AdvancedUserAccountInterface $account |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function isAccount(AdvancedUserAccountInterface $account = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $role |
||
| 383 | * @return $this|AdvancedUserAccountInterface |
||
| 384 | */ |
||
| 385 | public function removeRole($role) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param \DateTime $date |
||
| 397 | * @return $this|AdvancedUserAccountInterface |
||
| 398 | */ |
||
| 399 | public function setCredentialsExpireAt(\DateTime $date) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param boolean $boolean |
||
| 408 | * @return $this|AdvancedUserAccountInterface |
||
| 409 | */ |
||
| 410 | public function setCredentialsExpired($boolean) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $email |
||
| 419 | * @return $this|AdvancedUserAccountInterface |
||
| 420 | */ |
||
| 421 | public function setEmail($email) |
||
| 427 | |||
| 428 | public function setEnabled($boolean) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Boolean $boolean |
||
| 437 | * @return $this|AdvancedUserAccountInterface |
||
| 438 | */ |
||
| 439 | public function setExpired($boolean) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param \DateTime $date |
||
| 448 | * @return $this|AdvancedUserAccountInterface |
||
| 449 | */ |
||
| 450 | public function setExpiresAt(\DateTime $date) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $password |
||
| 459 | * @return $this|AdvancedUserAccountInterface |
||
| 460 | */ |
||
| 461 | public function setPassword($password) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param bool $boolean |
||
| 470 | * @return $this|AdvancedUserAccountInterface |
||
| 471 | */ |
||
| 472 | public function setSuperAdmin($boolean) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $password |
||
| 485 | * @return $this|AdvancedUserAccountInterface |
||
| 486 | */ |
||
| 487 | public function setPlainPassword($password) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param \DateTime $time |
||
| 496 | * @return $this|AdvancedUserAccountInterface |
||
| 497 | */ |
||
| 498 | public function setLastLogin(\DateTime $time) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param bool $boolean |
||
| 507 | * @return $this|AdvancedUserAccountInterface |
||
| 508 | */ |
||
| 509 | public function setLocked($boolean) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $confirmationToken |
||
| 518 | * @return $this|AdvancedUserAccountInterface |
||
| 519 | */ |
||
| 520 | public function setConfirmationToken($confirmationToken) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param \DateTime $date |
||
| 529 | * @return $this|AdvancedUserAccountInterface |
||
| 530 | */ |
||
| 531 | public function setPasswordRequestedAt(\DateTime $date = null) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return null|\DateTime |
||
| 540 | */ |
||
| 541 | public function getPasswordRequestedAt() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param int $ttl |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | public function isPasswordRequestNonExpired($ttl) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param array $roles |
||
| 558 | * @return $this|AdvancedUserAccountInterface |
||
| 559 | */ |
||
| 560 | public function setRoles(array $roles) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | public function getLocale() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $locale |
||
| 581 | * @return AdvancedUserAccountInterface|$this |
||
| 582 | */ |
||
| 583 | public function setLocale($locale) |
||
| 589 | |||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | public function __toString() |
||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * @param $string |
||
| 603 | * @return string |
||
| 604 | */ |
||
| 605 | protected function canonicalize($string) |
||
| 609 | } |