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 |
||
| 83 | class Organization extends User |
||
| 84 | { |
||
| 85 | use SelfBlameableTrait; |
||
| 86 | |||
| 87 | const TYPE_ORGANIZATION = 1; |
||
| 88 | const TYPE_DEPARTMENT = 2; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var boolean Organization does not need password and corresponding features. |
||
| 92 | */ |
||
| 93 | public $passwordHashAttribute = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var boolean Organization does not need password and corresponding features. |
||
| 97 | */ |
||
| 98 | public $passwordResetTokenAttribute = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean Organization does not need password and corresponding features. |
||
| 102 | */ |
||
| 103 | public $passwordHistoryClass = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var boolean Organization does not need source. |
||
| 107 | */ |
||
| 108 | public $sourceAttribute = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var boolean Organization does not need auth key. |
||
| 112 | */ |
||
| 113 | public $authKeyAttribute = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var boolean Organization does not need access token. |
||
| 117 | */ |
||
| 118 | public $accessTokenAttribute = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var boolean Organization does not need login log. |
||
| 122 | */ |
||
| 123 | public $loginLogClass = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string The Organization Profile Class |
||
| 127 | */ |
||
| 128 | public $profileClass = Profile::class; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string The Member Class. |
||
| 132 | */ |
||
| 133 | public $memberClass = Member::class; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var string The Subordinate Limit Class |
||
| 137 | */ |
||
| 138 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string The Member Limit Class |
||
| 142 | */ |
||
| 143 | public $memberLimitClass = MemberLimit::class; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var string The Organization Search Class |
||
| 147 | */ |
||
| 148 | public $searchClass = OrganizationSearch::class; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var string The Organization Setting Class |
||
| 152 | */ |
||
| 153 | public $organizationSettingClass = OrganizationSetting::class; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var Member |
||
| 157 | */ |
||
| 158 | private $noInitMember; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var SubordinateLimit |
||
| 162 | */ |
||
| 163 | private $noInitSubordinateLimit; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @var MemberLimit |
||
| 167 | */ |
||
| 168 | private $noInitMemberLimit; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @var OrganizationSetting |
||
| 172 | */ |
||
| 173 | private $noInitOrganizationSetting; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @var User the creator of current Organization or Department. |
||
| 177 | * This property is only available after registration. |
||
| 178 | * Please do not access it at other times. |
||
| 179 | * If you want to get creator model except registration, please |
||
| 180 | * access [[$creator]] magic-property instead. |
||
| 181 | */ |
||
| 182 | public $creatorModel; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @var array The configuration array of Organization Profile. |
||
| 186 | * This property is only available after registration. |
||
| 187 | * Please do not access it at other times. |
||
| 188 | * If you want to get profile model except registration, please |
||
| 189 | * access [[$profile]] magic-property instead. |
||
| 190 | */ |
||
| 191 | public $profileConfig; |
||
| 192 | |||
| 193 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 194 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 195 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 196 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 197 | |||
| 198 | public $cacheTagPrefix = 'tag_organization_'; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return Member |
||
| 202 | */ |
||
| 203 | 51 | public function getNoInitMember() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return SubordinateLimit |
||
| 214 | */ |
||
| 215 | 2 | public function getNoInitSubordinateLimit() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return MemberLimit |
||
| 226 | */ |
||
| 227 | 1 | public function getNoInitMemberLimit() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @return null|OrganizationSetting |
||
| 238 | */ |
||
| 239 | 31 | public function getNoInitOrganizationSetting() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @return null|OrganizationSearch |
||
| 253 | */ |
||
| 254 | public function getSearchModel() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritdoc |
||
| 265 | */ |
||
| 266 | 52 | public function init() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | */ |
||
| 287 | 1 | public function attributeLabels() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @inheritdoc |
||
| 308 | */ |
||
| 309 | 52 | public static function tableName() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Find. |
||
| 316 | * Friendly to IDE. |
||
| 317 | * @return OrganizationQuery |
||
| 318 | */ |
||
| 319 | 52 | public static function find() |
|
| 323 | |||
| 324 | 51 | protected function getTypeRules() |
|
| 332 | |||
| 333 | 51 | public function rules() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Get Member Query. |
||
| 340 | * @return MemberQuery |
||
| 341 | */ |
||
| 342 | 50 | public function getMembers() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Get organization member users' query. |
||
| 351 | * @return BaseUserQuery |
||
| 352 | */ |
||
| 353 | 6 | public function getMemberUsers() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get subordinate limit query. |
||
| 365 | * @return null|BaseBlameableQuery |
||
| 366 | */ |
||
| 367 | 2 | public function getSubordinateLimit() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get member limit query. |
||
| 379 | * @return null|BaseBlameableQuery |
||
| 380 | */ |
||
| 381 | 1 | public function getMemberLimit() |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param string|null $item If you want to get all settings, please set it null. |
||
| 393 | * @return null |
||
| 394 | */ |
||
| 395 | 31 | public function getSettings($item = null) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param $item |
||
| 409 | * @param $value |
||
| 410 | * @return bool|null Null if organization setting not enabled. |
||
| 411 | */ |
||
| 412 | 31 | public function setSetting($item, $value) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Get member with specified user. |
||
| 430 | * @param User|string|integer $user |
||
| 431 | * @return Member Null if `user` is not in this organization. |
||
| 432 | */ |
||
| 433 | 50 | public function getMember($user) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Add member to organization. |
||
| 440 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 441 | * If member is created, it will be re-assigned to this parameter. |
||
| 442 | * @see createMemberModel |
||
| 443 | * @see createMemberModelWithUser |
||
| 444 | * @return boolean |
||
| 445 | * @throws DisallowMemberJoinOtherException |
||
| 446 | * @throws ExcludeOtherMembersException |
||
| 447 | * @throws OnlyAcceptCurrentOrgMemberException |
||
| 448 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
| 449 | */ |
||
| 450 | 50 | public function addMember(&$member) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Create member model, and set organization with this. |
||
| 507 | * @param Member $member If this parameter is not new record, it's organization |
||
| 508 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 509 | * model and create new `Member` model. |
||
| 510 | * @see createMemberModelWithUser |
||
| 511 | * @return Member |
||
| 512 | */ |
||
| 513 | public function createMemberModel($member) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Create member model with user, and set organization with this. |
||
| 524 | * @param User|string|integer $user |
||
| 525 | * @return Member |
||
| 526 | */ |
||
| 527 | 50 | public function createMemberModelWithUser($user) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Remove member. |
||
| 541 | * Note: the creator cannot be removed. |
||
| 542 | * @param Member|User $member |
||
| 543 | * @return boolean |
||
| 544 | */ |
||
| 545 | 4 | public function removeMember(&$member) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Remove administrator. |
||
| 565 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 566 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 567 | * @return boolean |
||
| 568 | * @throws IntegrityException |
||
| 569 | */ |
||
| 570 | public function removeAdministrator(&$member, $keep = true) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * |
||
| 590 | * @param Event $event |
||
| 591 | * @throws IntegrityException |
||
| 592 | * @return boolean |
||
| 593 | */ |
||
| 594 | 51 | public function onAddProfile($event) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * |
||
| 605 | * @param Event $event |
||
| 606 | */ |
||
| 607 | 51 | public function onAssignCreator($event) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * |
||
| 614 | * @param Event $event |
||
| 615 | * @return boolean |
||
| 616 | */ |
||
| 617 | 20 | public function onRevokeCreator($event) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * |
||
| 629 | * @param Event $event |
||
| 630 | * @return boolean |
||
| 631 | */ |
||
| 632 | 20 | public function onRevokeAdministrators($event) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * |
||
| 647 | * @param Event $event |
||
| 648 | */ |
||
| 649 | 20 | public function onRevokePermissions($event) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Check whether current instance is an organization. |
||
| 656 | * @return boolean |
||
| 657 | */ |
||
| 658 | 50 | public function isOrganization() |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Check whether current instance if a department. |
||
| 665 | * @return boolean |
||
| 666 | */ |
||
| 667 | 50 | public function isDepartment() |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Check whether the current organization has a member. |
||
| 674 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 675 | * @return boolean |
||
| 676 | */ |
||
| 677 | 50 | public function hasMember($user) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Get member query which role is specified `Creator`. |
||
| 684 | * @return MemberQuery |
||
| 685 | */ |
||
| 686 | 24 | public function getMemberCreators() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Get member query which role is specified `Administrator`. |
||
| 693 | * @return MemberQuery |
||
| 694 | */ |
||
| 695 | 22 | public function getMemberAdministrators() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Get user query which role is specified `Creator`. |
||
| 702 | * @return BaseUserQuery |
||
| 703 | */ |
||
| 704 | 4 | public function getCreator() |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Get user query which role is specified `Administrator`. |
||
| 716 | * @return BaseUserQuery |
||
| 717 | */ |
||
| 718 | 2 | public function getAdministrators() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * |
||
| 730 | * @param User $user |
||
| 731 | * @return boolean |
||
| 732 | * @throws \Exception |
||
| 733 | * @throws IntegrityException |
||
| 734 | */ |
||
| 735 | 51 | protected function addCreator($user) |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Add administrator. |
||
| 762 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 763 | * @return boolean |
||
| 764 | * @throws \Exception |
||
| 765 | * @throws IntegrityException |
||
| 766 | */ |
||
| 767 | 17 | public function addAdministrator($user) |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Check whether the current organization has administrator. |
||
| 787 | * @param User|integer|string $user |
||
| 788 | * @return boolean |
||
| 789 | */ |
||
| 790 | 2 | public function hasAdministrator($user) |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 801 | * @return boolean |
||
| 802 | */ |
||
| 803 | 19 | public function hasReachedSubordinateLimit() |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Get the remaining places of subordinates. |
||
| 814 | * @return bool|int False if no limit |
||
| 815 | */ |
||
| 816 | 19 | public function getRemainingSubordinatePlaces() |
|
| 829 | |||
| 830 | /** |
||
| 831 | * Check whether this organization has reached the upper limit of members. |
||
| 832 | * @return boolean |
||
| 833 | */ |
||
| 834 | 50 | public function hasReachedMemberLimit() |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Get the remaining places of members. |
||
| 845 | * @return bool|int False if no limit. |
||
| 846 | */ |
||
| 847 | 50 | public function getRemainingMemberPlaces() |
|
| 860 | |||
| 861 | const SETTING_ITEM_EXCLUDE_OTHER_MEMBERS = 'exclude_other_members'; |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @return bool |
||
| 865 | */ |
||
| 866 | 31 | public function getIsExcludeOtherMembers() |
|
| 875 | |||
| 876 | /** |
||
| 877 | * @param bool $value |
||
| 878 | * @return bool |
||
| 879 | */ |
||
| 880 | 2 | public function setIsExcludeOtherMembers($value = true) |
|
| 884 | |||
| 885 | const SETTING_ITEM_DISALLOW_MEMBER_JOIN_OTHER = 'disallow_member_join_other'; |
||
| 886 | |||
| 887 | /** |
||
| 888 | * @return bool |
||
| 889 | */ |
||
| 890 | 31 | public function getIsDisallowMemberJoinOther() |
|
| 899 | |||
| 900 | /** |
||
| 901 | * @param bool $value |
||
| 902 | * @return bool |
||
| 903 | */ |
||
| 904 | 2 | public function setIsDisallowMemberJoinOther($value = true) |
|
| 908 | |||
| 909 | const SETTING_ITEM_ONLY_ACCEPT_CURRENT_ORG_MEMBER = 'only_accept_current_org_member'; |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @return bool |
||
| 913 | */ |
||
| 914 | 18 | public function getIsOnlyAcceptCurrentOrgMember() |
|
| 923 | |||
| 924 | /** |
||
| 925 | * @param bool $value |
||
| 926 | * @return bool |
||
| 927 | */ |
||
| 928 | 2 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
| 932 | |||
| 933 | const SETTING_ITEM_ONLY_ACCEPT_SUPERIOR_ORG_MEMBER = 'only_accept_superior_org_member'; |
||
| 934 | |||
| 935 | /** |
||
| 936 | * @return bool |
||
| 937 | */ |
||
| 938 | 18 | public function getIsOnlyAcceptSuperiorOrgMember() |
|
| 947 | |||
| 948 | /** |
||
| 949 | * @param bool $value |
||
| 950 | * @return bool |
||
| 951 | */ |
||
| 952 | 2 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
| 956 | |||
| 957 | /** |
||
| 958 | * @return $this|null|static |
||
| 959 | */ |
||
| 960 | 31 | public function getTopOrganization() |
|
| 968 | |||
| 969 | /** |
||
| 970 | * Check whether the subordinates have the [[$user]] |
||
| 971 | * Note, this operation may consume the quantity of database selection. |
||
| 972 | * @param User $user |
||
| 973 | * @return bool |
||
| 974 | */ |
||
| 975 | 2 | public function hasMemberInSubordinates($user) |
|
| 990 | } |
||
| 991 |