Complex classes like Organization 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 Organization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 84 | class Organization extends User |
||
| 85 | { |
||
| 86 | use SelfBlameableTrait; |
||
| 87 | |||
| 88 | const TYPE_ORGANIZATION = 1; |
||
| 89 | const TYPE_DEPARTMENT = 2; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var boolean Organization does not need password and corresponding features. |
||
| 93 | */ |
||
| 94 | public $passwordHashAttribute = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var boolean Organization does not need password and corresponding features. |
||
| 98 | */ |
||
| 99 | public $passwordResetTokenAttribute = false; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var boolean Organization does not need password and corresponding features. |
||
| 103 | */ |
||
| 104 | public $passwordHistoryClass = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var boolean Organization does not need source. |
||
| 108 | */ |
||
| 109 | public $sourceAttribute = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var boolean Organization does not need auth key. |
||
| 113 | */ |
||
| 114 | public $authKeyAttribute = false; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var boolean Organization does not need access token. |
||
| 118 | */ |
||
| 119 | public $accessTokenAttribute = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var boolean Organization does not need login log. |
||
| 123 | */ |
||
| 124 | public $loginLogClass = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string The Organization Profile Class |
||
| 128 | */ |
||
| 129 | public $profileClass = Profile::class; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string The Member Class. |
||
| 133 | */ |
||
| 134 | public $memberClass = Member::class; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string The Subordinate Limit Class |
||
| 138 | */ |
||
| 139 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string The Member Limit Class |
||
| 143 | */ |
||
| 144 | public $memberLimitClass = MemberLimit::class; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string The Organization Search Class |
||
| 148 | */ |
||
| 149 | public $searchClass = OrganizationSearch::class; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var Member |
||
| 153 | */ |
||
| 154 | private $noInitMember; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var SubordinateLimit |
||
| 158 | */ |
||
| 159 | private $noInitSubordinateLimit; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var MemberLimit |
||
| 163 | */ |
||
| 164 | private $noInitMemberLimit; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var User the creator of current Organization or Department. |
||
| 168 | * This property is only available after registration. |
||
| 169 | * Please do not access it at other times. |
||
| 170 | * If you want to get creator model except registration, please |
||
| 171 | * access [[$creator]] magic-property instead. |
||
| 172 | */ |
||
| 173 | public $creatorModel; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var array The configuration array of Organization Profile. |
||
| 177 | * This property is only available after registration. |
||
| 178 | * Please do not access it at other times. |
||
| 179 | * If you want to get profile model except registration, please |
||
| 180 | * access [[$profile]] magic-property instead. |
||
| 181 | */ |
||
| 182 | public $profileConfig; |
||
| 183 | |||
| 184 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 185 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 186 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 187 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return Member |
||
| 191 | */ |
||
| 192 | 51 | public function getNoInitMember() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return SubordinateLimit |
||
| 203 | */ |
||
| 204 | 2 | public function getNoInitSubordinateLimit() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return MemberLimit |
||
| 215 | */ |
||
| 216 | 1 | public function getNoInitMemberLimit() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return null|OrganizationSearch |
||
| 227 | */ |
||
| 228 | public function getSearchModel() |
||
| 236 | |||
| 237 | 52 | public function init() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritdoc |
||
| 257 | */ |
||
| 258 | 1 | public function attributeLabels() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @inheritdoc |
||
| 279 | */ |
||
| 280 | 52 | public static function tableName() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Find. |
||
| 287 | * Friendly to IDE. |
||
| 288 | * @return OrganizationQuery |
||
| 289 | */ |
||
| 290 | 52 | public static function find() |
|
| 294 | |||
| 295 | 51 | protected function getTypeRules() |
|
| 304 | |||
| 305 | 51 | public function rules() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get Member Query. |
||
| 312 | * @return MemberQuery |
||
| 313 | */ |
||
| 314 | 50 | public function getMembers() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get organization member users' query. |
||
| 323 | * @return BaseUserQuery |
||
| 324 | */ |
||
| 325 | 6 | public function getMemberUsers() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get subordinate limit query. |
||
| 337 | * @return null|BaseBlameableQuery |
||
| 338 | */ |
||
| 339 | 2 | public function getSubordinateLimit() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Get member limit query. |
||
| 351 | * @return null|BaseBlameableQuery |
||
| 352 | */ |
||
| 353 | 1 | public function getMemberLimit() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get member with specified user. |
||
| 365 | * @param User|string|integer $user |
||
| 366 | * @return Member Null if `user` is not in this organization. |
||
| 367 | */ |
||
| 368 | 50 | public function getMember($user) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Add member to organization. |
||
| 375 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 376 | * If member is created, it will be re-assigned to this parameter. |
||
| 377 | * @see createMemberModel |
||
| 378 | * @see createMemberModelWithUser |
||
| 379 | * @return boolean |
||
| 380 | * @throws DisallowMemberJoinOtherException |
||
| 381 | * @throws ExcludeOtherMembersException |
||
| 382 | * @throws OnlyAcceptCurrentOrgMemberException |
||
| 383 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
| 384 | */ |
||
| 385 | 50 | public function addMember(&$member) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Create member model, and set organization with this. |
||
| 442 | * @param Member $member If this parameter is not new record, it's organization |
||
| 443 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 444 | * model and create new `Member` model. |
||
| 445 | * @see createMemberModelWithUser |
||
| 446 | * @return Member |
||
| 447 | */ |
||
| 448 | public function createMemberModel($member) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Create member model with user, and set organization with this. |
||
| 459 | * @param User|string|integer $user |
||
| 460 | * @return Member |
||
| 461 | */ |
||
| 462 | 50 | public function createMemberModelWithUser($user) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Remove member. |
||
| 476 | * Note: the creator cannot be removed. |
||
| 477 | * @param Member|User $member |
||
| 478 | * @return boolean |
||
| 479 | */ |
||
| 480 | 4 | public function removeMember(&$member) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Remove administrator. |
||
| 500 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 501 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 502 | * @return boolean |
||
| 503 | * @throws IntegrityException |
||
| 504 | */ |
||
| 505 | public function removeAdministrator(&$member, $keep = true) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * |
||
| 525 | * @param Event $event |
||
| 526 | * @throws IntegrityException |
||
| 527 | * @return boolean |
||
| 528 | */ |
||
| 529 | 51 | public function onAddProfile($event) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * |
||
| 540 | * @param Event $event |
||
| 541 | */ |
||
| 542 | 51 | public function onAssignCreator($event) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * |
||
| 549 | * @param Event $event |
||
| 550 | * @return boolean |
||
| 551 | */ |
||
| 552 | 20 | public function onRevokeCreator($event) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * |
||
| 564 | * @param Event $event |
||
| 565 | * @return boolean |
||
| 566 | */ |
||
| 567 | 20 | public function onRevokeAdministrators($event) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * |
||
| 582 | * @param Event $event |
||
| 583 | */ |
||
| 584 | 20 | public function onRevokePermissions($event) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Check whether current instance is an organization. |
||
| 591 | * @return boolean |
||
| 592 | */ |
||
| 593 | 50 | public function isOrganization() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Check whether current instance if a department. |
||
| 600 | * @return boolean |
||
| 601 | */ |
||
| 602 | 50 | public function isDepartment() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Check whether the current organization has a member. |
||
| 609 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 610 | * @return boolean |
||
| 611 | */ |
||
| 612 | 50 | public function hasMember($user) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Get member query which role is specified `Creator`. |
||
| 619 | * @return MemberQuery |
||
| 620 | */ |
||
| 621 | 24 | public function getMemberCreators() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Get member query which role is specified `Administrator`. |
||
| 628 | * @return MemberQuery |
||
| 629 | */ |
||
| 630 | 22 | public function getMemberAdministrators() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Get user query which role is specified `Creator`. |
||
| 637 | * @return BaseUserQuery |
||
| 638 | */ |
||
| 639 | 4 | public function getCreator() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Get user query which role is specified `Administrator`. |
||
| 651 | * @return BaseUserQuery |
||
| 652 | */ |
||
| 653 | 2 | public function getAdministrators() |
|
| 662 | |||
| 663 | /** |
||
| 664 | * |
||
| 665 | * @param User $user |
||
| 666 | * @return boolean |
||
| 667 | * @throws \Exception |
||
| 668 | * @throws IntegrityException |
||
| 669 | */ |
||
| 670 | 51 | protected function addCreator($user) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Add administrator. |
||
| 697 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 698 | * @return boolean |
||
| 699 | * @throws \Exception |
||
| 700 | * @throws IntegrityException |
||
| 701 | */ |
||
| 702 | 17 | public function addAdministrator($user) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Check whether the current organization has administrator. |
||
| 722 | * @param User|integer|string $user |
||
| 723 | * @return boolean |
||
| 724 | */ |
||
| 725 | 2 | public function hasAdministrator($user) |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 736 | * @return boolean |
||
| 737 | */ |
||
| 738 | 18 | public function hasReachedSubordinateLimit() |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Check whether this organization has reached the upper limit of members. |
||
| 754 | * @return boolean |
||
| 755 | */ |
||
| 756 | 50 | public function hasReachedMemberLimit() |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @return bool |
||
| 772 | */ |
||
| 773 | 31 | public function getIsExcludeOtherMembers() |
|
| 777 | |||
| 778 | /** |
||
| 779 | * @param bool $value |
||
| 780 | */ |
||
| 781 | 2 | public function setIsExcludeOtherMembers($value = true) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * @return bool |
||
| 788 | */ |
||
| 789 | 31 | public function getIsDisallowMemberJoinOther() |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @param bool $value |
||
| 796 | */ |
||
| 797 | 2 | public function setIsDisallowMemberJoinOther($value = true) |
|
| 801 | |||
| 802 | /** |
||
| 803 | * @return bool |
||
| 804 | */ |
||
| 805 | 17 | public function getIsOnlyAcceptCurrentOrgMember() |
|
| 809 | |||
| 810 | /** |
||
| 811 | * @param bool $value |
||
| 812 | */ |
||
| 813 | 2 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
| 817 | |||
| 818 | /** |
||
| 819 | * @return bool |
||
| 820 | */ |
||
| 821 | 17 | public function getIsOnlyAcceptSuperiorOrgMember() |
|
| 825 | |||
| 826 | /** |
||
| 827 | * @param bool $value |
||
| 828 | */ |
||
| 829 | 2 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
| 833 | |||
| 834 | /** |
||
| 835 | * @return $this|null|static |
||
| 836 | */ |
||
| 837 | 31 | public function getTopOrganization() |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Check whether the subordinates have the [[$user]] |
||
| 848 | * Note, this operation may consume the quantity of database selection. |
||
| 849 | * @param User $user |
||
| 850 | * @return bool |
||
| 851 | */ |
||
| 852 | 2 | public function hasMemberInSubordinates($user) |
|
| 867 | } |
||
| 868 |