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 |
||
| 33 | class User extends AbstractIdentifiableEntity implements UserInterface, DraftableEntityInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Users login name |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | * @ODM\Field(type="string") |
||
| 40 | * @ODM\Index(unique=true, sparse=true, order="asc") |
||
| 41 | */ |
||
| 42 | protected $login; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Role of an user. Currently "user" or "recruiter" |
||
| 46 | * |
||
| 47 | * @ODM\Field(type="string") |
||
| 48 | */ |
||
| 49 | protected $role; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Users contact data. |
||
| 53 | * |
||
| 54 | * @ODM\EmbedOne(targetDocument="Info") |
||
| 55 | */ |
||
| 56 | protected $info; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Authentification Sessions like oAuth |
||
| 60 | * After Authentification with OAuth sessions can be stored like a password/key pair |
||
| 61 | * |
||
| 62 | * @ODM\EmbedMany(targetDocument="AuthSession") |
||
| 63 | */ |
||
| 64 | protected $authSessions; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Users login password |
||
| 68 | * |
||
| 69 | * @ODM\Field(type="string") |
||
| 70 | */ |
||
| 71 | protected $credential; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Users primary email address |
||
| 75 | * |
||
| 76 | * @ODM\Field(type="string") |
||
| 77 | */ |
||
| 78 | protected $email; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * pre-shared key, which allows an external application to authenticate |
||
| 82 | * |
||
| 83 | * @var String |
||
| 84 | * @ODM\Field(type="string") |
||
| 85 | */ |
||
| 86 | protected $secret; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Can contain various HybridAuth profiles. |
||
| 90 | * Deprecated: replaced by User::$profiles |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | * @deprecated |
||
| 94 | * @ODM\Hash |
||
| 95 | */ |
||
| 96 | protected $profile = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Can contain various HybridAuth profiles. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | * @ODM\Hash |
||
| 103 | */ |
||
| 104 | protected $profiles = []; |
||
| 105 | |||
| 106 | /** @var array |
||
| 107 | * @ODM\EmbedMany(discriminatorField="_entity") |
||
| 108 | */ |
||
| 109 | protected $settings; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * This is not a persistent property! |
||
| 113 | * |
||
| 114 | * @var SettingsEntityResolver |
||
| 115 | */ |
||
| 116 | protected $settingsEntityResolver; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * User groups. |
||
| 120 | * |
||
| 121 | * @var Collection |
||
| 122 | * @ODM\ReferenceMany(targetDocument="Group", mappedBy="owner", simple=true, cascade="all") |
||
| 123 | */ |
||
| 124 | protected $groups; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * User tokens. Is generated when recovering Passwords as a short term key. |
||
| 128 | * |
||
| 129 | * @var Collection |
||
| 130 | * @ODM\EmbedMany(targetDocument="Token") |
||
| 131 | */ |
||
| 132 | protected $tokens; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The organization reference for the user. |
||
| 136 | * |
||
| 137 | * This field is not stored in the database, but injected on postLoad via |
||
| 138 | * {@link \Organizations\Repository\Event\InjectOrganizationReferenceListener} |
||
| 139 | * |
||
| 140 | * @var OrganizationReferenceInterface |
||
| 141 | * |
||
| 142 | * @since 0.18 |
||
| 143 | */ |
||
| 144 | protected $organization; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Is this entity a draft or not? |
||
| 148 | * |
||
| 149 | * @var bool |
||
| 150 | * @ODM\Boolean |
||
| 151 | */ |
||
| 152 | protected $isDraft = false; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Status of user |
||
| 156 | * |
||
| 157 | * @var Status |
||
| 158 | * @ODM\EmbedOne(targetDocument="Status") |
||
| 159 | * @ODM\Index |
||
| 160 | */ |
||
| 161 | protected $status; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @see http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/best-practices.html |
||
| 165 | * It is recommended best practice to initialize any business collections in documents in the constructor. |
||
| 166 | * {mg: What about lazy loading? Initialize the Collection in the getter, if none is set? Reduce overload.} |
||
| 167 | */ |
||
| 168 | public function __construct() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function isDraft() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param bool $flag |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function setIsDraft($flag) |
||
| 191 | |||
| 192 | |||
| 193 | /** {@inheritdoc} */ |
||
| 194 | public function setLogin($login) |
||
| 199 | |||
| 200 | /** {@inheritdoc} */ |
||
| 201 | public function getLogin() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function setRole($role) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function getRole() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function getRoleId() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | public function setInfo(InfoInterface $info) |
||
| 242 | |||
| 243 | /** {@inheritdoc} */ |
||
| 244 | public function getInfo() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param $key |
||
| 254 | * @param $sessionParameter |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | public function updateAuthSession($key, $sessionParameter) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param $key |
||
| 279 | * @return null |
||
|
|
|||
| 280 | */ |
||
| 281 | public function getAuthSession($key) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * removes a stored Session |
||
| 296 | * @param string|null $key providerName, if null, remove all sessions |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function removeSessionData($key = null) |
||
| 311 | |||
| 312 | /** {@inheritdoc} */ |
||
| 313 | public function getCredential() |
||
| 317 | |||
| 318 | /** {@inheritdoc} */ |
||
| 319 | public function setPassword($password) |
||
| 325 | |||
| 326 | /** {@inheritdoc} */ |
||
| 327 | public function setCredential($credential) |
||
| 332 | |||
| 333 | /** {@inheritdoc} */ |
||
| 334 | public function getSecret() |
||
| 341 | |||
| 342 | /** {@inheritdoc} */ |
||
| 343 | public function setSecret($secret) |
||
| 348 | |||
| 349 | /** {@inheritdoc} */ |
||
| 350 | public function getEmail() |
||
| 354 | |||
| 355 | /** {@inheritdoc} */ |
||
| 356 | public function setEmail($email) |
||
| 361 | |||
| 362 | /** {@inheritdoc} */ |
||
| 363 | public function setProfile(array $profile) |
||
| 368 | |||
| 369 | /** {@inheritdoc} */ |
||
| 370 | public function getProfile($provider = null) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $provider |
||
| 382 | * @param array $data |
||
| 383 | * @return \Auth\Entity\User |
||
| 384 | */ |
||
| 385 | public function addProfile($provider, array $data) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $provider |
||
| 393 | * @return \Auth\Entity\User |
||
| 394 | */ |
||
| 395 | public function removeProfile($provider) |
||
| 400 | |||
| 401 | /** {@inheritdoc} */ |
||
| 402 | public function setSettingsEntityResolver($resolver) |
||
| 406 | |||
| 407 | /** {@inheritdoc} */ |
||
| 408 | public function getSettings($module) |
||
| 428 | |||
| 429 | /** {@inheritdoc} */ |
||
| 430 | public function getGroups() |
||
| 437 | |||
| 438 | /** {@inheritdoc} */ |
||
| 439 | public function getGroup($name, $create = false) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return Collection |
||
| 458 | */ |
||
| 459 | public function getTokens() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param Collection $tokens |
||
| 470 | */ |
||
| 471 | public function setTokens($tokens) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param OrganizationReferenceInterface $organization |
||
| 478 | * @return $this |
||
| 479 | */ |
||
| 480 | public function setOrganization(OrganizationReferenceInterface $organization) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | public function hasOrganization() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return OrganizationReferenceInterface |
||
| 499 | */ |
||
| 500 | public function getOrganization() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return Status |
||
| 507 | */ |
||
| 508 | public function getStatus() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @param Status $status |
||
| 519 | */ |
||
| 520 | public function setStatus($status) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return boolean |
||
| 531 | */ |
||
| 532 | public function isActive() |
||
| 536 | } |
||
| 537 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.