Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class User extends BaseUser implements \Symfony\Component\Security\Core\User\UserInterface, \Symfony\Component\Security\Core\User\EquatableInterface |
||
|
|
|||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @ORM\Id |
||
| 34 | * @ORM\Column(type="integer") |
||
| 35 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 36 | */ |
||
| 37 | protected $id; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * User constructor. |
||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * This method lets you do "echo $user". |
||
| 49 | * <p> So, to "show" $user, |
||
| 50 | * PHP will actually show the return of this method. <br /> |
||
| 51 | * Here, the username, so "echo $user" |
||
| 52 | * is equivalent to "echo $user->getUsername()" </p>. |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function __toString(): string |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | */ |
||
| 65 | public function addRole($role) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function serialize() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function unserialize($serialized) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function eraseCredentials() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | public function getId() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function getUsername() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function getUsernameCanonical() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function getSalt() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function getEmail() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * {@inheritdoc} |
||
| 175 | */ |
||
| 176 | public function getEmailCanonical() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function getPassword() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function getPlainPassword() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Gets the last login time. |
||
| 199 | * |
||
| 200 | * @return \DateTime|null |
||
| 201 | */ |
||
| 202 | public function getLastLogin() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function getConfirmationToken() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function getRoles() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function hasRole($role) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function isAccountNonExpired() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function isAccountNonLocked() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function isCredentialsNonExpired() |
||
| 263 | |||
| 264 | public function isEnabled() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function isSuperAdmin() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function removeRole($role) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function setUsername($username) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function setUsernameCanonical($userCanonical) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | public function setSalt($salt) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritdoc} |
||
| 322 | */ |
||
| 323 | public function setEmail($email) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | public function setEmailCanonical($emailCanonical) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function setEnabled($boolean) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function setPassword($password) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | */ |
||
| 363 | public function setSuperAdmin($boolean) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | public function setPlainPassword($password) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * {@inheritdoc} |
||
| 386 | */ |
||
| 387 | public function setLastLogin(\DateTime $time = null) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritdoc} |
||
| 396 | */ |
||
| 397 | public function setConfirmationToken($confirmationToken) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | */ |
||
| 407 | public function setPasswordRequestedAt(\DateTime $date = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the timestamp that the user requested a password reset. |
||
| 416 | * |
||
| 417 | * @return null|\DateTime |
||
| 418 | */ |
||
| 419 | public function getPasswordRequestedAt() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritdoc} |
||
| 426 | */ |
||
| 427 | public function isPasswordRequestNonExpired($ttl) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | */ |
||
| 436 | public function setRoles(array $roles) |
||
| 446 | |||
| 447 | public function isEqualTo(\Symfony\Component\Security\Core\User\UserInterface $user) |
||
| 461 | } |
||
| 462 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.