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 |
||
| 62 | class Organization extends User |
||
| 63 | { |
||
| 64 | use SelfBlameableTrait; |
||
| 65 | |||
| 66 | const TYPE_ORGANIZATION = 1; |
||
| 67 | const TYPE_DEPARTMENT = 2; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean Organization does not need password and corresponding features. |
||
| 71 | */ |
||
| 72 | public $passwordHashAttribute = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var boolean Organization does not need password and corresponding features. |
||
| 76 | */ |
||
| 77 | public $passwordResetTokenAttribute = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var boolean Organization does not need password and corresponding features. |
||
| 81 | */ |
||
| 82 | public $passwordHistoryClass = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean Organization does not need source. |
||
| 86 | */ |
||
| 87 | public $sourceAttribute = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var boolean Organization does not need auth key. |
||
| 91 | */ |
||
| 92 | public $authKeyAttribute = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var boolean Organization does not need access token. |
||
| 96 | */ |
||
| 97 | public $accessTokenAttribute = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * |
||
| 101 | * @var boolean Organization does not need login log. |
||
| 102 | */ |
||
| 103 | public $loginLogClass = false; |
||
| 104 | |||
| 105 | public $profileClass = Profile::class; |
||
| 106 | |||
| 107 | public $memberClass = Member::class; |
||
| 108 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 109 | public $memberLimitClass = MemberLimit::class; |
||
| 110 | public $searchClass = OrganizationSearch::class; |
||
| 111 | /** |
||
| 112 | * @var Member |
||
| 113 | */ |
||
| 114 | private $noInitMember; |
||
| 115 | /** |
||
| 116 | * @var SubordinateLimit |
||
| 117 | */ |
||
| 118 | private $noInitSubordinateLimit; |
||
| 119 | /** |
||
| 120 | * @var MemberLimit |
||
| 121 | */ |
||
| 122 | private $noInitMemberLimit; |
||
| 123 | public $creatorModel; |
||
| 124 | public $profileConfig; |
||
| 125 | |||
| 126 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 127 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 128 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 129 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return Member |
||
| 133 | */ |
||
| 134 | 49 | public function getNoInitMember() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @return SubordinateLimit |
||
| 145 | */ |
||
| 146 | 2 | public function getNoInitSubordinateLimit() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @return MemberLimit |
||
| 157 | */ |
||
| 158 | 1 | public function getNoInitMemberLimit() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return null|OrganizationSearch |
||
| 169 | */ |
||
| 170 | public function getSearchModel() |
||
| 178 | |||
| 179 | 50 | public function init() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritdoc |
||
| 199 | */ |
||
| 200 | 1 | public function attributeLabels() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | */ |
||
| 222 | 50 | public static function tableName() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Find. |
||
| 229 | * Friendly to IDE. |
||
| 230 | * @return OrganizationQuery |
||
| 231 | */ |
||
| 232 | 50 | public static function find() |
|
| 236 | |||
| 237 | 49 | protected function getTypeRules() |
|
| 246 | |||
| 247 | 49 | public function rules() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get Member Query. |
||
| 254 | * @return MemberQuery |
||
| 255 | */ |
||
| 256 | 48 | public function getMembers() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Get organization member users' query. |
||
| 265 | * @return BaseUserQuery |
||
| 266 | */ |
||
| 267 | 6 | public function getMemberUsers() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Get subordinate limit query. |
||
| 279 | * @return null|BaseBlameableQuery |
||
| 280 | */ |
||
| 281 | 2 | public function getSubordinateLimit() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get member limit query. |
||
| 293 | * @return null|BaseBlameableQuery |
||
| 294 | */ |
||
| 295 | 1 | public function getMemberLimit() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Get member with specified user. |
||
| 307 | * @param User|string|integer $user |
||
| 308 | * @return Member Null if `user` is not in this organization. |
||
| 309 | */ |
||
| 310 | 48 | public function getMember($user) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Add member to organization. |
||
| 317 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 318 | * If member is created, it will be re-assigned to this parameter. |
||
| 319 | * @see createMemberModel |
||
| 320 | * @see createMemberModelWithUser |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | 48 | public function addMember(&$member) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Create member model, and set organization with this. |
||
| 380 | * @param Member $member If this parameter is not new record, it's organization |
||
| 381 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 382 | * model and create new `Member` model. |
||
| 383 | * @see createMemberModelWithUser |
||
| 384 | * @return Member |
||
| 385 | */ |
||
| 386 | public function createMemberModel($member) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Create member model with user, and set organization with this. |
||
| 397 | * @param User|string|integer $user |
||
| 398 | * @return Member |
||
| 399 | */ |
||
| 400 | 48 | public function createMemberModelWithUser($user) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Remove member. |
||
| 414 | * Note: the creator cannot be removed. |
||
| 415 | * @param Member|User $member |
||
| 416 | * @return boolean |
||
| 417 | */ |
||
| 418 | 2 | public function removeMember(&$member) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Remove administrator. |
||
| 438 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 439 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 440 | * @return boolean |
||
| 441 | * @throws IntegrityException |
||
| 442 | */ |
||
| 443 | public function removeAdministrator(&$member, $keep = true) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * |
||
| 463 | * @param Event $event |
||
| 464 | * @throws IntegrityException |
||
| 465 | * @return boolean |
||
| 466 | */ |
||
| 467 | 49 | public function onAddProfile($event) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * |
||
| 478 | * @param Event $event |
||
| 479 | */ |
||
| 480 | 49 | public function onAssignCreator($event) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * |
||
| 487 | * @param Event $event |
||
| 488 | * @return boolean |
||
| 489 | */ |
||
| 490 | 20 | public function onRevokeCreator($event) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * |
||
| 502 | * @param Event $event |
||
| 503 | * @return boolean |
||
| 504 | */ |
||
| 505 | 20 | public function onRevokeAdministrators($event) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * |
||
| 520 | * @param Event $event |
||
| 521 | */ |
||
| 522 | 20 | public function onRevokePermissions($event) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Check whether current instance is an organization. |
||
| 529 | * @return boolean |
||
| 530 | */ |
||
| 531 | 48 | public function isOrganization() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Check whether current instance if a department. |
||
| 538 | * @return boolean |
||
| 539 | */ |
||
| 540 | 48 | public function isDepartment() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Check whether the current organization has a member. |
||
| 547 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 548 | * @return boolean |
||
| 549 | */ |
||
| 550 | 48 | public function hasMember($user) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Get member query which role is specified `Creator`. |
||
| 557 | * @return MemberQuery |
||
| 558 | */ |
||
| 559 | 24 | public function getMemberCreators() |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Get member query which role is specified `Administrator`. |
||
| 566 | * @return MemberQuery |
||
| 567 | */ |
||
| 568 | 22 | public function getMemberAdministrators() |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Get user query which role is specified `Creator`. |
||
| 575 | * @return BaseUserQuery |
||
| 576 | */ |
||
| 577 | 4 | public function getCreator() |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Get user query which role is specified `Administrator`. |
||
| 589 | * @return BaseUserQuery |
||
| 590 | */ |
||
| 591 | 2 | public function getAdministrators() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * |
||
| 603 | * @param User $user |
||
| 604 | * @return boolean |
||
| 605 | * @throws \Exception |
||
| 606 | * @throws IntegrityException |
||
| 607 | */ |
||
| 608 | 49 | protected function addCreator($user) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Add administrator. |
||
| 635 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 636 | * @return boolean |
||
| 637 | * @throws \Exception |
||
| 638 | * @throws IntegrityException |
||
| 639 | */ |
||
| 640 | 15 | public function addAdministrator($user) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Check whether the current organization has administrator. |
||
| 660 | * @param User|integer|string $user |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | 2 | public function hasAdministrator($user) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 674 | * @return boolean |
||
| 675 | */ |
||
| 676 | 16 | public function hasReachedSubordinateLimit() |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Check whether this organization has reached the upper limit of members. |
||
| 692 | * @return boolean |
||
| 693 | */ |
||
| 694 | 48 | public function hasReachedMemberLimit() |
|
| 707 | |||
| 708 | 29 | public function getIsExcludeOtherMembers() |
|
| 740 | |||
| 741 | /** |
||
| 742 | * @return $this|null|static |
||
| 743 | */ |
||
| 744 | 29 | public function getTopOrganization() |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Check whether the subordinates have the [[$user]] |
||
| 755 | * Note, this operation may consume the quantity of database selection. |
||
| 756 | * @param User $user |
||
| 757 | * @return bool |
||
| 758 | */ |
||
| 759 | 2 | public function hasMemberInSubordinates($user) |
|
| 774 | } |
||
| 775 |