Complex classes like UserEntity 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 UserEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class UserEntity extends EntityAccess |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * User ID: Primary user identifier |
||
| 37 | * |
||
| 38 | * @ORM\Id |
||
| 39 | * @ORM\Column(type="integer") |
||
| 40 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 41 | */ |
||
| 42 | private $uid; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * User Name: Primary user display name. |
||
| 46 | * |
||
| 47 | * @ZikulaAssert\ValidUname() |
||
| 48 | * @ORM\Column(type="string", length=25) |
||
| 49 | */ |
||
| 50 | private $uname; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * E-mail Address: For user notifications. |
||
| 54 | * |
||
| 55 | * @ZikulaAssert\ValidEmail() |
||
| 56 | * @ORM\Column(type="string", length=60) |
||
| 57 | */ |
||
| 58 | private $email; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Account State: The user's current state, see \Zikula\UsersModule\Constant::ACTIVATED_* for defined constants. |
||
| 62 | * A state represented by a negative integer means that the user's account is in a pending state, and should not yet be considered a "real" user account. |
||
| 63 | * For example, user accounts pending the completion of the registration process (because either moderation, e-mail verification, or both are in use) |
||
| 64 | * will have a negative integer representing their state. If the user's registration request expires before it the process is completed, or if the administrator |
||
| 65 | * denies the request for an new account, the user account record will be deleted. |
||
| 66 | * When this deletion happens, it will be assumed by the system that no external module has yet interacted with the user account record, |
||
| 67 | * because its state never progressed beyond its pending state, and therefore normal hooks/events may not be triggered |
||
| 68 | * (although it is possible that events regarding the pending account may be triggered). |
||
| 69 | * |
||
| 70 | * @Assert\Choice(callback = "getActivatedValues") |
||
| 71 | * @ORM\Column(type="smallint") |
||
| 72 | */ |
||
| 73 | private $activated; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Account Approved Date/Time: The date and time the user's registration request was approved through the moderation process. |
||
| 77 | * If the moderation process was not in effect at the time the user made a registration request, then this will be the date and time of the registration request. |
||
| 78 | * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time (unless one or the other happens to be UTC). |
||
| 79 | * WARNING: The date and time related functions available in SQL on many RDBMS servers are highly dependent on the database server's timezone setting. |
||
| 80 | * All parameters to these functions are treated as if the dates and times they represent are in the time zone that is set in the database server's settings. |
||
| 81 | * Use of date/time functions in SQL queries should be avoided if at all possible. PHP functions using UTC as the base time zone should be used instead. |
||
| 82 | * If SQL date/time functions must be used, then care should be taken to ensure that either the function is time zone neutral, |
||
| 83 | * or that the function and its relationship to time zone settings is completely understood. |
||
| 84 | * |
||
| 85 | * @Assert\DateTime() |
||
| 86 | * @ORM\Column(type="utcdatetime") |
||
| 87 | */ |
||
| 88 | private $approved_date; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The uid of the user account that approved the request to register a new account. |
||
| 92 | * If this is the same as the user account's uid, then moderation was not in use at the time the request for a new account was made. |
||
| 93 | * If this is -1, the the user account that approved the request has since been deleted. If this is 0, the user account has not yet been approved. |
||
| 94 | * |
||
| 95 | * @Assert\Type(type="integer") |
||
| 96 | * @ORM\Column(type="integer") |
||
| 97 | */ |
||
| 98 | private $approved_by; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Registration Date/Time: Date/time the user account was registered. |
||
| 102 | * For users not pending the completion of the registration process, this is the date and time the user account completed the process. |
||
| 103 | * For example, if registrations are moderated, then this is the date and time the registration request was approved. |
||
| 104 | * If registration e-mail addresses must be verified, then this is the date and time the user completed the verification process. |
||
| 105 | * If both moderation and verification are in use, then this is the later of those two dates. |
||
| 106 | * If neither is in use, then this is simply the date and time the user's registration request was made. |
||
| 107 | * If the user account's activated state is "pending registration" (implying that either moderation, verification, or both are in use) |
||
| 108 | * then this will be the date and time the user made the registration request UNTIL the registration process is complete, and then it is updated as above. |
||
| 109 | * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time. SEE WARNING under approved_date, above. |
||
| 110 | * |
||
| 111 | * @Assert\DateTime() |
||
| 112 | * @ORM\Column(type="utcdatetime") |
||
| 113 | */ |
||
| 114 | private $user_regdate; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Last Login Date/Time: Date/time user last successfully logged into the site. |
||
| 118 | * NOTE: This is stored as an SQL datetime, using the UTC time zone. The date/time is NEITHER server local time nor user local time. SEE WARNING under approved_date, above. |
||
| 119 | * |
||
| 120 | * @Assert\DateTime() |
||
| 121 | * @ORM\Column(type="utcdatetime") |
||
| 122 | */ |
||
| 123 | private $lastlogin; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * User's timezone, as supported by PHP (listed at http://us2.php.net/manual/en/timezones.php), and as expressed by the Olson tz database. |
||
| 127 | * Optional, if blank then the system default timezone should be used. [FUTURE USE] |
||
| 128 | * |
||
| 129 | * @Assert\Type(type="string") |
||
| 130 | * @ORM\Column(type="string", length=30) |
||
| 131 | */ |
||
| 132 | private $tz; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The user's chosen locale for i18n purposes, as defined by gettext, POSIX, and the Common Locale Data Repository; |
||
| 136 | * Optional, if blank then the system default locale should be used. |
||
| 137 | * |
||
| 138 | * @Assert\Type(type="string") |
||
| 139 | * @ORM\Column(type="string", length=5) |
||
| 140 | */ |
||
| 141 | private $locale; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Additional attributes of this user |
||
| 145 | * |
||
| 146 | * @ORM\OneToMany(targetEntity="UserAttributeEntity", |
||
| 147 | * mappedBy="user", |
||
| 148 | * cascade={"all"}, |
||
| 149 | * orphanRemoval=true, |
||
| 150 | * indexBy="name") |
||
| 151 | */ |
||
| 152 | private $attributes; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @ORM\ManyToMany(targetEntity="Zikula\GroupsModule\Entity\GroupEntity", inversedBy="users", indexBy="gid") |
||
| 156 | * @ORM\JoinTable(name="group_membership", |
||
| 157 | * joinColumns={@ORM\JoinColumn(name="uid", referencedColumnName="uid")}, |
||
| 158 | * inverseJoinColumns={@ORM\JoinColumn(name="gid", referencedColumnName="gid")} |
||
| 159 | * ) |
||
| 160 | **/ |
||
| 161 | private $groups; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * constructor |
||
| 165 | */ |
||
| 166 | public function __construct() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * get the uid of the user |
||
| 184 | * |
||
| 185 | * @return integer the user's uid |
||
| 186 | */ |
||
| 187 | public function getUid() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * set the uid for the user |
||
| 194 | * |
||
| 195 | * @param integer $uid the user's uid |
||
| 196 | */ |
||
| 197 | public function setUid($uid) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * get the uname of the user |
||
| 204 | * |
||
| 205 | * @return string the user's uname |
||
| 206 | */ |
||
| 207 | public function getUname() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * get the username of the user |
||
| 214 | * |
||
| 215 | * @return string the user's name |
||
| 216 | */ |
||
| 217 | public function getUsername() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * set the uname for the user |
||
| 224 | * |
||
| 225 | * @param string $uname the user's uname |
||
| 226 | */ |
||
| 227 | public function setUname($uname) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * get the email of the user |
||
| 234 | * |
||
| 235 | * @return string the user's email |
||
| 236 | */ |
||
| 237 | public function getEmail() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * set the email for the user |
||
| 244 | * |
||
| 245 | * @param string $email the user's email |
||
| 246 | */ |
||
| 247 | public function setEmail($email) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * get the activation status of the user |
||
| 254 | * |
||
| 255 | * @return integer the user's activation status |
||
| 256 | */ |
||
| 257 | public function getActivated() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * set the activation status for the user |
||
| 264 | * |
||
| 265 | * @param integer $activated the user's activation status |
||
| 266 | */ |
||
| 267 | public function setActivated($activated) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * get the approved date of the user |
||
| 274 | * |
||
| 275 | * @return \Datetime the user's approved date |
||
| 276 | */ |
||
| 277 | public function getApproved_Date() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * set the approved date for the user |
||
| 284 | * |
||
| 285 | * @param \Datetime $approved_date the user's approved date |
||
| 286 | */ |
||
| 287 | public function setApproved_Date($approved_date) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * get the user id who approved the user |
||
| 299 | * |
||
| 300 | * @return integer the user's id who approved the user |
||
| 301 | */ |
||
| 302 | public function getApproved_By() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * set the user id who approved the user |
||
| 309 | * |
||
| 310 | * @param integer $approved_by the user's id who approved the user |
||
| 311 | */ |
||
| 312 | public function setApproved_By($approved_by) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function isApproved() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * get the regdate of the user |
||
| 327 | * |
||
| 328 | * @return \Datetime the user's regdate |
||
| 329 | */ |
||
| 330 | public function getUser_Regdate() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * set the regdate for the user |
||
| 337 | * |
||
| 338 | * @param \Datetime $user_regdate the user's regdate |
||
| 339 | */ |
||
| 340 | public function setUser_Regdate($user_regdate) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * get the last login of the user |
||
| 352 | * |
||
| 353 | * @return \Datetime the user's last login |
||
| 354 | */ |
||
| 355 | public function getLastlogin() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * set the last login for the user |
||
| 362 | * |
||
| 363 | * @param \Datetime $lastlogin the user's last login |
||
| 364 | */ |
||
| 365 | public function setLastlogin($lastlogin) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * get the tz of the user |
||
| 377 | * |
||
| 378 | * @return string the user's tz |
||
| 379 | */ |
||
| 380 | public function getTz() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * set the tz for the user |
||
| 387 | * |
||
| 388 | * @param string $tz the user's tz |
||
| 389 | */ |
||
| 390 | public function setTz($tz) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * get the locale of the user |
||
| 397 | * |
||
| 398 | * @return string the user's locale |
||
| 399 | */ |
||
| 400 | public function getLocale() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * set the locale for the user |
||
| 407 | * |
||
| 408 | * @param string $locale the user's locale |
||
| 409 | */ |
||
| 410 | public function setLocale($locale) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * get the attributes of the user |
||
| 417 | * |
||
| 418 | * @return ArrayCollection UserAttributeEntity[] of the user's attributes |
||
| 419 | */ |
||
| 420 | public function getAttributes() |
||
| 424 | |||
| 425 | public function getAttributeValue($name) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * set the attributes for the user |
||
| 432 | * |
||
| 433 | * @param UserAttributeEntity $attributes the attributes for the user |
||
| 434 | */ |
||
| 435 | public function setAttributes($attributes) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * set a single attribute for the user |
||
| 442 | * |
||
| 443 | * @param $name string attribute name |
||
| 444 | * @param $value string attribute value |
||
| 445 | */ |
||
| 446 | public function setAttribute($name, $value) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * delete a single attribute of the user |
||
| 457 | * |
||
| 458 | * @param $name string attribute name |
||
| 459 | */ |
||
| 460 | public function delAttribute($name) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param $name |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function hasAttribute($name) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return ArrayCollection |
||
| 478 | */ |
||
| 479 | public function getGroups() |
||
| 483 | |||
| 484 | public function setGroups(ArrayCollection $groups) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * UserEntity is the 'Owning side' |
||
| 491 | * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association |
||
| 492 | * @param GroupEntity $group |
||
| 493 | */ |
||
| 494 | public function addGroup(GroupEntity $group) |
||
| 499 | |||
| 500 | public function removeGroup(GroupEntity $group) |
||
| 505 | |||
| 506 | public function removeGroups() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Callback function used to validate the activated value |
||
| 517 | * @return array |
||
| 518 | */ |
||
| 519 | public static function getActivatedValues() |
||
| 528 | } |
||
| 529 |