| Total Complexity | 42 |
| Total Lines | 369 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 | #[ORM\Entity(repositoryClass: UserRepository::class)] |
||
| 33 | #[ORM\Table(name: 'users')] |
||
| 34 | #[ |
||
| 35 | ORM\Index(fields: ['uname'], name: 'uname'), |
||
| 36 | ORM\Index(fields: ['email'], name: 'email') |
||
| 37 | ] |
||
| 38 | #[ZikulaAssert\ValidUserFields] |
||
| 39 | class User |
||
| 40 | { |
||
| 41 | #[ORM\Id] |
||
| 42 | #[ORM\Column] |
||
| 43 | #[ORM\GeneratedValue(strategy: 'AUTO')] |
||
| 44 | private int $uid; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Username: Primary user display name. |
||
| 48 | */ |
||
| 49 | #[ORM\Column(length: 25)] |
||
| 50 | #[Assert\Length(min: 1, max: 25)] |
||
| 51 | #[ZikulaAssert\ValidUname] |
||
| 52 | private string $uname; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * E-mail address: For user notifications. |
||
| 56 | */ |
||
| 57 | #[ORM\Column(length: 60)] |
||
| 58 | #[Assert\Length(min: 1, max: 60)] |
||
| 59 | #[ZikulaAssert\ValidEmail] |
||
| 60 | private string $email; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Account State: The user's current state, see \Zikula\UsersBundle\Constant::ACTIVATED_* for defined constants. |
||
| 64 | * 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. |
||
| 65 | * For example, user accounts pending the completion of the registration process (because either moderation, e-mail verification, or both are in use) |
||
| 66 | * 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 |
||
| 67 | * denies the request for an new account, the user account record will be deleted. |
||
| 68 | * When this deletion happens, it will be assumed by the system that no external module has yet interacted with the user account record, |
||
| 69 | * because its state never progressed beyond its pending state, and therefore normal events may not be triggered |
||
| 70 | * (although it is possible that events regarding the pending account may be triggered). |
||
| 71 | */ |
||
| 72 | #[Assert\Choice(choices: UsersConstant::ACTIVATED_OPTIONS)] |
||
| 73 | #[ORM\Column(type: Types::SMALLINT)] |
||
| 74 | private int $activated; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Account Approved Date/Time: The date and time the user's registration request was approved through the moderation process. |
||
| 78 | * 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. |
||
| 79 | * 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). |
||
| 80 | * WARNING: The date and time related functions available in SQL on many RDBMS servers are highly dependent on the database server's timezone setting. |
||
| 81 | * 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. |
||
| 82 | * 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. |
||
| 83 | * If SQL date/time functions must be used, then care should be taken to ensure that either the function is time zone neutral, |
||
| 84 | * or that the function and its relationship to time zone settings is completely understood. |
||
| 85 | */ |
||
| 86 | #[ORM\Column(name: 'approved_date', type: 'utcdatetime')] |
||
| 87 | private DateTimeInterface $approvedDate; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The uid of the user account that approved the request to register a new account. |
||
| 91 | * 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. |
||
| 92 | * 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. |
||
| 93 | */ |
||
| 94 | #[ORM\Column(name: 'approved_by')] |
||
| 95 | private int $approvedBy; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Registration Date/Time: Date/time the user account was registered. |
||
| 99 | * For users not pending the completion of the registration process, this is the date and time the user account completed the process. |
||
| 100 | * For example, if registrations are moderated, then this is the date and time the registration request was approved. |
||
| 101 | * If registration e-mail addresses must be verified, then this is the date and time the user completed the verification process. |
||
| 102 | * If both moderation and verification are in use, then this is the later of those two dates. |
||
| 103 | * If neither is in use, then this is simply the date and time the user's registration request was made. |
||
| 104 | * If the user account's activated state is "pending registration" (implying that either moderation, verification, or both are in use) |
||
| 105 | * 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. |
||
| 106 | * 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. |
||
| 107 | * See WARNING under approvedDate above. |
||
| 108 | */ |
||
| 109 | #[ORM\Column(name: 'user_regdate', type: 'utcdatetime')] |
||
| 110 | private DateTimeInterface $registrationDate; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Last Login Date/Time: Date/time user last successfully logged into the site. |
||
| 114 | * 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. |
||
| 115 | * See WARNING under approvedDate above. |
||
| 116 | */ |
||
| 117 | #[ORM\Column(name: 'lastlogin', type: 'utcdatetime')] |
||
| 118 | private DateTimeInterface $lastLogin; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 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. |
||
| 122 | * Optional, if blank then the system default timezone should be used. [FUTURE USE] |
||
| 123 | */ |
||
| 124 | #[ORM\Column(length: 30)] |
||
| 125 | #[Assert\AtLeastOneOf([ |
||
| 126 | new Assert\Blank(), |
||
| 127 | new Assert\Length(min: 1, max: 30) |
||
| 128 | ])] |
||
| 129 | private string $tz; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The user's chosen locale for i18n purposes, as defined by gettext, POSIX, and the Common Locale Data Repository; |
||
| 133 | * Optional, if blank then the system default locale should be used. |
||
| 134 | */ |
||
| 135 | #[ORM\Column(length: 5)] |
||
| 136 | #[Assert\AtLeastOneOf([ |
||
| 137 | new Assert\Blank(), |
||
| 138 | new Assert\Length(min: 1, max: 5) |
||
| 139 | ])] |
||
| 140 | private string $locale; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Additional attributes of this user |
||
| 144 | */ |
||
| 145 | #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAttribute::class, cascade: ['all'], orphanRemoval: true, indexBy: 'name')] |
||
| 146 | private Collection $attributes; |
||
| 147 | |||
| 148 | #[ORM\ManyToMany(targetEntity: Group::class, inversedBy: 'users', indexBy: 'gid')] |
||
| 149 | #[ORM\JoinTable(name: 'group_membership')] |
||
| 150 | #[ORM\JoinColumn(name: 'uid', referencedColumnName: 'uid')] |
||
| 151 | #[ORM\InverseJoinColumn(name: 'gid', referencedColumnName: 'gid')] |
||
| 152 | /** @var Group[] */ |
||
| 153 | private Collection $groups; |
||
| 154 | |||
| 155 | public function __construct() |
||
| 156 | { |
||
| 157 | $this->uname = ''; |
||
| 158 | $this->email = ''; |
||
| 159 | $this->activated = 0; |
||
| 160 | $utcTZ = new \DateTimeZone('UTC'); |
||
| 161 | $this->approvedDate = new DateTime('1970-01-01 00:00:00', $utcTZ); |
||
| 162 | $this->approvedBy = 0; |
||
| 163 | $this->registrationDate = new DateTime('1970-01-01 00:00:00', $utcTZ); |
||
| 164 | $this->lastLogin = new DateTime('1970-01-01 00:00:00', $utcTZ); |
||
| 165 | $this->tz = ''; |
||
| 166 | $this->locale = ''; |
||
| 167 | |||
| 168 | $this->attributes = new ArrayCollection(); |
||
| 169 | $this->groups = new ArrayCollection(); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function getUid(): ?int |
||
| 173 | { |
||
| 174 | return $this->uid; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function setUid(int $uid): self |
||
| 178 | { |
||
| 179 | $this->uid = $uid; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function getUname(): string |
||
| 185 | { |
||
| 186 | return $this->uname; |
||
| 187 | } |
||
| 188 | |||
| 189 | public function getUsername(): string |
||
| 190 | { |
||
| 191 | return $this->getUname(); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function setUname(string $uname): self |
||
| 195 | { |
||
| 196 | $this->uname = $uname; |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getEmail(): string |
||
| 202 | { |
||
| 203 | return $this->email; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function setEmail(string $email): self |
||
| 207 | { |
||
| 208 | $this->email = $email; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getActivated(): int |
||
| 214 | { |
||
| 215 | return $this->activated; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function setActivated(int $activated): self |
||
| 219 | { |
||
| 220 | $this->activated = $activated; |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function getApprovedDate(): DateTimeInterface |
||
| 226 | { |
||
| 227 | return $this->approvedDate; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function setApprovedDate(string|DateTimeInterface $approvedDate): self |
||
| 231 | { |
||
| 232 | if ($approvedDate instanceof DateTimeInterface) { |
||
| 233 | $this->approvedDate = $approvedDate; |
||
| 234 | } else { |
||
| 235 | $this->approvedDate = new DateTime($approvedDate); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function getApprovedBy(): int |
||
| 242 | { |
||
| 243 | return $this->approvedBy; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function setApprovedBy(int $approvedBy): self |
||
| 247 | { |
||
| 248 | $this->approvedBy = $approvedBy; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function isApproved(): bool |
||
| 254 | { |
||
| 255 | return 0 !== $this->approvedBy; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getRegistrationDate(): DateTimeInterface |
||
| 259 | { |
||
| 260 | return $this->registrationDate; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function setRegistrationDate(string|DateTimeInterface $registrationDate): self |
||
| 264 | { |
||
| 265 | if ($registrationDate instanceof DateTimeInterface) { |
||
| 266 | $this->registrationDate = $registrationDate; |
||
| 267 | } else { |
||
| 268 | $this->registrationDate = new DateTime($registrationDate); |
||
| 269 | } |
||
| 270 | |||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function getLastLogin(): DateTimeInterface |
||
| 275 | { |
||
| 276 | return $this->lastLogin; |
||
| 277 | } |
||
| 278 | |||
| 279 | public function setLastLogin(string|DateTimeInterface $lastLogin): self |
||
| 280 | { |
||
| 281 | if ($lastLogin instanceof DateTimeInterface) { |
||
| 282 | $this->lastLogin = $lastLogin; |
||
| 283 | } else { |
||
| 284 | $this->lastLogin = new DateTime($lastLogin); |
||
| 285 | } |
||
| 286 | |||
| 287 | return $this; |
||
| 288 | } |
||
| 289 | |||
| 290 | public function getTz(): string |
||
| 291 | { |
||
| 292 | return $this->tz; |
||
| 293 | } |
||
| 294 | |||
| 295 | public function setTz(string $tz): self |
||
| 296 | { |
||
| 297 | $this->tz = $tz; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | public function getLocale(): string |
||
| 303 | { |
||
| 304 | return $this->locale; |
||
| 305 | } |
||
| 306 | |||
| 307 | public function setLocale(string $locale): self |
||
| 308 | { |
||
| 309 | $this->locale = $locale; |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | public function getAttributes(): Collection |
||
| 315 | { |
||
| 316 | return $this->attributes; |
||
| 317 | } |
||
| 318 | |||
| 319 | public function getAttributeValue(string $name): string |
||
| 322 | } |
||
| 323 | |||
| 324 | public function setAttributes(ArrayCollection $attributes): self |
||
| 325 | { |
||
| 326 | $this->attributes = $attributes; |
||
| 327 | |||
| 328 | return $this; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param mixed $value |
||
| 333 | */ |
||
| 334 | public function setAttribute(string $name, $value): self |
||
| 335 | { |
||
| 336 | if (isset($this->attributes[$name])) { |
||
| 337 | $this->attributes[$name]->setValue($value); |
||
| 338 | } else { |
||
| 339 | $this->attributes[$name] = new UserAttribute($this, $name, $value); |
||
| 340 | } |
||
| 341 | |||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | public function delAttribute(string $name): self |
||
| 346 | { |
||
| 347 | if (isset($this->attributes[$name])) { |
||
| 348 | $this->attributes->remove($name); |
||
| 349 | } |
||
| 350 | |||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | public function hasAttribute(string $name): bool |
||
| 355 | { |
||
| 356 | return $this->attributes->containsKey($name); |
||
| 357 | } |
||
| 358 | |||
| 359 | public function getGroups(): Collection |
||
| 360 | { |
||
| 361 | return $this->groups; |
||
| 362 | } |
||
| 363 | |||
| 364 | public function setGroups(ArrayCollection $groups): self |
||
| 369 | } |
||
| 370 | |||
| 371 | public function addGroup(Group $group): self |
||
| 372 | { |
||
| 373 | $group->addUser($this); |
||
| 374 | $this->groups[] = $group; |
||
| 375 | |||
| 376 | return $this; |
||
| 377 | } |
||
| 378 | |||
| 379 | public function removeGroup(Group $group): self |
||
| 380 | { |
||
| 381 | $group->removeUser($this); |
||
| 382 | $this->groups->removeElement($group); |
||
| 385 | } |
||
| 386 | |||
| 387 | public function removeGroups(): self |
||
| 388 | { |
||
| 389 | /** @var Group $group */ |
||
| 396 | } |
||
| 397 | |||
| 398 | public function __toString(): string |
||
| 399 | { |
||
| 400 | return $this->getUsername(); |
||
| 401 | } |
||
| 402 | } |
||
| 403 |