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 |
||
| 45 | class Organization extends User |
||
| 46 | { |
||
| 47 | use SelfBlameableTrait; |
||
| 48 | |||
| 49 | const TYPE_ORGANIZATION = 1; |
||
| 50 | const TYPE_DEPARTMENT = 2; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var boolean Organization does not need password and corresponding features. |
||
| 54 | */ |
||
| 55 | public $passwordHashAttribute = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var boolean Organization does not need password and corresponding features. |
||
| 59 | */ |
||
| 60 | public $passwordResetTokenAttribute = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var boolean Organization does not need password and corresponding features. |
||
| 64 | */ |
||
| 65 | public $passwordHistoryClass = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var boolean Organization does not need source. |
||
| 69 | */ |
||
| 70 | public $sourceAttribute = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var boolean Organization does not need auth key. |
||
| 74 | */ |
||
| 75 | public $authKeyAttribute = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var boolean Organization does not need access token. |
||
| 79 | */ |
||
| 80 | public $accessTokenAttribute = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * |
||
| 84 | * @var boolean Organization does not need login log. |
||
| 85 | */ |
||
| 86 | public $loginLogClass = false; |
||
| 87 | |||
| 88 | public $profileClass = Profile::class; |
||
| 89 | |||
| 90 | public $memberClass = Member::class; |
||
| 91 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 92 | public $memberLimitClass = MemberLimit::class; |
||
| 93 | /** |
||
| 94 | * @var Member |
||
| 95 | */ |
||
| 96 | private $noInitMember; |
||
| 97 | /** |
||
| 98 | * @var SubordinateLimit |
||
| 99 | */ |
||
| 100 | private $noInitSubordinateLimit; |
||
| 101 | /** |
||
| 102 | * @var MemberLimit |
||
| 103 | */ |
||
| 104 | private $noInitMemberLimit; |
||
| 105 | public $creatorModel; |
||
| 106 | public $profileConfig; |
||
| 107 | /** |
||
| 108 | * @return Member |
||
| 109 | */ |
||
| 110 | 36 | protected function getNoInitMember() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return SubordinateLimit |
||
| 121 | */ |
||
| 122 | protected function getNoInitSubordinateLimit() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return MemberLimit |
||
| 133 | */ |
||
| 134 | protected function getNoInitMemberLimit() |
||
| 142 | |||
| 143 | 38 | public function init() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | 1 | public function attributeLabels() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | 38 | public static function tableName() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Find. |
||
| 189 | * Friendly to IDE. |
||
| 190 | * @return OrganizationQuery |
||
| 191 | */ |
||
| 192 | 38 | public static function find() |
|
| 196 | |||
| 197 | 37 | protected function getTypeRules() |
|
| 205 | |||
| 206 | 37 | public function rules() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get Member Query. |
||
| 213 | * @return MemberQuery |
||
| 214 | */ |
||
| 215 | 36 | public function getMembers() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get organization member users' query. |
||
| 222 | * @return BaseUserQuery |
||
| 223 | */ |
||
| 224 | 4 | public function getMemberUsers() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get subordinate limit query. |
||
| 234 | * @return null|BaseBlameableQuery |
||
| 235 | */ |
||
| 236 | public function getSubordinateLimit() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get member limit query. |
||
| 246 | * @return null|BaseBlameableQuery |
||
| 247 | */ |
||
| 248 | public function getMemberLimit() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get member with specified user. |
||
| 258 | * @param User|string|integer $user |
||
| 259 | * @return Member Null if `user` is not in this organization. |
||
| 260 | */ |
||
| 261 | 36 | public function getMember($user) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Add member to organization. |
||
| 268 | * @param Member|User|string|integer $member |
||
| 269 | * @see createMemberModel |
||
| 270 | * @see createMemberModelWithUser |
||
| 271 | * @return boolean |
||
| 272 | */ |
||
| 273 | 36 | public function addMember(&$member) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Create member model, and set organization with this. |
||
| 297 | * @param Member $member If this parameter is not new record, it's organization |
||
| 298 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 299 | * model and create new `Member` model. |
||
| 300 | * @see createMemberModelWithUser |
||
| 301 | * @return Member |
||
| 302 | */ |
||
| 303 | public function createMemberModel($member) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Create member model with user, and set organization with this. |
||
| 314 | * @param User|string|integer $user |
||
| 315 | * @return Member |
||
| 316 | */ |
||
| 317 | 36 | public function createMemberModelWithUser($user) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Remove member. |
||
| 331 | * Note: the creator cannot be removed. |
||
| 332 | * @param Member|User $member |
||
| 333 | * @return boolean |
||
| 334 | */ |
||
| 335 | 1 | public function removeMember(&$member) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Remove administrator. |
||
| 352 | * @param Member|User $member |
||
| 353 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
| 354 | * @return boolean |
||
| 355 | * @throws IntegrityException |
||
| 356 | */ |
||
| 357 | public function removeAdministrator(&$member, $keepMember = true) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @param Event $event |
||
| 378 | */ |
||
| 379 | 37 | public function onAddProfile($event) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * |
||
| 390 | * @param Event $event |
||
| 391 | */ |
||
| 392 | 37 | public function onAssignCreator($event) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * |
||
| 399 | * @param Event $event |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | 17 | public function onRevokeCreator($event) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * |
||
| 414 | * @param Event $event |
||
| 415 | * @return boolean |
||
| 416 | */ |
||
| 417 | 17 | public function onRevokeAdministrators($event) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * |
||
| 432 | * @param Event $event |
||
| 433 | */ |
||
| 434 | 17 | public function onRevokePermissions($event) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Check whether current instance is an organization. |
||
| 441 | * @return boolean |
||
| 442 | */ |
||
| 443 | 2 | public function isOrganization() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Check whether current instance if a department. |
||
| 450 | * @return boolean |
||
| 451 | */ |
||
| 452 | 2 | public function isDepartment() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Check whether the current organization has a member. |
||
| 459 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 460 | * @return boolean |
||
| 461 | */ |
||
| 462 | 36 | public function hasMember($user) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Get member query which role is specified `Creator`. |
||
| 469 | * @return MemberQuery |
||
| 470 | */ |
||
| 471 | 18 | public function getMemberCreators() |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Get member query which role is specified `Administrator`. |
||
| 478 | * @return MemberQuery |
||
| 479 | */ |
||
| 480 | 18 | public function getMemberAdministrators() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Get user query which role is specified `Creator`. |
||
| 487 | * @return BaseUserQuery |
||
| 488 | */ |
||
| 489 | 1 | public function getCreator() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Get user query which role is specified `Administrator`. |
||
| 499 | * @return BaseUserQuery |
||
| 500 | */ |
||
| 501 | 1 | public function getAdministrators() |
|
| 508 | |||
| 509 | /** |
||
| 510 | * |
||
| 511 | * @param User $user |
||
| 512 | * @return boolean |
||
| 513 | * @throws \Exception |
||
| 514 | * @throws IntegrityException |
||
| 515 | */ |
||
| 516 | 37 | protected function addCreator($user) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * |
||
| 543 | * @param User $user |
||
| 544 | * @return boolean |
||
| 545 | * @throws \Exception |
||
| 546 | * @throws IntegrityException |
||
| 547 | */ |
||
| 548 | 7 | public function addAdministrator($user) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * |
||
| 568 | * @param type $user |
||
| 569 | * @return boolean |
||
| 570 | */ |
||
| 571 | 2 | public function hasAdministrator($user) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 582 | * @return boolean |
||
| 583 | */ |
||
| 584 | public function hasReachedSubordinateLimit() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Check whether this organization has reached the upper limit of members. |
||
| 600 | * @return boolean |
||
| 601 | */ |
||
| 602 | public function hasReachedMemberLimit() |
||
| 615 | } |
||
| 616 |