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 Member |
||
| 154 | */ |
||
| 155 | private $noInitMember; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var SubordinateLimit |
||
| 159 | */ |
||
| 160 | private $noInitSubordinateLimit; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var MemberLimit |
||
| 164 | */ |
||
| 165 | private $noInitMemberLimit; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var User the creator of current Organization or Department. |
||
| 169 | * This property is only available after registration. |
||
| 170 | * Please do not access it at other times. |
||
| 171 | * If you want to get creator model except registration, please |
||
| 172 | * access [[$creator]] magic-property instead. |
||
| 173 | */ |
||
| 174 | public $creatorModel; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var array The configuration array of Organization Profile. |
||
| 178 | * This property is only available after registration. |
||
| 179 | * Please do not access it at other times. |
||
| 180 | * If you want to get profile model except registration, please |
||
| 181 | * access [[$profile]] magic-property instead. |
||
| 182 | */ |
||
| 183 | public $profileConfig; |
||
| 184 | |||
| 185 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 186 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 187 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 188 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return Member |
||
| 192 | */ |
||
| 193 | 51 | public function getNoInitMember() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @return SubordinateLimit |
||
| 204 | */ |
||
| 205 | 19 | public function getNoInitSubordinateLimit() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return MemberLimit |
||
| 216 | */ |
||
| 217 | 1 | public function getNoInitMemberLimit() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return null|OrganizationSearch |
||
| 228 | */ |
||
| 229 | public function getSearchModel() |
||
| 237 | |||
| 238 | 52 | public function init() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @inheritdoc |
||
| 258 | */ |
||
| 259 | 20 | public function attributeLabels() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | */ |
||
| 281 | 52 | public static function tableName() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Find. |
||
| 288 | * Friendly to IDE. |
||
| 289 | * @return OrganizationQuery |
||
| 290 | */ |
||
| 291 | 52 | public static function find() |
|
| 295 | |||
| 296 | 51 | protected function getTypeRules() |
|
| 305 | |||
| 306 | 51 | public function rules() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get Member Query. |
||
| 313 | * @return MemberQuery |
||
| 314 | */ |
||
| 315 | 50 | public function getMembers() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get organization member users' query. |
||
| 324 | * @return BaseUserQuery |
||
| 325 | */ |
||
| 326 | 6 | public function getMemberUsers() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get subordinate limit query. |
||
| 338 | * @return null|BaseBlameableQuery |
||
| 339 | */ |
||
| 340 | 2 | public function getSubordinateLimit() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get member limit query. |
||
| 352 | * @return null|BaseBlameableQuery |
||
| 353 | */ |
||
| 354 | 1 | public function getMemberLimit() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get member with specified user. |
||
| 366 | * @param User|string|integer $user |
||
| 367 | * @return Member Null if `user` is not in this organization. |
||
| 368 | */ |
||
| 369 | 50 | public function getMember($user) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Add member to organization. |
||
| 376 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 377 | * If member is created, it will be re-assigned to this parameter. |
||
| 378 | * @see createMemberModel |
||
| 379 | * @see createMemberModelWithUser |
||
| 380 | * @return boolean |
||
| 381 | * @throws DisallowMemberJoinOtherException |
||
| 382 | * @throws ExcludeOtherMembersException |
||
| 383 | * @throws OnlyAcceptCurrentOrgMemberException |
||
| 384 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
| 385 | */ |
||
| 386 | 50 | public function addMember(&$member) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Create member model, and set organization with this. |
||
| 443 | * @param Member $member If this parameter is not new record, it's organization |
||
| 444 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 445 | * model and create new `Member` model. |
||
| 446 | * @see createMemberModelWithUser |
||
| 447 | * @return Member |
||
| 448 | */ |
||
| 449 | public function createMemberModel($member) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Create member model with user, and set organization with this. |
||
| 460 | * @param User|string|integer $user |
||
| 461 | * @return Member |
||
| 462 | */ |
||
| 463 | 50 | public function createMemberModelWithUser($user) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Remove member. |
||
| 477 | * Note: the creator cannot be removed. |
||
| 478 | * @param Member|User $member |
||
| 479 | * @return boolean |
||
| 480 | */ |
||
| 481 | 4 | public function removeMember(&$member) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Remove administrator. |
||
| 501 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 502 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 503 | * @return boolean |
||
| 504 | * @throws IntegrityException |
||
| 505 | */ |
||
| 506 | public function removeAdministrator(&$member, $keep = true) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * |
||
| 526 | * @param Event $event |
||
| 527 | * @throws IntegrityException |
||
| 528 | * @return boolean |
||
| 529 | */ |
||
| 530 | 51 | public function onAddProfile($event) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * |
||
| 541 | * @param Event $event |
||
| 542 | */ |
||
| 543 | 51 | public function onAssignCreator($event) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * |
||
| 550 | * @param Event $event |
||
| 551 | * @return boolean |
||
| 552 | */ |
||
| 553 | 20 | public function onRevokeCreator($event) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * |
||
| 565 | * @param Event $event |
||
| 566 | * @return boolean |
||
| 567 | */ |
||
| 568 | 20 | public function onRevokeAdministrators($event) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * |
||
| 583 | * @param Event $event |
||
| 584 | */ |
||
| 585 | 20 | public function onRevokePermissions($event) |
|
| 589 | |||
| 590 | /** |
||
| 591 | * Check whether current instance is an organization. |
||
| 592 | * @return boolean |
||
| 593 | */ |
||
| 594 | 50 | public function isOrganization() |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Check whether current instance if a department. |
||
| 601 | * @return boolean |
||
| 602 | */ |
||
| 603 | 50 | public function isDepartment() |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Check whether the current organization has a member. |
||
| 610 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 611 | * @return boolean |
||
| 612 | */ |
||
| 613 | 50 | public function hasMember($user) |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Get member query which role is specified `Creator`. |
||
| 620 | * @return MemberQuery |
||
| 621 | */ |
||
| 622 | 24 | public function getMemberCreators() |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Get member query which role is specified `Administrator`. |
||
| 629 | * @return MemberQuery |
||
| 630 | */ |
||
| 631 | 22 | public function getMemberAdministrators() |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Get user query which role is specified `Creator`. |
||
| 638 | * @return BaseUserQuery |
||
| 639 | */ |
||
| 640 | 4 | public function getCreator() |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get user query which role is specified `Administrator`. |
||
| 652 | * @return BaseUserQuery |
||
| 653 | */ |
||
| 654 | 2 | public function getAdministrators() |
|
| 663 | |||
| 664 | /** |
||
| 665 | * |
||
| 666 | * @param User $user |
||
| 667 | * @return boolean |
||
| 668 | * @throws \Exception |
||
| 669 | * @throws IntegrityException |
||
| 670 | */ |
||
| 671 | 51 | protected function addCreator($user) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Add administrator. |
||
| 698 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 699 | * @return boolean |
||
| 700 | * @throws \Exception |
||
| 701 | * @throws IntegrityException |
||
| 702 | */ |
||
| 703 | 17 | public function addAdministrator($user) |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Check whether the current organization has administrator. |
||
| 723 | * @param User|integer|string $user |
||
| 724 | * @return boolean |
||
| 725 | */ |
||
| 726 | 2 | public function hasAdministrator($user) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 737 | * @return boolean |
||
| 738 | */ |
||
| 739 | 18 | public function hasReachedSubordinateLimit() |
|
| 747 | |||
| 748 | /** |
||
| 749 | * Get the remaining places of subordinates. |
||
| 750 | * @return bool|int False if no limit |
||
| 751 | */ |
||
| 752 | 18 | public function getRemainingSubordinatePlaces() |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Check whether this organization has reached the upper limit of members. |
||
| 768 | * @return boolean |
||
| 769 | */ |
||
| 770 | 50 | public function hasReachedMemberLimit() |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Get the remaining places of members. |
||
| 781 | * @return bool|int False if no limit. |
||
| 782 | */ |
||
| 783 | 50 | public function getRemainingMemberPlaces() |
|
| 796 | |||
| 797 | /** |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | 31 | public function getIsExcludeOtherMembers() |
|
| 804 | |||
| 805 | /** |
||
| 806 | * @param bool $value |
||
| 807 | */ |
||
| 808 | 2 | public function setIsExcludeOtherMembers($value = true) |
|
| 812 | |||
| 813 | /** |
||
| 814 | * @return bool |
||
| 815 | */ |
||
| 816 | 31 | public function getIsDisallowMemberJoinOther() |
|
| 820 | |||
| 821 | /** |
||
| 822 | * @param bool $value |
||
| 823 | */ |
||
| 824 | 2 | public function setIsDisallowMemberJoinOther($value = true) |
|
| 828 | |||
| 829 | /** |
||
| 830 | * @return bool |
||
| 831 | */ |
||
| 832 | 17 | public function getIsOnlyAcceptCurrentOrgMember() |
|
| 836 | |||
| 837 | /** |
||
| 838 | * @param bool $value |
||
| 839 | */ |
||
| 840 | 2 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
| 844 | |||
| 845 | /** |
||
| 846 | * @return bool |
||
| 847 | */ |
||
| 848 | 17 | public function getIsOnlyAcceptSuperiorOrgMember() |
|
| 852 | |||
| 853 | /** |
||
| 854 | * @param bool $value |
||
| 855 | */ |
||
| 856 | 2 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * @return $this|null|static |
||
| 863 | */ |
||
| 864 | 31 | public function getTopOrganization() |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Check whether the subordinates have the [[$user]] |
||
| 875 | * Note, this operation may consume the quantity of database selection. |
||
| 876 | * @param User $user |
||
| 877 | * @return bool |
||
| 878 | */ |
||
| 879 | 2 | public function hasMemberInSubordinates($user) |
|
| 894 | } |
||
| 895 |