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 |
||
| 24 | class User extends AbstractIdentifiableEntity implements UserInterface, DraftableEntityInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Users login name |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | * @ODM\Field(type="string") |
||
| 31 | * @ODM\Index(unique=true, sparse=true, order="asc") |
||
| 32 | */ |
||
| 33 | protected $login; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Role of an user. Currently "user" or "recruiter" |
||
| 37 | * |
||
| 38 | * @ODM\Field(type="string") |
||
| 39 | */ |
||
| 40 | protected $role; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Users contact data. |
||
| 44 | * |
||
| 45 | * @ODM\EmbedOne(targetDocument="Info") |
||
| 46 | */ |
||
| 47 | protected $info; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Authentification Sessions like oAuth |
||
| 51 | * After Authentification with OAuth sessions can be stored like a password/key pair |
||
| 52 | * |
||
| 53 | * @ODM\EmbedMany(targetDocument="AuthSession") |
||
| 54 | */ |
||
| 55 | protected $authSessions; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Users login password |
||
| 59 | * |
||
| 60 | * @ODM\Field(type="string") |
||
| 61 | */ |
||
| 62 | protected $credential; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Users primary email address |
||
| 66 | * |
||
| 67 | * @ODM\Field(type="string") |
||
| 68 | */ |
||
| 69 | protected $email; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * pre-shared key, which allows an external application to authenticate |
||
| 73 | * |
||
| 74 | * @var String |
||
| 75 | * @ODM\Field(type="string") |
||
| 76 | */ |
||
| 77 | protected $secret; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Can contain various HybridAuth profiles. |
||
| 81 | * Deprecated: replaced by User::$profiles |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | * @deprecated |
||
| 85 | * @ODM\Hash |
||
| 86 | */ |
||
| 87 | protected $profile = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Can contain various HybridAuth profiles. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | * @ODM\Hash |
||
| 94 | */ |
||
| 95 | protected $profiles = []; |
||
| 96 | |||
| 97 | /** @var array |
||
| 98 | * @ODM\EmbedMany(discriminatorField="_entity") |
||
| 99 | */ |
||
| 100 | protected $settings; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This is not a persistent property! |
||
| 104 | * |
||
| 105 | * @var SettingsEntityResolver |
||
| 106 | */ |
||
| 107 | protected $settingsEntityResolver; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * User groups. |
||
| 111 | * |
||
| 112 | * @var Collection |
||
| 113 | * @ODM\ReferenceMany(targetDocument="Group", mappedBy="owner", simple=true, cascade="all") |
||
| 114 | */ |
||
| 115 | protected $groups; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * User tokens. Is generated when recovering Passwords as a short term key. |
||
| 119 | * |
||
| 120 | * @var Collection |
||
| 121 | * @ODM\EmbedMany(targetDocument="Token") |
||
| 122 | */ |
||
| 123 | protected $tokens; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The organization reference for the user. |
||
| 127 | * |
||
| 128 | * This field is not stored in the database, but injected on postLoad via |
||
| 129 | * {@link \Organizations\Repository\Event\InjectOrganizationReferenceListener} |
||
| 130 | * |
||
| 131 | * @var OrganizationReferenceInterface |
||
| 132 | * |
||
| 133 | * @since 0.18 |
||
| 134 | */ |
||
| 135 | protected $organization; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Is this entity a draft or not? |
||
| 139 | * |
||
| 140 | * @var bool |
||
| 141 | * @ODM\Boolean |
||
| 142 | */ |
||
| 143 | protected $isDraft = false; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @see http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/best-practices.html |
||
| 147 | * It is recommended best practice to initialize any business collections in documents in the constructor. |
||
| 148 | * {mg: What about lazy loading? Initialize the Collection in the getter, if none is set? Reduce overload.} |
||
| 149 | */ |
||
| 150 | public function __construct() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | public function isDraft() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param bool $flag |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function setIsDraft($flag) |
||
| 172 | |||
| 173 | |||
| 174 | /** {@inheritdoc} */ |
||
| 175 | public function setLogin($login) |
||
| 180 | |||
| 181 | /** {@inheritdoc} */ |
||
| 182 | public function getLogin() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function setRole($role) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function getRole() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function getRoleId() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function setInfo(InfoInterface $info) |
||
| 223 | |||
| 224 | /** {@inheritdoc} */ |
||
| 225 | public function getInfo() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param $key |
||
| 235 | * @param $sessionParameter |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function updateAuthSession($key, $sessionParameter) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $key |
||
| 260 | * @return null |
||
|
|
|||
| 261 | */ |
||
| 262 | public function getAuthSession($key) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * removes a stored Session |
||
| 277 | * @param string|null $key providerName, if null, remove all sessions |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function removeSessionData($key = null) |
||
| 292 | |||
| 293 | /** {@inheritdoc} */ |
||
| 294 | public function getCredential() |
||
| 298 | |||
| 299 | /** {@inheritdoc} */ |
||
| 300 | public function setPassword($password) |
||
| 306 | |||
| 307 | /** {@inheritdoc} */ |
||
| 308 | public function setCredential($credential) |
||
| 313 | |||
| 314 | /** {@inheritdoc} */ |
||
| 315 | public function getSecret() |
||
| 322 | |||
| 323 | /** {@inheritdoc} */ |
||
| 324 | public function setSecret($secret) |
||
| 329 | |||
| 330 | /** {@inheritdoc} */ |
||
| 331 | public function getEmail() |
||
| 335 | |||
| 336 | /** {@inheritdoc} */ |
||
| 337 | public function setEmail($email) |
||
| 342 | |||
| 343 | /** {@inheritdoc} */ |
||
| 344 | public function setProfile(array $profile) |
||
| 349 | |||
| 350 | /** {@inheritdoc} */ |
||
| 351 | public function getProfile($provider = null) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $provider |
||
| 363 | * @param array $data |
||
| 364 | * @return \Auth\Entity\User |
||
| 365 | */ |
||
| 366 | public function addProfile($provider, array $data) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $provider |
||
| 374 | * @return \Auth\Entity\User |
||
| 375 | */ |
||
| 376 | public function removeProfile($provider) |
||
| 381 | |||
| 382 | /** {@inheritdoc} */ |
||
| 383 | public function setSettingsEntityResolver($resolver) |
||
| 387 | |||
| 388 | /** {@inheritdoc} */ |
||
| 389 | public function getSettings($module) |
||
| 409 | |||
| 410 | /** {@inheritdoc} */ |
||
| 411 | public function getGroups() |
||
| 418 | |||
| 419 | /** {@inheritdoc} */ |
||
| 420 | public function getGroup($name, $create = false) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return Collection |
||
| 439 | */ |
||
| 440 | public function getTokens() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param Collection $tokens |
||
| 451 | */ |
||
| 452 | public function setTokens($tokens) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param OrganizationReferenceInterface $organization |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | public function setOrganization(OrganizationReferenceInterface $organization) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function hasOrganization() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return OrganizationReferenceInterface |
||
| 480 | */ |
||
| 481 | public function getOrganization() |
||
| 485 | } |
||
| 486 |
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.