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 |
||
| 27 | class User |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * User unique id. |
||
| 31 | * |
||
| 32 | * @var ObjectId |
||
| 33 | */ |
||
| 34 | protected $_id; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Username. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $username; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Groups. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $groups = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Last sync timestamp. |
||
| 52 | * |
||
| 53 | * @var UTCDateTime |
||
| 54 | */ |
||
| 55 | protected $last_attr_sync; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Soft Quota. |
||
| 59 | * |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | protected $soft_quota = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Hard Quota. |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $hard_quota = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Is user deleted? |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $deleted = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Admin. |
||
| 80 | * |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | protected $admin = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Created. |
||
| 87 | * |
||
| 88 | * @var UTCDateTime |
||
| 89 | */ |
||
| 90 | protected $created; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * avatar. |
||
| 94 | * |
||
| 95 | * @var Binary |
||
| 96 | */ |
||
| 97 | protected $avatar; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Namespace. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $namespace; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Mail. |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $mail; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Db. |
||
| 115 | * |
||
| 116 | * @var Database |
||
| 117 | */ |
||
| 118 | protected $db; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * LoggerInterface. |
||
| 122 | * |
||
| 123 | * @var LoggerInterface |
||
| 124 | */ |
||
| 125 | protected $logger; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Server. |
||
| 129 | * |
||
| 130 | * @var Server |
||
| 131 | */ |
||
| 132 | protected $server; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Filesystem. |
||
| 136 | * |
||
| 137 | * @var Filesystem |
||
| 138 | */ |
||
| 139 | protected $fs; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Valid attributes. |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | */ |
||
| 146 | protected static $valid_attributes = [ |
||
| 147 | 'id', |
||
| 148 | 'username', |
||
| 149 | 'created', |
||
| 150 | 'soft_quota', |
||
| 151 | 'hard_quota', |
||
| 152 | 'mail', |
||
| 153 | 'namespace', |
||
| 154 | 'last_attr_sync', |
||
| 155 | 'avatar', |
||
| 156 | 'created', |
||
| 157 | 'admin', |
||
| 158 | ]; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Instance user. |
||
| 162 | * |
||
| 163 | * @param array $attributes |
||
| 164 | * @param Server $server |
||
| 165 | * @param Database $db |
||
| 166 | * @param LoggerInterface $logger |
||
| 167 | */ |
||
| 168 | public function __construct(array $attributes, Server $server, Database $db, LoggerInterface $logger) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Return username as string. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function __toString(): string |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Update user with identity attributes. |
||
| 191 | * |
||
| 192 | * @param Identity $identity |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function updateIdentity(Identity $identity): bool |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set user attribute. |
||
| 232 | * |
||
| 233 | * @param array $attribute |
||
| 234 | */ |
||
| 235 | public function setAttribute($attribute = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get user attribute. |
||
| 268 | * |
||
| 269 | * @param array|string $attribute |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function getAttribute($attribute = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Find all shares with membership. |
||
| 333 | * |
||
| 334 | * @param bool $string |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getShares(bool $string = false): array |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get node attribute usage. |
||
| 370 | * |
||
| 371 | * @param array|string $attributes |
||
| 372 | * @param int $limit |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function getNodeAttributeSummary($attributes = [], int $limit = 25): array |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get filesystem. |
||
| 407 | * |
||
| 408 | * @return Filesystem |
||
| 409 | */ |
||
| 410 | public function getFilesystem(): Filesystem |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Is Admin user? |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function isAdmin(): bool |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Check if user has share. |
||
| 431 | * |
||
| 432 | * @param Collection $node |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function hasShare(Collection $node): bool |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Find new shares and create reference. |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function findNewShares(): bool |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get unique id. |
||
| 560 | * |
||
| 561 | * @return ObjectId |
||
| 562 | */ |
||
| 563 | public function getId(): ObjectId |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get namespace. |
||
| 570 | * |
||
| 571 | * @return string |
||
| 572 | */ |
||
| 573 | public function getNamespace(): ?string |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Get hard quota. |
||
| 580 | * |
||
| 581 | * @return int |
||
| 582 | */ |
||
| 583 | public function getHardQuota(): int |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Set hard quota. |
||
| 590 | * |
||
| 591 | * @param int $quota In Bytes |
||
| 592 | * |
||
| 593 | * @return User |
||
| 594 | */ |
||
| 595 | public function setHardQuota(int $quota): self |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Set soft quota. |
||
| 605 | * |
||
| 606 | * @param int $quota In Bytes |
||
| 607 | * |
||
| 608 | * @return User |
||
| 609 | */ |
||
| 610 | public function setSoftQuota(int $quota): self |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Save. |
||
| 620 | * |
||
| 621 | * @param array $attributes |
||
| 622 | * |
||
| 623 | * @throws Exception\InvalidArgument if a given argument is not valid |
||
| 624 | * |
||
| 625 | * @return bool |
||
| 626 | */ |
||
| 627 | public function save(array $attributes = []): bool |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get used qota. |
||
| 648 | * |
||
| 649 | * @return array |
||
| 650 | */ |
||
| 651 | public function getQuotaUsage(): array |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Check quota. |
||
| 679 | * |
||
| 680 | * @param int $add Size in bytes |
||
| 681 | * |
||
| 682 | * @return bool |
||
| 683 | */ |
||
| 684 | public function checkQuota(int $add): bool |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Delete user. |
||
| 697 | * |
||
| 698 | * @param bool $force |
||
| 699 | * |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | public function delete(bool $force = false): bool |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Undelete user. |
||
| 722 | * |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | public function undelete(): bool |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Check if user is deleted. |
||
| 734 | * |
||
| 735 | * @return bool |
||
| 736 | */ |
||
| 737 | public function isDeleted(): bool |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Get Username. |
||
| 744 | * |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | public function getUsername(): string |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Get groups. |
||
| 754 | * |
||
| 755 | * @return array |
||
| 756 | */ |
||
| 757 | public function getGroups(): array |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Get attribute usage summary. |
||
| 764 | * |
||
| 765 | * @param string $attribute |
||
| 766 | * @param string $type |
||
| 767 | * @param int $limit |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | protected function _getAttributeSummary(string $attribute, string $type = 'string', int $limit = 25): array |
||
| 813 | } |
||
| 814 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: