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