Complex classes like BaseUser 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 BaseUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class BaseUser implements UserInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @ORM\Id |
||
| 19 | * @ORM\Column(type="integer") |
||
| 20 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 21 | */ |
||
| 22 | protected $id; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | * |
||
| 27 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 28 | */ |
||
| 29 | protected $username; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Next Major: Remove attribute |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | * |
||
| 36 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 37 | */ |
||
| 38 | protected $usernameCanonical; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The doctrine metadata is set dynamically in Kunstmaan\AdminBundle\EventListener\MappingListener |
||
| 42 | */ |
||
| 43 | protected $groups; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @ORM\Column(type="string", name="admin_locale", length=5, nullable=true) |
||
| 47 | */ |
||
| 48 | protected $adminLocale; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @ORM\Column(type="boolean", name="password_changed", nullable=true) |
||
| 52 | */ |
||
| 53 | protected $passwordChanged; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ORM\Column(name="google_id", type="string", length=255, nullable=true) |
||
| 57 | */ |
||
| 58 | protected $googleId; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | * |
||
| 63 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 64 | */ |
||
| 65 | protected $email; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Next Major: Remove attribute |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | * |
||
| 72 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 73 | */ |
||
| 74 | protected $emailCanonical; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | * |
||
| 79 | * @ORM\Column(name="password", type="string", length=100) |
||
| 80 | */ |
||
| 81 | protected $password; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string|null |
||
| 85 | */ |
||
| 86 | protected $plainPassword; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string|null |
||
| 90 | * |
||
| 91 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 92 | */ |
||
| 93 | protected $confirmationToken; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | * |
||
| 98 | * @ORM\Column(name="salt", type="string", length=100) |
||
| 99 | */ |
||
| 100 | protected $salt; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var \DateTime |
||
| 104 | * |
||
| 105 | * @ORM\Column(name="last_login", type="datetime", nullable=true) |
||
| 106 | */ |
||
| 107 | protected $lastLogin; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | * |
||
| 112 | * @ORM\Column(name="roles", type="array") |
||
| 113 | */ |
||
| 114 | protected $roles; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @ORM\Column(name="enabled", type="boolean") |
||
| 118 | */ |
||
| 119 | protected $enabled; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Construct a new user |
||
| 123 | */ |
||
| 124 | 35 | public function __construct() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get id |
||
| 132 | * |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | 3 | public function getId() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set id |
||
| 142 | * |
||
| 143 | * @param int $id |
||
| 144 | * |
||
| 145 | * @return BaseUser |
||
| 146 | */ |
||
| 147 | 8 | public function setId($id) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Gets the groupIds for the user. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 1 | public function getGroupIds() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the groups the user belongs to. |
||
| 176 | * |
||
| 177 | * @return ArrayCollection |
||
| 178 | */ |
||
| 179 | 5 | public function getGroups() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get adminLocale |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 1 | public function getAdminLocale() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 2 | public function setEnabled($boolean) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Set adminLocale |
||
| 206 | * |
||
| 207 | * @param string $adminLocale |
||
| 208 | * |
||
| 209 | * @return BaseUser |
||
| 210 | */ |
||
| 211 | 2 | public function setAdminLocale($adminLocale) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * is passwordChanged |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 1 | public function isPasswordChanged() |
|
| 227 | |||
| 228 | 2 | public function setPasswordChanged($passwordChanged) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 1 | public function getGoogleId() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param mixed $googleId |
||
| 245 | */ |
||
| 246 | 3 | public function setGoogleId($googleId) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param ClassMetadata $metadata |
||
| 253 | */ |
||
| 254 | 1 | public static function loadValidatorMetadata(ClassMetadata $metadata) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Return class name of form type used to add & edit users |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | abstract public function getFormTypeClass(); |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | 1 | public function isAccountNonLocked() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getEmail(): string |
||
| 298 | |||
| 299 | 1 | public function setEmail($email) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getPassword(): string |
||
| 313 | |||
| 314 | public function setPassword($password) |
||
| 320 | |||
| 321 | public function getPlainPassword(): ?string |
||
| 325 | |||
| 326 | 1 | public function setPlainPassword($plainPassword) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | 1 | public function getRoles() |
|
| 349 | |||
| 350 | 1 | public function hasRole($role) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | public function setRoles(array $roles) |
||
| 368 | |||
| 369 | 1 | public function removeRole($role) |
|
| 378 | |||
| 379 | public function getSalt(): ?string |
||
| 383 | |||
| 384 | public function setSalt($salt) |
||
| 390 | |||
| 391 | 1 | public function isEnabled() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @return string The username |
||
| 398 | */ |
||
| 399 | 7 | public function getUsername(): string |
|
| 403 | |||
| 404 | 8 | public function setUsername($username) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Removes sensitive data from the user. |
||
| 413 | */ |
||
| 414 | public function eraseCredentials(): void |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | 1 | public function addRole($role) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritdoc} |
||
| 438 | */ |
||
| 439 | public function getGroupNames() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | public function hasGroup($name) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * {@inheritdoc} |
||
| 459 | */ |
||
| 460 | 4 | public function addGroup(GroupInterface $group) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function removeGroup(GroupInterface $group) |
||
| 480 | |||
| 481 | public function __toString() |
||
| 485 | |||
| 486 | public function getUsernameCanonical() |
||
| 493 | |||
| 494 | public function setUsernameCanonical($usernameCanonical) |
||
| 501 | |||
| 502 | public function getEmailCanonical() |
||
| 509 | |||
| 510 | public function setEmailCanonical($emailCanonical) |
||
| 517 | |||
| 518 | 1 | public function isSuperAdmin() |
|
| 523 | |||
| 524 | public function setSuperAdmin($boolean) |
||
| 529 | |||
| 530 | public function getConfirmationToken() |
||
| 534 | |||
| 535 | public function setConfirmationToken($confirmationToken) |
||
| 539 | |||
| 540 | public function setPasswordRequestedAt(DateTime $date = null) |
||
| 545 | |||
| 546 | public function getLastLogin() |
||
| 550 | |||
| 551 | public function setLastLogin(?DateTime $lastLogin = null) |
||
| 557 | |||
| 558 | public function isAccountNonExpired() |
||
| 565 | |||
| 566 | public function isCredentialsNonExpired() |
||
| 573 | |||
| 574 | public function isPasswordRequestNonExpired($ttl) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * {@inheritdoc} |
||
| 584 | */ |
||
| 585 | public function serialize() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * {@inheritdoc} |
||
| 599 | */ |
||
| 600 | public function unserialize($serialized) |
||
| 613 | } |
||
| 614 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: