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 |
||
| 61 | class Organization extends User |
||
| 62 | { |
||
| 63 | use SelfBlameableTrait; |
||
| 64 | |||
| 65 | const TYPE_ORGANIZATION = 1; |
||
| 66 | const TYPE_DEPARTMENT = 2; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var boolean Organization does not need password and corresponding features. |
||
| 70 | */ |
||
| 71 | public $passwordHashAttribute = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var boolean Organization does not need password and corresponding features. |
||
| 75 | */ |
||
| 76 | public $passwordResetTokenAttribute = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var boolean Organization does not need password and corresponding features. |
||
| 80 | */ |
||
| 81 | public $passwordHistoryClass = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var boolean Organization does not need source. |
||
| 85 | */ |
||
| 86 | public $sourceAttribute = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var boolean Organization does not need auth key. |
||
| 90 | */ |
||
| 91 | public $authKeyAttribute = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var boolean Organization does not need access token. |
||
| 95 | */ |
||
| 96 | public $accessTokenAttribute = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * |
||
| 100 | * @var boolean Organization does not need login log. |
||
| 101 | */ |
||
| 102 | public $loginLogClass = false; |
||
| 103 | |||
| 104 | public $profileClass = Profile::class; |
||
| 105 | |||
| 106 | public $memberClass = Member::class; |
||
| 107 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 108 | public $memberLimitClass = MemberLimit::class; |
||
| 109 | public $searchClass = OrganizationSearch::class; |
||
| 110 | /** |
||
| 111 | * @var Member |
||
| 112 | */ |
||
| 113 | private $noInitMember; |
||
| 114 | /** |
||
| 115 | * @var SubordinateLimit |
||
| 116 | */ |
||
| 117 | private $noInitSubordinateLimit; |
||
| 118 | /** |
||
| 119 | * @var MemberLimit |
||
| 120 | */ |
||
| 121 | private $noInitMemberLimit; |
||
| 122 | public $creatorModel; |
||
| 123 | public $profileConfig; |
||
| 124 | |||
| 125 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
| 126 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
| 127 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
| 128 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return Member |
||
| 132 | */ |
||
| 133 | 44 | public function getNoInitMember() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return SubordinateLimit |
||
| 144 | */ |
||
| 145 | 2 | public function getNoInitSubordinateLimit() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return MemberLimit |
||
| 156 | */ |
||
| 157 | 1 | public function getNoInitMemberLimit() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return null|OrganizationSearch |
||
| 168 | */ |
||
| 169 | public function getSearchModel() |
||
| 177 | |||
| 178 | 45 | public function init() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | 1 | public function attributeLabels() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @inheritdoc |
||
| 220 | */ |
||
| 221 | 45 | public static function tableName() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Find. |
||
| 228 | * Friendly to IDE. |
||
| 229 | * @return OrganizationQuery |
||
| 230 | */ |
||
| 231 | 45 | public static function find() |
|
| 235 | |||
| 236 | 44 | protected function getTypeRules() |
|
| 244 | |||
| 245 | 44 | public function rules() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get Member Query. |
||
| 252 | * @return MemberQuery |
||
| 253 | */ |
||
| 254 | 43 | public function getMembers() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Get organization member users' query. |
||
| 261 | * @return BaseUserQuery |
||
| 262 | */ |
||
| 263 | 5 | public function getMemberUsers() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get subordinate limit query. |
||
| 273 | * @return null|BaseBlameableQuery |
||
| 274 | */ |
||
| 275 | 2 | public function getSubordinateLimit() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get member limit query. |
||
| 285 | * @return null|BaseBlameableQuery |
||
| 286 | */ |
||
| 287 | 1 | public function getMemberLimit() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Get member with specified user. |
||
| 297 | * @param User|string|integer $user |
||
| 298 | * @return Member Null if `user` is not in this organization. |
||
| 299 | */ |
||
| 300 | 43 | public function getMember($user) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Add member to organization. |
||
| 307 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 308 | * If member is created, it will be re-assigned to this parameter. |
||
| 309 | * @see createMemberModel |
||
| 310 | * @see createMemberModelWithUser |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | 43 | public function addMember(&$member) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Create member model, and set organization with this. |
||
| 343 | * @param Member $member If this parameter is not new record, it's organization |
||
| 344 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 345 | * model and create new `Member` model. |
||
| 346 | * @see createMemberModelWithUser |
||
| 347 | * @return Member |
||
| 348 | */ |
||
| 349 | public function createMemberModel($member) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Create member model with user, and set organization with this. |
||
| 360 | * @param User|string|integer $user |
||
| 361 | * @return Member |
||
| 362 | */ |
||
| 363 | 43 | public function createMemberModelWithUser($user) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Remove member. |
||
| 377 | * Note: the creator cannot be removed. |
||
| 378 | * @param Member|User $member |
||
| 379 | * @return boolean |
||
| 380 | */ |
||
| 381 | 1 | public function removeMember(&$member) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Remove administrator. |
||
| 401 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
| 402 | * @param boolean $keep Keep member after administrator being revoked. |
||
| 403 | * @return boolean |
||
| 404 | * @throws IntegrityException |
||
| 405 | */ |
||
| 406 | public function removeAdministrator(&$member, $keep = true) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * |
||
| 426 | * @param Event $event |
||
| 427 | * @throws IntegrityException |
||
| 428 | * @return boolean |
||
| 429 | */ |
||
| 430 | 44 | public function onAddProfile($event) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * |
||
| 441 | * @param Event $event |
||
| 442 | */ |
||
| 443 | 44 | public function onAssignCreator($event) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * |
||
| 450 | * @param Event $event |
||
| 451 | * @return boolean |
||
| 452 | */ |
||
| 453 | 20 | public function onRevokeCreator($event) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * |
||
| 465 | * @param Event $event |
||
| 466 | * @return boolean |
||
| 467 | */ |
||
| 468 | 20 | public function onRevokeAdministrators($event) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * |
||
| 483 | * @param Event $event |
||
| 484 | */ |
||
| 485 | 20 | public function onRevokePermissions($event) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Check whether current instance is an organization. |
||
| 492 | * @return boolean |
||
| 493 | */ |
||
| 494 | 2 | public function isOrganization() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Check whether current instance if a department. |
||
| 501 | * @return boolean |
||
| 502 | */ |
||
| 503 | 2 | public function isDepartment() |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Check whether the current organization has a member. |
||
| 510 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 511 | * @return boolean |
||
| 512 | */ |
||
| 513 | 43 | public function hasMember($user) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Get member query which role is specified `Creator`. |
||
| 520 | * @return MemberQuery |
||
| 521 | */ |
||
| 522 | 24 | public function getMemberCreators() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Get member query which role is specified `Administrator`. |
||
| 529 | * @return MemberQuery |
||
| 530 | */ |
||
| 531 | 22 | public function getMemberAdministrators() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Get user query which role is specified `Creator`. |
||
| 538 | * @return BaseUserQuery |
||
| 539 | */ |
||
| 540 | 4 | public function getCreator() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Get user query which role is specified `Administrator`. |
||
| 550 | * @return BaseUserQuery |
||
| 551 | */ |
||
| 552 | 2 | public function getAdministrators() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * |
||
| 562 | * @param User $user |
||
| 563 | * @return boolean |
||
| 564 | * @throws \Exception |
||
| 565 | * @throws IntegrityException |
||
| 566 | */ |
||
| 567 | 44 | protected function addCreator($user) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Add administrator. |
||
| 594 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
| 595 | * @return boolean |
||
| 596 | * @throws \Exception |
||
| 597 | * @throws IntegrityException |
||
| 598 | */ |
||
| 599 | 10 | public function addAdministrator($user) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Check whether the current organization has administrator. |
||
| 619 | * @param User|integer|string $user |
||
| 620 | * @return boolean |
||
| 621 | */ |
||
| 622 | 2 | public function hasAdministrator($user) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 633 | * @return boolean |
||
| 634 | */ |
||
| 635 | 11 | public function hasReachedSubordinateLimit() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Check whether this organization has reached the upper limit of members. |
||
| 651 | * @return boolean |
||
| 652 | */ |
||
| 653 | 43 | public function hasReachedMemberLimit() |
|
| 666 | |||
| 667 | public function getIsExcludeOtherMembers() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @return $this|null|static |
||
| 702 | */ |
||
| 703 | public function getTopOrganization() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Check whether the subordinates have the [[$user]] |
||
| 713 | * Note, this operation may consume the quantity of database selection. |
||
| 714 | * @param User $user |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | 1 | public function hasMemberInSubordinates($user) |
|
| 732 | } |
||
| 733 |