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 | 35 | */ |
|
| 48 | protected $adminLocale; |
||
| 49 | 35 | ||
| 50 | 35 | /** |
|
| 51 | 35 | * @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 | 3 | protected $googleId; |
|
| 59 | |||
| 60 | 3 | /** |
|
| 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 | 8 | * @var string |
|
| 71 | * |
||
| 72 | 8 | * @ORM\Column(type="string", length=180, unique=true) |
|
| 73 | */ |
||
| 74 | 8 | protected $emailCanonical; |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | * |
||
| 79 | * @ORM\Column(name="password", type="string", length=100) |
||
| 80 | */ |
||
| 81 | protected $password; |
||
| 82 | 1 | ||
| 83 | /** |
||
| 84 | 1 | * @var string|null |
|
| 85 | */ |
||
| 86 | 1 | protected $plainPassword; |
|
| 87 | 1 | ||
| 88 | /** |
||
| 89 | 1 | * @var string|null |
|
| 90 | 1 | * |
|
| 91 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 92 | */ |
||
| 93 | protected $confirmationToken; |
||
| 94 | 1 | ||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | * |
||
| 98 | * @ORM\Column(name="salt", type="string", length=100) |
||
| 99 | */ |
||
| 100 | protected $salt; |
||
| 101 | |||
| 102 | 6 | /** |
|
| 103 | * @var \DateTime |
||
| 104 | 6 | * |
|
| 105 | * @ORM\Column(name="last_login", type="datetime", nullable=true) |
||
| 106 | */ |
||
| 107 | protected $lastLogin; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | * |
||
| 112 | 1 | * @ORM\Column(name="roles", type="array") |
|
| 113 | */ |
||
| 114 | 1 | protected $roles; |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @ORM\Column(name="enabled", type="boolean") |
||
| 118 | */ |
||
| 119 | protected $enabled; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Construct a new user |
||
| 123 | */ |
||
| 124 | 2 | public function __construct() |
|
| 125 | { |
||
| 126 | 2 | $this->groups = new ArrayCollection(); |
|
| 127 | $this->roles = []; |
||
| 128 | 2 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get id |
||
| 132 | * |
||
| 133 | * @return int |
||
| 134 | */ |
||
| 135 | public function getId() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set id |
||
| 142 | * |
||
| 143 | * @param int $id |
||
| 144 | * |
||
| 145 | * @return BaseUser |
||
| 146 | */ |
||
| 147 | public function setId($id) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Gets the groupIds for the user. |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | 1 | */ |
|
| 159 | public function getGroupIds() |
||
| 160 | 1 | { |
|
| 161 | $groups = $this->groups; |
||
| 162 | |||
| 163 | $groupIds = array(); |
||
| 164 | if (\count($groups) > 0) { |
||
| 165 | /* @var $group GroupInterface */ |
||
| 166 | 3 | foreach ($groups as $group) { |
|
| 167 | $groupIds[] = $group->getId(); |
||
| 168 | 3 | } |
|
| 169 | 3 | } |
|
| 170 | |||
| 171 | return $groupIds; |
||
| 172 | } |
||
| 173 | |||
| 174 | 1 | /** |
|
| 175 | * Gets the groups the user belongs to. |
||
| 176 | 1 | * |
|
| 177 | 1 | * @return ArrayCollection |
|
| 178 | 1 | */ |
|
| 179 | public function getGroups() |
||
| 183 | |||
| 184 | 1 | /** |
|
| 185 | 1 | * Get adminLocale |
|
| 186 | 1 | * |
|
| 187 | 1 | * @return string |
|
| 188 | */ |
||
| 189 | public function getAdminLocale() |
||
| 193 | |||
| 194 | 1 | /** |
|
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function setEnabled($boolean) |
||
| 198 | { |
||
| 199 | $this->enabled = (bool) $boolean; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set adminLocale |
||
| 206 | 1 | * |
|
| 207 | * @param string $adminLocale |
||
| 208 | 1 | * |
|
| 209 | * @return BaseUser |
||
| 210 | */ |
||
| 211 | public function setAdminLocale($adminLocale) |
||
| 212 | { |
||
| 213 | $this->adminLocale = $adminLocale; |
||
| 214 | |||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * is passwordChanged |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function isPasswordChanged() |
||
| 224 | { |
||
| 225 | return $this->passwordChanged; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setPasswordChanged($passwordChanged) |
||
| 229 | { |
||
| 230 | $this->passwordChanged = $passwordChanged; |
||
| 231 | |||
| 232 | return $this; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | public function getGoogleId() |
||
| 239 | { |
||
| 240 | return $this->googleId; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param mixed $googleId |
||
| 245 | */ |
||
| 246 | public function setGoogleId($googleId) |
||
| 247 | { |
||
| 248 | $this->googleId = $googleId; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param ClassMetadata $metadata |
||
| 253 | */ |
||
| 254 | public static function loadValidatorMetadata(ClassMetadata $metadata) |
||
| 255 | { |
||
| 256 | $metadata->addPropertyConstraint('username', new NotBlank()); |
||
| 257 | $metadata->addPropertyConstraints( |
||
| 258 | 'plainPassword', |
||
| 259 | array( |
||
| 260 | new NotBlank(array('groups' => array('Registration'))), |
||
| 261 | new PasswordRestrictions(array('groups' => array('Registration', 'Default'))), |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | $metadata->addPropertyConstraint('email', new NotBlank()); |
||
| 265 | $metadata->addPropertyConstraint('email', new Email()); |
||
| 266 | $metadata->addConstraint(new UniqueEntity(array( |
||
| 267 | 'fields' => 'username', |
||
| 268 | 'message' => 'errors.user.loginexists', |
||
| 269 | ))); |
||
| 270 | $metadata->addConstraint(new UniqueEntity(array( |
||
| 271 | 'fields' => 'email', |
||
| 272 | 'message' => 'errors.user.emailexists', |
||
| 273 | ))); |
||
| 274 | } |
||
| 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 | public function isAccountNonLocked() |
||
| 287 | { |
||
| 288 | return $this->isEnabled(); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getEmail(): string |
||
| 298 | |||
| 299 | 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 | public function setPlainPassword($plainPassword) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function getRoles() |
||
| 349 | |||
| 350 | public function hasRole($role) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | public function setRoles(array $roles) |
||
| 368 | |||
| 369 | public function removeRole($role) |
||
| 378 | |||
| 379 | public function getSalt(): ?string |
||
| 383 | |||
| 384 | public function setSalt($salt) |
||
| 390 | |||
| 391 | public function isEnabled() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return string The username |
||
| 398 | */ |
||
| 399 | public function getUsername(): string |
||
| 403 | |||
| 404 | 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 | 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 | 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 | 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: