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 |
||
| 86 | class Organization extends User |
||
| 87 | { |
||
| 88 | use SelfBlameableTrait; |
||
| 89 | |||
| 90 | const TYPE_ORGANIZATION = 1; |
||
| 91 | const TYPE_DEPARTMENT = 2; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var boolean Organization does not need password and corresponding features. |
||
| 95 | */ |
||
| 96 | public $passwordHashAttribute = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean Organization does not need password and corresponding features. |
||
| 100 | */ |
||
| 101 | public $passwordResetTokenAttribute = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var boolean Organization does not need password and corresponding features. |
||
| 105 | */ |
||
| 106 | public $passwordHistoryClass = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var boolean Organization does not need source. |
||
| 110 | */ |
||
| 111 | public $sourceAttribute = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var boolean Organization does not need auth key. |
||
| 115 | */ |
||
| 116 | public $authKeyAttribute = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var boolean Organization does not need access token. |
||
| 120 | */ |
||
| 121 | public $accessTokenAttribute = false; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var boolean Organization does not need login log. |
||
| 125 | */ |
||
| 126 | public $loginLogClass = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string The Organization Profile Class |
||
| 130 | */ |
||
| 131 | public $profileClass = Profile::class; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string The Member Class. |
||
| 135 | */ |
||
| 136 | public $memberClass = Member::class; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string The Subordinate Limit Class |
||
| 140 | */ |
||
| 141 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string The Member Limit Class |
||
| 145 | */ |
||
| 146 | public $memberLimitClass = MemberLimit::class; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var string The Organization Search Class |
||
| 150 | */ |
||
| 151 | public $searchClass = OrganizationSearch::class; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var string The Organization Setting Class |
||
| 155 | */ |
||
| 156 | public $organizationSettingClass = OrganizationSetting::class; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var Member |
||
| 160 | */ |
||
| 161 | private $noInitMember; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var SubordinateLimit |
||
| 165 | */ |
||
| 166 | private $noInitSubordinateLimit; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var MemberLimit |
||
| 170 | */ |
||
| 171 | private $noInitMemberLimit; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var OrganizationSetting |
||
| 175 | */ |
||
| 176 | private $noInitOrganizationSetting; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var User the creator of current Organization or Department. |
||
| 180 | * This property is only available after registration. |
||
| 181 | * Please do not access it at other times. |
||
| 182 | * If you want to get creator model except registration, please |
||
| 183 | * access [[$creator]] magic-property instead. |
||
| 184 | */ |
||
| 185 | public $creatorModel; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var array The configuration array of Organization Profile. |
||
| 189 | * This property is only available after registration. |
||
| 190 | * Please do not access it at other times. |
||
| 191 | * If you want to get profile model except registration, please |
||
| 192 | * access [[$profile]] magic-property instead. |
||
| 193 | */ |
||
| 194 | public $profileConfig; |
||
| 195 | |||
| 196 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 197 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 198 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 199 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 200 | |||
| 201 | public $cacheTagPrefix = 'tag_organization_'; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return Member |
||
| 205 | */ |
||
| 206 | 51 | public function getNoInitMember() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return SubordinateLimit |
||
| 217 | */ |
||
| 218 | 2 | public function getNoInitSubordinateLimit() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return MemberLimit |
||
| 229 | */ |
||
| 230 | 1 | public function getNoInitMemberLimit() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @return null|OrganizationSetting |
||
| 241 | */ |
||
| 242 | 31 | public function getNoInitOrganizationSetting() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return null|OrganizationSearch |
||
| 256 | */ |
||
| 257 | public function getSearchModel() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @inheritdoc |
||
| 268 | */ |
||
| 269 | 52 | public function init() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @inheritdoc |
||
| 289 | */ |
||
| 290 | 1 | public function attributeLabels() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @inheritdoc |
||
| 311 | */ |
||
| 312 | 52 | public static function tableName() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Find. |
||
| 319 | * Friendly to IDE. |
||
| 320 | * @return OrganizationQuery |
||
| 321 | */ |
||
| 322 | 52 | public static function find() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get rules associated with type attribute. |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | 51 | protected function getTypeRules() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @inheritdoc |
||
| 342 | */ |
||
| 343 | 51 | public function rules() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get Member Query. |
||
| 350 | * @return MemberQuery |
||
| 351 | */ |
||
| 352 | 50 | public function getMembers() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Get organization member users' query. |
||
| 361 | * @return BaseUserQuery |
||
| 362 | */ |
||
| 363 | 6 | public function getMemberUsers() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get subordinate limit query. |
||
| 375 | * @return null|BaseBlameableQuery |
||
| 376 | */ |
||
| 377 | 2 | public function getSubordinateLimit() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get member limit query. |
||
| 389 | * @return null|BaseBlameableQuery |
||
| 390 | */ |
||
| 391 | 1 | public function getMemberLimit() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @param string|null $item If you want to get all settings, please set it null. |
||
| 403 | * @return null |
||
| 404 | */ |
||
| 405 | 31 | public function getSettings($item = null) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Set organization setting. |
||
| 419 | * @param string $item |
||
| 420 | * @param string $value |
||
| 421 | * @param bool $unique |
||
| 422 | * @return bool|null Null if organization setting not enabled. |
||
| 423 | * @throws IntegrityException throw if "item-value" unique broke. |
||
| 424 | */ |
||
| 425 | 31 | public function setSetting($item, $value, $unique = false) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Get member with specified user. |
||
| 452 | * @param User|string|integer $user |
||
| 453 | * @return Member Null if `user` is not in this organization. |
||
| 454 | */ |
||
| 455 | 50 | public function getMember($user) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Add member to organization. |
||
| 462 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 463 | * If member is created, it will be re-assigned to this parameter. |
||
| 464 | * @see createMemberModel |
||
| 465 | * @see createMemberModelWithUser |
||
| 466 | * @return boolean |
||
| 467 | * @throws DisallowMemberJoinOtherException |
||
| 468 | * @throws ExcludeOtherMembersException |
||
| 469 | * @throws OnlyAcceptCurrentOrgMemberException |
||
| 470 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
| 471 | */ |
||
| 472 | 50 | public function addMember(&$member) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Create member model, and set organization with this. |
||
| 529 | * @param Member $member If this parameter is not new record, it's organization |
||
| 530 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 531 | * model and create new `Member` model. |
||
| 532 | * @see createMemberModelWithUser |
||
| 533 | * @return Member |
||
| 534 | */ |
||
| 535 | public function createMemberModel($member) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Create member model with user, and set organization with this. |
||
| 546 | * @param User|string|integer $user |
||
| 547 | * @return Member |
||
| 548 | */ |
||
| 549 | 50 | public function createMemberModelWithUser($user) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Remove member. |
||
| 563 | * Note: the creator cannot be removed. |
||
| 564 | * @param Member|User $member |
||
| 565 | * @return boolean |
||
| 566 | */ |
||
| 567 | 4 | public function removeMember(&$member) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Remove administrator. |
||
| 587 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 588 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 589 | * @return boolean |
||
| 590 | * @throws IntegrityException |
||
| 591 | */ |
||
| 592 | public function removeAdministrator(&$member, $keep = true) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * |
||
| 612 | * @param Event $event |
||
| 613 | * @throws IntegrityException |
||
| 614 | * @return boolean |
||
| 615 | */ |
||
| 616 | 51 | public function onAddProfile($event) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * |
||
| 627 | * @param Event $event |
||
| 628 | */ |
||
| 629 | 51 | public function onAssignCreator($event) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * |
||
| 636 | * @param Event $event |
||
| 637 | * @return boolean |
||
| 638 | */ |
||
| 639 | 20 | public function onRevokeCreator($event) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * |
||
| 651 | * @param Event $event |
||
| 652 | * @return boolean |
||
| 653 | */ |
||
| 654 | 20 | public function onRevokeAdministrators($event) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * |
||
| 669 | * @param Event $event |
||
| 670 | */ |
||
| 671 | 20 | public function onRevokePermissions($event) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Check whether current instance is an organization. |
||
| 678 | * @return boolean |
||
| 679 | */ |
||
| 680 | 50 | public function isOrganization() |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Check whether current instance if a department. |
||
| 687 | * @return boolean |
||
| 688 | */ |
||
| 689 | 50 | public function isDepartment() |
|
| 693 | |||
| 694 | /** |
||
| 695 | * Check whether the current organization has a member. |
||
| 696 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 697 | * @return boolean |
||
| 698 | */ |
||
| 699 | 50 | public function hasMember($user) |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Get member query which role is specified `Creator`. |
||
| 706 | * @return MemberQuery |
||
| 707 | */ |
||
| 708 | 24 | public function getMemberCreators() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Get member query which role is specified `Administrator`. |
||
| 715 | * @return MemberQuery |
||
| 716 | */ |
||
| 717 | 22 | public function getMemberAdministrators() |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Get user query which role is specified `Creator`. |
||
| 724 | * @return BaseUserQuery |
||
| 725 | */ |
||
| 726 | 4 | public function getCreator() |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Get user query which role is specified `Administrator`. |
||
| 738 | * @return BaseUserQuery |
||
| 739 | */ |
||
| 740 | 2 | public function getAdministrators() |
|
| 749 | |||
| 750 | /** |
||
| 751 | * |
||
| 752 | * @param User $user |
||
| 753 | * @return boolean |
||
| 754 | * @throws \Exception |
||
| 755 | * @throws IntegrityException |
||
| 756 | */ |
||
| 757 | 51 | protected function addCreator($user) |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Add administrator. |
||
| 784 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 785 | * @return boolean |
||
| 786 | * @throws \Exception |
||
| 787 | * @throws IntegrityException |
||
| 788 | */ |
||
| 789 | 17 | public function addAdministrator($user) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Check whether the current organization has administrator. |
||
| 809 | * @param User|integer|string $user |
||
| 810 | * @return boolean |
||
| 811 | */ |
||
| 812 | 2 | public function hasAdministrator($user) |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 823 | * @return boolean |
||
| 824 | */ |
||
| 825 | 19 | public function hasReachedSubordinateLimit() |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Get the remaining places of subordinates. |
||
| 836 | * @return bool|int False if no limit |
||
| 837 | */ |
||
| 838 | 19 | public function getRemainingSubordinatePlaces() |
|
| 851 | |||
| 852 | /** |
||
| 853 | * Check whether this organization has reached the upper limit of members. |
||
| 854 | * @return boolean |
||
| 855 | */ |
||
| 856 | 50 | public function hasReachedMemberLimit() |
|
| 864 | |||
| 865 | /** |
||
| 866 | * Get the remaining places of members. |
||
| 867 | * @return bool|int False if no limit. |
||
| 868 | */ |
||
| 869 | 50 | public function getRemainingMemberPlaces() |
|
| 882 | |||
| 883 | const SETTING_ITEM_EXCLUDE_OTHER_MEMBERS = 'exclude_other_members'; |
||
| 884 | |||
| 885 | /** |
||
| 886 | * @return bool |
||
| 887 | */ |
||
| 888 | 31 | public function getIsExcludeOtherMembers() |
|
| 897 | |||
| 898 | /** |
||
| 899 | * @param bool $value |
||
| 900 | * @return bool |
||
| 901 | */ |
||
| 902 | 31 | public function setIsExcludeOtherMembers($value = true) |
|
| 906 | |||
| 907 | const SETTING_ITEM_DISALLOW_MEMBER_JOIN_OTHER = 'disallow_member_join_other'; |
||
| 908 | |||
| 909 | /** |
||
| 910 | * @return bool |
||
| 911 | */ |
||
| 912 | 31 | public function getIsDisallowMemberJoinOther() |
|
| 921 | |||
| 922 | /** |
||
| 923 | * @param bool $value |
||
| 924 | * @return bool |
||
| 925 | */ |
||
| 926 | 31 | public function setIsDisallowMemberJoinOther($value = true) |
|
| 930 | |||
| 931 | const SETTING_ITEM_ONLY_ACCEPT_CURRENT_ORG_MEMBER = 'only_accept_current_org_member'; |
||
| 932 | |||
| 933 | /** |
||
| 934 | * @return bool |
||
| 935 | */ |
||
| 936 | 18 | public function getIsOnlyAcceptCurrentOrgMember() |
|
| 945 | |||
| 946 | /** |
||
| 947 | * @param bool $value |
||
| 948 | * @return bool |
||
| 949 | */ |
||
| 950 | 18 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
| 954 | |||
| 955 | const SETTING_ITEM_ONLY_ACCEPT_SUPERIOR_ORG_MEMBER = 'only_accept_superior_org_member'; |
||
| 956 | |||
| 957 | /** |
||
| 958 | * @return bool |
||
| 959 | */ |
||
| 960 | 10 | public function getIsOnlyAcceptSuperiorOrgMember() |
|
| 972 | |||
| 973 | /** |
||
| 974 | * @param bool $value |
||
| 975 | * @return bool |
||
| 976 | */ |
||
| 977 | 10 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
| 984 | |||
| 985 | const SETTING_ITEM_JOIN_PASSWORD = 'join_password'; |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Get join password. |
||
| 989 | * @return mixed |
||
| 990 | */ |
||
| 991 | public function getJoinPassword() |
||
| 1000 | |||
| 1001 | /** |
||
| 1002 | * Set join password. |
||
| 1003 | * @param string $value |
||
| 1004 | * @return bool|null |
||
| 1005 | */ |
||
| 1006 | public function setJoinPassword($value = '') |
||
| 1010 | |||
| 1011 | const SETTING_ITEM_JOIN_IP_ADDRESS = 'join_ip_address'; |
||
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Get Join IP address |
||
| 1015 | * @return mixed |
||
| 1016 | */ |
||
| 1017 | public function getJoinIpAddress() |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Set join IP address. |
||
| 1029 | * @param $value |
||
| 1030 | * @return bool|null |
||
| 1031 | */ |
||
| 1032 | public function setJoinIpAddress($value = '') |
||
| 1036 | |||
| 1037 | const SETTING_ITEM_JOIN_ENTRANCE_URL = 'join_entrance_url'; |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Get join entrance URL. |
||
| 1041 | * This setting should be confirmed unique. |
||
| 1042 | * @return string |
||
| 1043 | */ |
||
| 1044 | public function getJoinEntranceUrl() |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Set join entrance URL. |
||
| 1056 | * @param string $value |
||
| 1057 | * @return bool|null |
||
| 1058 | */ |
||
| 1059 | public function setJoinEntranceUrl($value = '') |
||
| 1063 | |||
| 1064 | const SETTING_ITEM_EXIT_ALLOW_WITHDRAW_ACTIVELY = 'exit_allow_withdraw_actively'; |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * @return bool |
||
| 1068 | */ |
||
| 1069 | public function getExitAllowWithdrawActively() |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * @param bool $value |
||
| 1081 | * @return bool|null |
||
| 1082 | */ |
||
| 1083 | public function setExitAllowWithdrawActively($value = false) |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * @return $this|null|static |
||
| 1090 | */ |
||
| 1091 | 31 | public function getTopOrganization() |
|
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Check whether the subordinates have the [[$user]] |
||
| 1102 | * Note, this operation may consume the quantity of database selection. |
||
| 1103 | * @param User $user |
||
| 1104 | * @return bool |
||
| 1105 | */ |
||
| 1106 | 2 | public function hasMemberInSubordinates($user) |
|
| 1121 | } |
||
| 1122 |