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 |
||
| 32 | class User implements AdvancedUserInterface |
||
| 33 | { |
||
| 34 | const GENDER_NEUTRAL = 0; |
||
| 35 | const GENDER_MALE = 1; |
||
| 36 | const GENDER_FEMALE = 2; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @ORM\Id |
||
| 40 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 41 | * @ORM\Column(type="integer") |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $id; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @ORM\Column(type="string", unique=true, nullable=true) |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $userName; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @ORM\Column(type="string", unique=true, nullable=true) |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $password; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @ORM\Column(type="string") |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $firstName; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @ORM\Column(type="string") |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $lastName; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @ORM\Column(type="boolean") |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $enabled; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @ORM\Column(type="boolean") |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | private $globalAdministrator; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @ORM\Column(type="string", unique=true, nullable=true) |
||
| 84 | * @Assert\Email |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $emailAddress; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @ORM\Column(type="integer") |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | private $gender; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @ORM\Column(type="string", nullable=true) |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $token; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @ORM\Column(type="datetime", nullable=true) |
||
| 103 | * @var \DateTime |
||
| 104 | */ |
||
| 105 | private $tokenExpiration; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @ORM\Column(type="datetime", nullable=true) |
||
| 109 | * @var \DateTime |
||
| 110 | */ |
||
| 111 | private $lastAccess; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @ORM\Column(type="datetime", nullable=true) |
||
| 115 | * @var \DateTime |
||
| 116 | */ |
||
| 117 | private $blockedUntil; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @ORM\OneToMany(targetEntity="Membership", mappedBy="user") |
||
| 121 | * @var Collection |
||
| 122 | */ |
||
| 123 | private $memberships; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Convertir usuario en cadena |
||
| 127 | */ |
||
| 128 | public function __toString() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Constructor |
||
| 135 | */ |
||
| 136 | public function __construct() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get id |
||
| 143 | * |
||
| 144 | * @return integer |
||
| 145 | */ |
||
| 146 | public function getId() |
||
| 147 | { |
||
| 148 | return $this->id; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set userName |
||
| 153 | * |
||
| 154 | * @param string $userName |
||
| 155 | * |
||
| 156 | * @return User |
||
| 157 | */ |
||
| 158 | public function setUserName($userName) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get userName |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getUserName() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set password |
||
| 177 | * |
||
| 178 | * @param string $password |
||
| 179 | * |
||
| 180 | * @return User |
||
| 181 | */ |
||
| 182 | public function setPassword($password) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get password |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getPassword() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set firstName |
||
| 201 | * |
||
| 202 | * @param string $firstName |
||
| 203 | * |
||
| 204 | * @return User |
||
| 205 | */ |
||
| 206 | public function setFirstName($firstName) |
||
| 207 | { |
||
| 208 | $this->firstName = $firstName; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get firstName |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getFirstName() |
||
| 219 | { |
||
| 220 | return $this->firstName; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set lastName |
||
| 225 | * |
||
| 226 | * @param string $lastName |
||
| 227 | * |
||
| 228 | * @return User |
||
| 229 | */ |
||
| 230 | public function setLastName($lastName) |
||
| 231 | { |
||
| 232 | $this->lastName = $lastName; |
||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get lastName |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function getLastName() |
||
| 243 | { |
||
| 244 | return $this->lastName; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set enabled |
||
| 249 | * |
||
| 250 | * @param boolean $enabled |
||
| 251 | * |
||
| 252 | * @return User |
||
| 253 | */ |
||
| 254 | public function setEnabled($enabled) |
||
| 255 | { |
||
| 256 | $this->enabled = $enabled; |
||
| 257 | |||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Set globalAdmin |
||
| 264 | * |
||
| 265 | * @param boolean $globalAdministrator |
||
| 266 | * |
||
| 267 | * @return User |
||
| 268 | */ |
||
| 269 | public function setGlobalAdministrator($globalAdministrator) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get globalAdmin |
||
| 278 | * |
||
| 279 | * @return boolean |
||
| 280 | */ |
||
| 281 | public function isGlobalAdministrator() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set emailAddress |
||
| 288 | * |
||
| 289 | * @param string $emailAddress |
||
| 290 | * |
||
| 291 | * @return User |
||
| 292 | */ |
||
| 293 | public function setEmailAddress($emailAddress) |
||
| 294 | { |
||
| 295 | $this->emailAddress = $emailAddress; |
||
| 296 | |||
| 297 | return $this; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get emailAddress |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getEmailAddress() |
||
| 306 | { |
||
| 307 | return $this->emailAddress; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set gender |
||
| 312 | * |
||
| 313 | * @param integer $gender |
||
| 314 | * |
||
| 315 | * @return User |
||
| 316 | */ |
||
| 317 | public function setGender($gender) |
||
| 318 | { |
||
| 319 | $this->gender = $gender; |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get gender |
||
| 326 | * |
||
| 327 | * @return integer |
||
| 328 | */ |
||
| 329 | public function getGender() |
||
| 330 | { |
||
| 331 | return $this->gender; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set token |
||
| 336 | * |
||
| 337 | * @param string $token |
||
| 338 | * |
||
| 339 | * @return User |
||
| 340 | */ |
||
| 341 | public function setToken($token) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get token |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getToken() |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * Set tokenExpiration |
||
| 361 | * |
||
| 362 | * @param \DateTime $tokenExpiration |
||
| 363 | * |
||
| 364 | * @return User |
||
| 365 | */ |
||
| 366 | public function setTokenExpiration($tokenExpiration) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get tokenExpiration |
||
| 375 | * |
||
| 376 | * @return \DateTime |
||
| 377 | */ |
||
| 378 | public function getTokenExpiration() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set lastAccess |
||
| 385 | * |
||
| 386 | * @param \DateTime $lastAccess |
||
| 387 | * |
||
| 388 | * @return User |
||
| 389 | */ |
||
| 390 | public function setLastAccess($lastAccess) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get lastAccess |
||
| 399 | * |
||
| 400 | * @return \DateTime |
||
| 401 | */ |
||
| 402 | public function getLastAccess() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set blockedUntil |
||
| 409 | * |
||
| 410 | * @param \DateTime $blockedUntil |
||
| 411 | * |
||
| 412 | * @return User |
||
| 413 | */ |
||
| 414 | public function setBlockedUntil($blockedUntil) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get blockedUntil |
||
| 423 | * |
||
| 424 | * @return \DateTime |
||
| 425 | */ |
||
| 426 | public function getBlockedUntil() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Add membership |
||
| 433 | * |
||
| 434 | * @param Membership $membership |
||
| 435 | * |
||
| 436 | * @return User |
||
| 437 | */ |
||
| 438 | public function addMembership(Membership $membership) |
||
| 439 | { |
||
| 440 | $this->memberships[] = $membership; |
||
| 441 | |||
| 442 | return $this; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Remove membership |
||
| 447 | * |
||
| 448 | * @param Membership $membership |
||
| 449 | */ |
||
| 450 | public function removeMembership(Membership $membership) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get memberships |
||
| 457 | * |
||
| 458 | * @return \Doctrine\Common\Collections\Collection |
||
| 459 | */ |
||
| 460 | public function getMemberships() |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * @Assert\Callback |
||
| 468 | */ |
||
| 469 | public function validate(ExecutionContextInterface $context) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Checks whether the user's account has expired. |
||
| 484 | * |
||
| 485 | * Internally, if this method returns false, the authentication system |
||
| 486 | * will throw an AccountExpiredException and prevent login. |
||
| 487 | * |
||
| 488 | * @return bool true if the user's account is non expired, false otherwise |
||
| 489 | * |
||
| 490 | * @see AccountExpiredException |
||
| 491 | */ |
||
| 492 | public function isAccountNonExpired() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Checks whether the user is locked. |
||
| 499 | * |
||
| 500 | * Internally, if this method returns false, the authentication system |
||
| 501 | * will throw a LockedException and prevent login. |
||
| 502 | * |
||
| 503 | * @return bool true if the user is not locked, false otherwise |
||
| 504 | * |
||
| 505 | * @see LockedException |
||
| 506 | */ |
||
| 507 | public function isAccountNonLocked() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Checks whether the user's credentials (password) has expired. |
||
| 514 | * |
||
| 515 | * Internally, if this method returns false, the authentication system |
||
| 516 | * will throw a CredentialsExpiredException and prevent login. |
||
| 517 | * |
||
| 518 | * @return bool true if the user's credentials are non expired, false otherwise |
||
| 519 | * |
||
| 520 | * @see CredentialsExpiredException |
||
| 521 | */ |
||
| 522 | public function isCredentialsNonExpired() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Checks whether the user is enabled. |
||
| 529 | * |
||
| 530 | * Internally, if this method returns false, the authentication system |
||
| 531 | * will throw a DisabledException and prevent login. |
||
| 532 | * |
||
| 533 | * @return bool true if the user is enabled, false otherwise |
||
| 534 | * |
||
| 535 | * @see DisabledException |
||
| 536 | */ |
||
| 537 | public function isEnabled() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the roles granted to the user. |
||
| 544 | * |
||
| 545 | * <code> |
||
| 546 | * public function getRoles() |
||
| 547 | * { |
||
| 548 | * return array('ROLE_USER'); |
||
| 549 | * } |
||
| 550 | * </code> |
||
| 551 | * |
||
| 552 | * Alternatively, the roles might be stored on a ``roles`` property, |
||
| 553 | * and populated in any number of different ways when the user object |
||
| 554 | * is created. |
||
| 555 | * |
||
| 556 | * @return string[] The user roles |
||
| 557 | */ |
||
| 558 | public function getRoles() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Returns the salt that was originally used to encode the password. |
||
| 565 | * |
||
| 566 | * This can return null if the password was not encoded using a salt. |
||
| 567 | * |
||
| 568 | * @return string|null The salt |
||
| 569 | */ |
||
| 570 | public function getSalt() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Removes sensitive data from the user. |
||
| 577 | * |
||
| 578 | * This is important if, at any given point, sensitive information like |
||
| 579 | * the plain-text password is stored on this object. |
||
| 580 | */ |
||
| 581 | public function eraseCredentials() |
||
| 584 | } |
||
| 585 |