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 | 35 | ||
| 60 | /** | ||
| 61 | 35 | * @var string | |
| 62 | 35 | * | |
| 63 | 35 | * @ORM\Column(type="string", length=180, unique=true) | |
| 64 | 35 | */ | |
| 65 | protected $email; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Next Major: Remove attribute | ||
| 69 | * | ||
| 70 | * @var string | ||
| 71 | 3 | * | |
| 72 | * @ORM\Column(type="string", length=180, unique=true) | ||
| 73 | 3 | */ | |
| 74 | protected $emailCanonical; | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @var string | ||
| 78 | * | ||
| 79 | * @ORM\Column(name="password", type="string", length=100) | ||
| 80 | */ | ||
| 81 | protected $password; | ||
| 82 | |||
| 83 | 8 | /** | |
| 84 | * @var string|null | ||
| 85 | 8 | */ | |
| 86 | protected $plainPassword; | ||
| 87 | 8 | ||
| 88 | /** | ||
| 89 | * @var string|null | ||
| 90 | * | ||
| 91 | * @ORM\Column(type="string", length=255, nullable=true) | ||
| 92 | */ | ||
| 93 | protected $confirmationToken; | ||
| 94 | |||
| 95 | 1 | /** | |
| 96 | * @var string | ||
| 97 | 1 | * | |
| 98 | * @ORM\Column(name="salt", type="string", length=100) | ||
| 99 | 1 | */ | |
| 100 | 1 | protected $salt; | |
| 101 | |||
| 102 | 1 | /** | |
| 103 | 1 | * @var \DateTime | |
| 104 | * | ||
| 105 | * @ORM\Column(name="last_login", type="datetime", nullable=true) | ||
| 106 | */ | ||
| 107 | 1 | protected $lastLogin; | |
| 108 | |||
| 109 | /** | ||
| 110 | * @var array | ||
| 111 | * | ||
| 112 | * @ORM\Column(name="roles", type="array") | ||
| 113 | */ | ||
| 114 | protected $roles; | ||
| 115 | 6 | ||
| 116 | /** | ||
| 117 | 6 | * @ORM\Column(name="enabled", type="boolean") | |
| 118 | */ | ||
| 119 | protected $enabled; | ||
| 120 | |||
| 121 | /** | ||
| 122 | * @var \DateTimeImmutable|null | ||
| 123 | * @ORM\Column(name="created_at", type="datetime_immutable", nullable=true) | ||
| 124 | */ | ||
| 125 | 1 | protected $createdAt; | |
| 126 | |||
| 127 | 1 | /** | |
| 128 | * @var string|null | ||
| 129 | * @ORM\Column(name="created_by", type="string", nullable=true) | ||
| 130 | */ | ||
| 131 | protected $createdBy; | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Construct a new user | ||
| 135 | */ | ||
| 136 | public function __construct() | ||
| 137 | 2 |     { | |
| 138 | $this->groups = new ArrayCollection(); | ||
| 139 | 2 | $this->roles = []; | |
| 140 | $this->createdAt = new \DateTimeImmutable(); | ||
| 141 | 2 | } | |
| 142 | |||
| 143 | /** | ||
| 144 | * Get id | ||
| 145 | * | ||
| 146 | * @return int | ||
| 147 | */ | ||
| 148 | public function getId() | ||
| 152 | |||
| 153 | /** | ||
| 154 | * Set id | ||
| 155 | * | ||
| 156 | * @param int $id | ||
| 157 | * | ||
| 158 | * @return BaseUser | ||
| 159 | */ | ||
| 160 | public function setId($id) | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Gets the groupIds for the user. | ||
| 169 | * | ||
| 170 | * @return array | ||
| 171 | 1 | */ | |
| 172 | public function getGroupIds() | ||
| 173 | 1 |     { | |
| 174 | $groups = $this->groups; | ||
| 175 | |||
| 176 | $groupIds = []; | ||
| 177 |         if (\count($groups) > 0) { | ||
| 178 | /* @var $group GroupInterface */ | ||
| 179 | 3 |             foreach ($groups as $group) { | |
| 180 | $groupIds[] = $group->getId(); | ||
| 181 | 3 | } | |
| 182 | 3 | } | |
| 183 | |||
| 184 | return $groupIds; | ||
| 185 | } | ||
| 186 | |||
| 187 | 1 | /** | |
| 188 | * Gets the groups the user belongs to. | ||
| 189 | 1 | * | |
| 190 | 1 | * @return ArrayCollection | |
| 191 | 1 | */ | |
| 192 | public function getGroups() | ||
| 196 | |||
| 197 | 1 | /** | |
| 198 | 1 | * Get adminLocale | |
| 199 | 1 | * | |
| 200 | 1 | * @return string | |
| 201 | */ | ||
| 202 | public function getAdminLocale() | ||
| 206 | |||
| 207 | 1 | /** | |
| 208 |      * {@inheritdoc} | ||
| 209 | */ | ||
| 210 | public function setEnabled($boolean) | ||
| 211 |     { | ||
| 212 | $this->enabled = (bool) $boolean; | ||
| 213 | |||
| 214 | return $this; | ||
| 215 | } | ||
| 216 | |||
| 217 | /** | ||
| 218 | * Set adminLocale | ||
| 219 | 1 | * | |
| 220 | * @param string $adminLocale | ||
| 221 | 1 | * | |
| 222 | * @return BaseUser | ||
| 223 | */ | ||
| 224 | public function setAdminLocale($adminLocale) | ||
| 225 |     { | ||
| 226 | $this->adminLocale = $adminLocale; | ||
| 227 | |||
| 228 | return $this; | ||
| 229 | } | ||
| 230 | |||
| 231 | /** | ||
| 232 | * is passwordChanged | ||
| 233 | * | ||
| 234 | * @return bool | ||
| 235 | */ | ||
| 236 | public function isPasswordChanged() | ||
| 237 |     { | ||
| 238 | return $this->passwordChanged; | ||
| 239 | } | ||
| 240 | |||
| 241 | public function setPasswordChanged($passwordChanged) | ||
| 242 |     { | ||
| 243 | $this->passwordChanged = $passwordChanged; | ||
| 244 | |||
| 245 | return $this; | ||
| 246 | } | ||
| 247 | |||
| 248 | /** | ||
| 249 | * @return mixed | ||
| 250 | */ | ||
| 251 | public function getGoogleId() | ||
| 252 |     { | ||
| 253 | return $this->googleId; | ||
| 254 | } | ||
| 255 | |||
| 256 | /** | ||
| 257 | * @param mixed $googleId | ||
| 258 | */ | ||
| 259 | public function setGoogleId($googleId) | ||
| 260 |     { | ||
| 261 | $this->googleId = $googleId; | ||
| 262 | } | ||
| 263 | |||
| 264 | public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
| 265 |     { | ||
| 266 |         $metadata->addPropertyConstraint('username', new NotBlank()); | ||
| 267 | $metadata->addPropertyConstraints( | ||
| 268 | 'plainPassword', | ||
| 269 | [ | ||
| 270 | new NotBlank(['groups' => ['Registration']]), | ||
| 271 | new PasswordRestrictions(['groups' => ['Registration', 'Default']]), | ||
| 272 | ] | ||
| 273 | ); | ||
| 274 |         $metadata->addPropertyConstraint('email', new NotBlank()); | ||
| 275 |         $metadata->addPropertyConstraint('email', new Email()); | ||
| 276 | $metadata->addConstraint(new UniqueEntity([ | ||
| 277 | 'fields' => 'username', | ||
| 278 | 'message' => 'errors.user.loginexists', | ||
| 279 | ])); | ||
| 280 | $metadata->addConstraint(new UniqueEntity([ | ||
| 281 | 'fields' => 'email', | ||
| 282 | 'message' => 'errors.user.emailexists', | ||
| 283 | ])); | ||
| 284 | } | ||
| 285 | |||
| 286 | /** | ||
| 287 | * Return class name of form type used to add & edit users | ||
| 288 | * | ||
| 289 | * @return string | ||
| 290 | */ | ||
| 291 | abstract public function getFormTypeClass(); | ||
| 292 | |||
| 293 | /** | ||
| 294 |      * {@inheritdoc} | ||
| 295 | */ | ||
| 296 | public function isAccountNonLocked() | ||
| 297 |     { | ||
| 298 | return $this->isEnabled(); | ||
| 299 | } | ||
| 300 | |||
| 301 | public function getEmail(): string | ||
| 302 |     { | ||
| 305 | |||
| 306 | public function setEmail($email) | ||
| 312 | |||
| 313 | public function getPassword(): string | ||
| 317 | |||
| 318 | public function setPassword($password) | ||
| 324 | |||
| 325 | public function getPlainPassword(): ?string | ||
| 329 | |||
| 330 | public function setPlainPassword($plainPassword) | ||
| 336 | |||
| 337 | /** | ||
| 338 | * @return array | ||
| 339 | */ | ||
| 340 | public function getRoles() | ||
| 353 | |||
| 354 | public function hasRole($role) | ||
| 358 | |||
| 359 | /** | ||
| 360 |      * {@inheritdoc} | ||
| 361 | */ | ||
| 362 | public function setRoles(array $roles) | ||
| 372 | |||
| 373 | public function removeRole($role) | ||
| 382 | |||
| 383 | public function getSalt(): ?string | ||
| 387 | |||
| 388 | public function setSalt($salt) | ||
| 394 | |||
| 395 | public function isEnabled() | ||
| 399 | |||
| 400 | /** | ||
| 401 | * @return string The username | ||
| 402 | */ | ||
| 403 | public function getUsername(): string | ||
| 407 | |||
| 408 | public function setUsername($username) | ||
| 414 | |||
| 415 | /** | ||
| 416 | * Removes sensitive data from the user. | ||
| 417 | */ | ||
| 418 | public function eraseCredentials(): void | ||
| 422 | |||
| 423 | /** | ||
| 424 |      * {@inheritdoc} | ||
| 425 | */ | ||
| 426 | public function addRole($role) | ||
| 439 | |||
| 440 | /** | ||
| 441 |      * {@inheritdoc} | ||
| 442 | */ | ||
| 443 | public function getGroupNames() | ||
| 452 | |||
| 453 | /** | ||
| 454 |      * {@inheritdoc} | ||
| 455 | */ | ||
| 456 | public function hasGroup($name) | ||
| 460 | |||
| 461 | /** | ||
| 462 |      * {@inheritdoc} | ||
| 463 | */ | ||
| 464 | public function addGroup(GroupInterface $group) | ||
| 472 | |||
| 473 | /** | ||
| 474 |      * {@inheritdoc} | ||
| 475 | */ | ||
| 476 | public function removeGroup(GroupInterface $group) | ||
| 484 | |||
| 485 | public function __toString() | ||
| 489 | |||
| 490 | public function getUsernameCanonical() | ||
| 497 | |||
| 498 | public function setUsernameCanonical($usernameCanonical) | ||
| 505 | |||
| 506 | public function getEmailCanonical() | ||
| 513 | |||
| 514 | public function setEmailCanonical($emailCanonical) | ||
| 521 | |||
| 522 | public function isSuperAdmin() | ||
| 527 | |||
| 528 | public function setSuperAdmin($boolean) | ||
| 533 | |||
| 534 | public function getConfirmationToken() | ||
| 538 | |||
| 539 | public function setConfirmationToken($confirmationToken) | ||
| 543 | |||
| 544 | public function setPasswordRequestedAt(DateTime $date = null) | ||
| 549 | |||
| 550 | public function getLastLogin() | ||
| 554 | |||
| 555 | public function setLastLogin(?DateTime $lastLogin = null) | ||
| 561 | |||
| 562 | public function getCreatedAt(): ?\DateTimeImmutable | ||
| 566 | |||
| 567 | public function getCreatedBy(): ?string | ||
| 571 | |||
| 572 | public function setCreatedBy(string $createdBy): void | ||
| 576 | |||
| 577 | /** | ||
| 578 | * NEXT_MAJOR remove method | ||
| 579 | * | ||
| 580 | * @deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0. | ||
| 581 | */ | ||
| 582 | public function isAccountNonExpired() | ||
| 586 | |||
| 587 | /** | ||
| 588 | * NEXT_MAJOR remove method | ||
| 589 | * | ||
| 590 | * @deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0. | ||
| 591 | */ | ||
| 592 | public function isCredentialsNonExpired() | ||
| 596 | |||
| 597 | /** | ||
| 598 | * NEXT_MAJOR remove method | ||
| 599 | * | ||
| 600 | * @deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0. | ||
| 601 | */ | ||
| 602 | public function isPasswordRequestNonExpired($ttl) | ||
| 606 | |||
| 607 | /** | ||
| 608 |      * {@inheritdoc} | ||
| 609 | */ | ||
| 610 | public function serialize() | ||
| 621 | |||
| 622 | /** | ||
| 623 |      * {@inheritdoc} | ||
| 624 | */ | ||
| 625 | public function unserialize($serialized) | ||
| 638 | } | ||
| 639 | 
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: