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 |
||
| 47 | class Organization extends User |
||
| 48 | { |
||
| 49 | use SelfBlameableTrait; |
||
| 50 | |||
| 51 | const TYPE_ORGANIZATION = 1; |
||
| 52 | const TYPE_DEPARTMENT = 2; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var boolean Organization does not need password and corresponding features. |
||
| 56 | */ |
||
| 57 | public $passwordHashAttribute = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean Organization does not need password and corresponding features. |
||
| 61 | */ |
||
| 62 | public $passwordResetTokenAttribute = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var boolean Organization does not need password and corresponding features. |
||
| 66 | */ |
||
| 67 | public $passwordHistoryClass = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean Organization does not need source. |
||
| 71 | */ |
||
| 72 | public $sourceAttribute = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var boolean Organization does not need auth key. |
||
| 76 | */ |
||
| 77 | public $authKeyAttribute = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var boolean Organization does not need access token. |
||
| 81 | */ |
||
| 82 | public $accessTokenAttribute = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @var boolean Organization does not need login log. |
||
| 87 | */ |
||
| 88 | public $loginLogClass = false; |
||
| 89 | |||
| 90 | public $profileClass = Profile::class; |
||
| 91 | |||
| 92 | public $memberClass = Member::class; |
||
| 93 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 94 | public $memberLimitClass = MemberLimit::class; |
||
| 95 | /** |
||
| 96 | * @var Member |
||
| 97 | */ |
||
| 98 | private $noInitMember; |
||
| 99 | /** |
||
| 100 | * @var SubordinateLimit |
||
| 101 | */ |
||
| 102 | private $noInitSubordinateLimit; |
||
| 103 | /** |
||
| 104 | * @var MemberLimit |
||
| 105 | */ |
||
| 106 | private $noInitMemberLimit; |
||
| 107 | public $creatorModel; |
||
| 108 | public $profileConfig; |
||
| 109 | /** |
||
| 110 | * @return Member |
||
| 111 | */ |
||
| 112 | 39 | protected function getNoInitMember() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return SubordinateLimit |
||
| 123 | */ |
||
| 124 | 2 | protected function getNoInitSubordinateLimit() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @return MemberLimit |
||
| 135 | */ |
||
| 136 | 1 | protected function getNoInitMemberLimit() |
|
| 144 | |||
| 145 | 41 | public function init() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | */ |
||
| 166 | 1 | public function attributeLabels() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | 41 | public static function tableName() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Find. |
||
| 191 | * Friendly to IDE. |
||
| 192 | * @return OrganizationQuery |
||
| 193 | */ |
||
| 194 | 41 | public static function find() |
|
| 198 | |||
| 199 | 40 | protected function getTypeRules() |
|
| 207 | |||
| 208 | 40 | public function rules() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get Member Query. |
||
| 215 | * @return MemberQuery |
||
| 216 | */ |
||
| 217 | 39 | public function getMembers() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get organization member users' query. |
||
| 224 | * @return BaseUserQuery |
||
| 225 | */ |
||
| 226 | 4 | public function getMemberUsers() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get subordinate limit query. |
||
| 236 | * @return null|BaseBlameableQuery |
||
| 237 | */ |
||
| 238 | 2 | public function getSubordinateLimit() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get member limit query. |
||
| 248 | * @return null|BaseBlameableQuery |
||
| 249 | */ |
||
| 250 | 1 | public function getMemberLimit() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Get member with specified user. |
||
| 260 | * @param User|string|integer $user |
||
| 261 | * @return Member Null if `user` is not in this organization. |
||
| 262 | */ |
||
| 263 | 39 | public function getMember($user) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Add member to organization. |
||
| 270 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
| 271 | * If member is created, it will be re-assigned to this parameter. |
||
| 272 | * @see createMemberModel |
||
| 273 | * @see createMemberModelWithUser |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | 39 | public function addMember(&$member) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Create member model, and set organization with this. |
||
| 303 | * @param Member $member If this parameter is not new record, it's organization |
||
| 304 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 305 | * model and create new `Member` model. |
||
| 306 | * @see createMemberModelWithUser |
||
| 307 | * @return Member |
||
| 308 | */ |
||
| 309 | public function createMemberModel($member) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Create member model with user, and set organization with this. |
||
| 320 | * @param User|string|integer $user |
||
| 321 | * @return Member |
||
| 322 | */ |
||
| 323 | 39 | public function createMemberModelWithUser($user) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Remove member. |
||
| 337 | * Note: the creator cannot be removed. |
||
| 338 | * @param Member|User $member |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | 1 | public function removeMember(&$member) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Remove administrator. |
||
| 358 | * @param Member|User $member |
||
| 359 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
| 360 | * @return boolean |
||
| 361 | * @throws IntegrityException |
||
| 362 | */ |
||
| 363 | public function removeAdministrator(&$member, $keepMember = true) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * |
||
| 383 | * @param Event $event |
||
| 384 | */ |
||
| 385 | 40 | public function onAddProfile($event) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * |
||
| 396 | * @param Event $event |
||
| 397 | */ |
||
| 398 | 40 | public function onAssignCreator($event) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * |
||
| 405 | * @param Event $event |
||
| 406 | * @return boolean |
||
| 407 | */ |
||
| 408 | 20 | public function onRevokeCreator($event) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * |
||
| 420 | * @param Event $event |
||
| 421 | * @return boolean |
||
| 422 | */ |
||
| 423 | 20 | public function onRevokeAdministrators($event) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * |
||
| 438 | * @param Event $event |
||
| 439 | */ |
||
| 440 | 20 | public function onRevokePermissions($event) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Check whether current instance is an organization. |
||
| 447 | * @return boolean |
||
| 448 | */ |
||
| 449 | 2 | public function isOrganization() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Check whether current instance if a department. |
||
| 456 | * @return boolean |
||
| 457 | */ |
||
| 458 | 2 | public function isDepartment() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Check whether the current organization has a member. |
||
| 465 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 466 | * @return boolean |
||
| 467 | */ |
||
| 468 | 39 | public function hasMember($user) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Get member query which role is specified `Creator`. |
||
| 475 | * @return MemberQuery |
||
| 476 | */ |
||
| 477 | 21 | public function getMemberCreators() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get member query which role is specified `Administrator`. |
||
| 484 | * @return MemberQuery |
||
| 485 | */ |
||
| 486 | 21 | public function getMemberAdministrators() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get user query which role is specified `Creator`. |
||
| 493 | * @return BaseUserQuery |
||
| 494 | */ |
||
| 495 | 1 | public function getCreator() |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get user query which role is specified `Administrator`. |
||
| 505 | * @return BaseUserQuery |
||
| 506 | */ |
||
| 507 | 1 | public function getAdministrators() |
|
| 514 | |||
| 515 | /** |
||
| 516 | * |
||
| 517 | * @param User $user |
||
| 518 | * @return boolean |
||
| 519 | * @throws \Exception |
||
| 520 | * @throws IntegrityException |
||
| 521 | */ |
||
| 522 | 40 | protected function addCreator($user) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * |
||
| 549 | * @param User $user |
||
| 550 | * @return boolean |
||
| 551 | * @throws \Exception |
||
| 552 | * @throws IntegrityException |
||
| 553 | */ |
||
| 554 | 7 | public function addAdministrator($user) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * |
||
| 574 | * @param type $user |
||
| 575 | * @return boolean |
||
| 576 | */ |
||
| 577 | 2 | public function hasAdministrator($user) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 588 | * @return boolean |
||
| 589 | */ |
||
| 590 | 9 | public function hasReachedSubordinateLimit() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Check whether this organization has reached the upper limit of members. |
||
| 606 | * @return boolean |
||
| 607 | */ |
||
| 608 | 39 | public function hasReachedMemberLimit() |
|
| 621 | } |
||
| 622 |