| Total Complexity | 44 |
| Total Lines | 551 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 76 | class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface |
||
| 77 | { |
||
| 78 | /** The User id of the anonymous user */ |
||
| 79 | public const ID_ANONYMOUS = 1; |
||
| 80 | |||
| 81 | public const AVAILABLE_THEMES = ['bootstrap', 'cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal', |
||
| 82 | 'litera', 'lumen', 'lux', 'materia', 'minty', 'pulse', 'sandstone', 'simplex', 'sketchy', 'slate', 'solar', |
||
| 83 | 'spacelab', 'united', 'yeti', ]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var Collection|UserAttachment[] |
||
| 87 | * @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
||
| 88 | */ |
||
| 89 | protected $attachments; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @ORM\Id() |
||
| 93 | * @ORM\GeneratedValue() |
||
| 94 | * @ORM\Column(type="integer") |
||
| 95 | */ |
||
| 96 | protected $id; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 100 | * @Assert\NotBlank |
||
| 101 | */ |
||
| 102 | protected $name = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * //@ORM\Column(type="json"). |
||
| 106 | */ |
||
| 107 | //protected $roles = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string|null The hashed password |
||
| 111 | * @ORM\Column(type="string", nullable=true) |
||
| 112 | */ |
||
| 113 | protected $password; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var bool True if the user needs to change password after log in |
||
| 117 | * @ORM\Column(type="boolean") |
||
| 118 | */ |
||
| 119 | protected $need_pw_change = true; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string|null The first name of the User |
||
| 123 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 124 | */ |
||
| 125 | protected $first_name = ''; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string|null The last name of the User |
||
| 129 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 130 | */ |
||
| 131 | protected $last_name = ''; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string|null The department the user is working |
||
| 135 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 136 | */ |
||
| 137 | protected $department = ''; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var string|null The email address of the user |
||
| 141 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 142 | * @Assert\Email() |
||
| 143 | */ |
||
| 144 | protected $email = ''; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string|null The language/locale the user prefers |
||
| 148 | * @ORM\Column(type="string", name="config_language", nullable=true) |
||
| 149 | * @Assert\Language() |
||
| 150 | */ |
||
| 151 | protected $language = ''; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string|null The timezone the user prefers |
||
| 155 | * @ORM\Column(type="string", name="config_timezone", nullable=true) |
||
| 156 | * @Assert\Timezone() |
||
| 157 | */ |
||
| 158 | protected $timezone = ''; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string|null The theme |
||
| 162 | * @ORM\Column(type="string", name="config_theme", nullable=true) |
||
| 163 | * @Assert\Choice(choices=User::AVAILABLE_THEMES) |
||
| 164 | */ |
||
| 165 | protected $theme = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var Group|null the group this user belongs to |
||
| 169 | * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER") |
||
| 170 | * @ORM\JoinColumn(name="group_id", referencedColumnName="id") |
||
| 171 | * @Selectable() |
||
| 172 | */ |
||
| 173 | protected $group; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var array |
||
| 177 | * @ORM\Column(type="json") |
||
| 178 | */ |
||
| 179 | protected $settings = []; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var Currency|null The currency the user wants to see prices in. |
||
| 183 | * Dont use fetch=EAGER here, this will cause problems with setting the currency setting. |
||
| 184 | * TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call). |
||
| 185 | * TODO: Find a way to use fetch EAGER (this improves performance a bit) |
||
| 186 | * @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency") |
||
| 187 | * @ORM\JoinColumn(name="currency_id", referencedColumnName="id") |
||
| 188 | * @Selectable() |
||
| 189 | */ |
||
| 190 | protected $currency = null; |
||
| 191 | |||
| 192 | /** @var PermissionsEmbed |
||
| 193 | * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") |
||
| 194 | * @ValidPermission() |
||
| 195 | */ |
||
| 196 | protected $permissions; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @ORM\Column(type="text", name="config_instock_comment_w") |
||
| 200 | */ |
||
| 201 | protected $instock_comment_w = ''; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @ORM\Column(type="text", name="config_instock_comment_a") |
||
| 205 | */ |
||
| 206 | protected $instock_comment_a = ''; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var string|null The hash of a token the user must provide when he wants to reset his password. |
||
| 210 | * @ORM\Column(type="string", nullable=true) |
||
| 211 | */ |
||
| 212 | protected $pw_reset_token = null; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @var \DateTime The time until the password reset token is valid. |
||
| 216 | * @ORM\Column(type="datetime", nullable=true) |
||
| 217 | */ |
||
| 218 | protected $pw_reset_expires = null; |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @var bool Determines if the user is disabled (user can not log in) |
||
| 222 | * @ORM\Column(type="boolean") |
||
| 223 | */ |
||
| 224 | protected $disabled = false; |
||
| 225 | |||
| 226 | public function __construct() |
||
| 227 | { |
||
| 228 | parent::__construct(); |
||
| 229 | $this->permissions = new PermissionsEmbed(); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Checks if the current user, is the user which represents the not logged in (anonymous) users. |
||
| 234 | * |
||
| 235 | * @return bool true if this user is the anonymous user |
||
| 236 | */ |
||
| 237 | public function isAnonymousUser(): bool |
||
| 238 | { |
||
| 239 | return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * A visual identifier that represents this user. |
||
| 244 | * |
||
| 245 | * @see UserInterface |
||
| 246 | */ |
||
| 247 | public function getUsername(): string |
||
| 248 | { |
||
| 249 | return (string) $this->name; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @see UserInterface |
||
| 254 | */ |
||
| 255 | public function getRoles(): array |
||
| 256 | { |
||
| 257 | $roles = []; |
||
| 258 | //$roles = $this->roles; |
||
| 259 | // guarantee every user at least has ROLE_USER |
||
| 260 | $roles[] = 'ROLE_USER'; |
||
| 261 | |||
| 262 | return array_unique($roles); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function setRoles(array $roles): self |
||
|
|
|||
| 266 | { |
||
| 267 | //$this->roles = $roles; |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @see UserInterface |
||
| 274 | * Gets the password hash for this entity. |
||
| 275 | */ |
||
| 276 | public function getPassword(): string |
||
| 277 | { |
||
| 278 | return (string) $this->password; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Sets the password hash for this user. |
||
| 283 | * |
||
| 284 | * @param string $password |
||
| 285 | * @return User |
||
| 286 | */ |
||
| 287 | public function setPassword(string $password): self |
||
| 288 | { |
||
| 289 | $this->password = $password; |
||
| 290 | |||
| 291 | return $this; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @see UserInterface |
||
| 296 | */ |
||
| 297 | public function getSalt() |
||
| 298 | { |
||
| 299 | // not needed when using the "bcrypt" algorithm in security.yaml |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @see UserInterface |
||
| 304 | */ |
||
| 305 | public function eraseCredentials() |
||
| 306 | { |
||
| 307 | // If you store any temporary, sensitive data on the user, clear it here |
||
| 308 | // $this->plainPassword = null; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Gets the currency the user prefers when showing him prices. |
||
| 313 | * |
||
| 314 | * @return Currency|null The currency the user prefers, or null if the global currency should be used. |
||
| 315 | */ |
||
| 316 | public function getCurrency(): ?Currency |
||
| 317 | { |
||
| 318 | return $this->currency; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Sets the currency the users prefers to see prices in. |
||
| 323 | * |
||
| 324 | * @param Currency|null $currency |
||
| 325 | * @return User |
||
| 326 | */ |
||
| 327 | public function setCurrency(?Currency $currency): self |
||
| 328 | { |
||
| 329 | $this->currency = $currency; |
||
| 330 | |||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Checks if this user is disabled (user cannot login any more). |
||
| 336 | * |
||
| 337 | * @return bool True, if the user is disabled. |
||
| 338 | */ |
||
| 339 | public function isDisabled(): bool |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Sets the status if a user is disabled. |
||
| 346 | * |
||
| 347 | * @param bool $disabled True if the user should be disabled. |
||
| 348 | * |
||
| 349 | * @return User |
||
| 350 | */ |
||
| 351 | public function setDisabled(bool $disabled): self |
||
| 352 | { |
||
| 353 | $this->disabled = $disabled; |
||
| 354 | |||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns the ID as an string, defined by the element class. |
||
| 360 | * This should have a form like P000014, for a part with ID 14. |
||
| 361 | * |
||
| 362 | * @return string The ID as a string; |
||
| 363 | */ |
||
| 364 | public function getIDString(): string |
||
| 365 | { |
||
| 366 | return 'U'.sprintf('%06d', $this->getID()); |
||
| 367 | } |
||
| 368 | |||
| 369 | public function getPermissions(): PermissionsEmbed |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Check if the user needs a password change. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function isNeedPwChange(): bool |
||
| 380 | { |
||
| 381 | return $this->need_pw_change; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set the status, if the user needs a password change. |
||
| 386 | * |
||
| 387 | * @param bool $need_pw_change |
||
| 388 | * @return User |
||
| 389 | */ |
||
| 390 | public function setNeedPwChange(bool $need_pw_change): self |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the encrypted password reset token |
||
| 399 | * @return string|null |
||
| 400 | */ |
||
| 401 | public function getPwResetToken(): ?string |
||
| 402 | { |
||
| 403 | return $this->pw_reset_token; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Sets the encrypted password reset token |
||
| 408 | * @param string|null $pw_reset_token |
||
| 409 | * @return User |
||
| 410 | */ |
||
| 411 | public function setPwResetToken(?string $pw_reset_token): User |
||
| 412 | { |
||
| 413 | $this->pw_reset_token = $pw_reset_token; |
||
| 414 | return $this; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Gets the datetime when the password reset token expires |
||
| 419 | * @return \DateTime |
||
| 420 | */ |
||
| 421 | public function getPwResetExpires(): \DateTime |
||
| 422 | { |
||
| 423 | return $this->pw_reset_expires; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Sets the datetime when the password reset token expires |
||
| 428 | * @param \DateTime $pw_reset_expires |
||
| 429 | * @return User |
||
| 430 | */ |
||
| 431 | public function setPwResetExpires(\DateTime $pw_reset_expires): User |
||
| 432 | { |
||
| 433 | $this->pw_reset_expires = $pw_reset_expires; |
||
| 434 | return $this; |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /************************************************ |
||
| 440 | * Getters |
||
| 441 | ************************************************/ |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)]. |
||
| 445 | * Example: Max Muster (m.muster). |
||
| 446 | * |
||
| 447 | * @param bool $including_username include the username in the full name |
||
| 448 | * |
||
| 449 | * @return string a string with the full name of this user |
||
| 450 | */ |
||
| 451 | public function getFullName(bool $including_username = false): string |
||
| 452 | { |
||
| 453 | if ($including_username) { |
||
| 454 | return sprintf('%s %s (%s)', $this->getFirstName(), $this->getLastName(), $this->getName()); |
||
| 455 | } |
||
| 456 | |||
| 457 | return sprintf('%s %s', $this->getFirstName(), $this->getLastName()); |
||
| 458 | } |
||
| 459 | |||
| 460 | public function setName(string $new_name): NamedDBElement |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getFirstName(): ?string |
||
| 474 | { |
||
| 475 | return $this->first_name; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $first_name |
||
| 480 | * |
||
| 481 | * @return User |
||
| 482 | */ |
||
| 483 | public function setFirstName(?string $first_name): self |
||
| 484 | { |
||
| 485 | $this->first_name = $first_name; |
||
| 486 | |||
| 487 | return $this; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getLastName(): ?string |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $last_name |
||
| 500 | * |
||
| 501 | * @return User |
||
| 502 | */ |
||
| 503 | public function setLastName(?string $last_name): self |
||
| 504 | { |
||
| 505 | $this->last_name = $last_name; |
||
| 506 | |||
| 507 | return $this; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function getDepartment(): ?string |
||
| 514 | { |
||
| 515 | return $this->department; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $department |
||
| 520 | * |
||
| 521 | * @return User |
||
| 522 | */ |
||
| 523 | public function setDepartment(?string $department): self |
||
| 524 | { |
||
| 525 | $this->department = $department; |
||
| 526 | |||
| 527 | return $this; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function getEmail(): ?string |
||
| 534 | { |
||
| 535 | return $this->email; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $email |
||
| 540 | * |
||
| 541 | * @return User |
||
| 542 | */ |
||
| 543 | public function setEmail(?string $email): self |
||
| 544 | { |
||
| 545 | $this->email = $email; |
||
| 546 | |||
| 547 | return $this; |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function getLanguage(): ?string |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @param string $language |
||
| 560 | * |
||
| 561 | * @return User |
||
| 562 | */ |
||
| 563 | public function setLanguage(?string $language): self |
||
| 564 | { |
||
| 565 | $this->language = $language; |
||
| 566 | |||
| 567 | return $this; |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function getTimezone(): ?string |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param string $timezone |
||
| 580 | * |
||
| 581 | * @return User |
||
| 582 | */ |
||
| 583 | public function setTimezone(?string $timezone): self |
||
| 584 | { |
||
| 585 | $this->timezone = $timezone; |
||
| 586 | |||
| 587 | return $this; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getTheme(): ?string |
||
| 594 | { |
||
| 595 | return $this->theme; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param string $theme |
||
| 600 | * |
||
| 601 | * @return User |
||
| 602 | */ |
||
| 603 | public function setTheme(?string $theme): self |
||
| 604 | { |
||
| 605 | $this->theme = $theme; |
||
| 606 | |||
| 607 | return $this; |
||
| 608 | } |
||
| 609 | |||
| 610 | public function getGroup(): ?Group |
||
| 611 | { |
||
| 612 | return $this->group; |
||
| 613 | } |
||
| 614 | |||
| 615 | public function setGroup(?Group $group): self |
||
| 620 | } |
||
| 621 | |||
| 622 | public function __toString() |
||
| 623 | { |
||
| 624 | $tmp = $this->isDisabled() ? ' [DISABLED]' : ''; |
||
| 625 | |||
| 626 | return $this->getFullName(true).$tmp; |
||
| 627 | } |
||
| 628 | } |
||
| 629 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.