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 |
||
| 44 | class Organization extends User |
||
| 45 | { |
||
| 46 | use SelfBlameableTrait; |
||
| 47 | |||
| 48 | const TYPE_ORGANIZATION = 1; |
||
| 49 | const TYPE_DEPARTMENT = 2; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean Organization does not need password and corresponding features. |
||
| 53 | */ |
||
| 54 | public $passwordHashAttribute = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean Organization does not need password and corresponding features. |
||
| 58 | */ |
||
| 59 | public $passwordResetTokenAttribute = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean Organization does not need password and corresponding features. |
||
| 63 | */ |
||
| 64 | public $passwordHistoryClass = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var boolean Organization does not need source. |
||
| 68 | */ |
||
| 69 | public $sourceAttribute = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean Organization does not need auth key. |
||
| 73 | */ |
||
| 74 | public $authKeyAttribute = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean Organization does not need access token. |
||
| 78 | */ |
||
| 79 | public $accessTokenAttribute = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * |
||
| 83 | * @var boolean Organization does not need login log. |
||
| 84 | */ |
||
| 85 | public $loginLogClass = false; |
||
| 86 | |||
| 87 | public $profileClass = Profile::class; |
||
| 88 | |||
| 89 | public $memberClass = Member::class; |
||
| 90 | public $subordinateLimitClass = SubordinateLimit::class; |
||
| 91 | public $memberLimitClass = MemberLimit::class; |
||
| 92 | /** |
||
| 93 | * @var Member |
||
| 94 | */ |
||
| 95 | private $noInitMember; |
||
| 96 | /** |
||
| 97 | * @var SubordinateLimit |
||
| 98 | */ |
||
| 99 | private $noInitSubordinateLimit; |
||
| 100 | /** |
||
| 101 | * @var MemberLimit |
||
| 102 | */ |
||
| 103 | private $noInitMemberLimit; |
||
| 104 | public $creatorModel; |
||
| 105 | public $profileConfig; |
||
| 106 | /** |
||
| 107 | * @return Member |
||
| 108 | */ |
||
| 109 | 36 | protected function getNoInitMember() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return SubordinateLimit |
||
| 120 | */ |
||
| 121 | protected function getNoInitSubordinateLimit() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return MemberLimit |
||
| 132 | */ |
||
| 133 | protected function getNoInitMemberLimit() |
||
| 141 | |||
| 142 | 38 | public function init() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @inheritdoc |
||
| 162 | */ |
||
| 163 | 36 | public function attributeLabels() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | 38 | public static function tableName() |
|
| 185 | |||
| 186 | 37 | protected function getTypeRules() |
|
| 194 | |||
| 195 | 37 | public function rules() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get Member Query. |
||
| 202 | * @return MemberQuery |
||
| 203 | */ |
||
| 204 | 36 | public function getMembers() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get organization member users' query. |
||
| 211 | * @return BaseUserQuery |
||
| 212 | */ |
||
| 213 | 4 | public function getMemberUsers() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get subordinate limit query. |
||
| 223 | * @return null|BaseBlameableQuery |
||
| 224 | */ |
||
| 225 | 17 | public function getSubordinateLimit() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get member limit query. |
||
| 235 | * @return null|BaseBlameableQuery |
||
| 236 | */ |
||
| 237 | 17 | public function getMemberLimit() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get member with specified user. |
||
| 247 | * @param User|string|integer $user |
||
| 248 | * @return Member Null if `user` is not in this organization. |
||
| 249 | */ |
||
| 250 | 36 | public function getMember($user) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Add member to organization. |
||
| 257 | * @param Member|User|string|integer $member |
||
| 258 | * @see createMemberModel |
||
| 259 | * @see createMemberModelWithUser |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | 36 | public function addMember(&$member) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Create member model, and set organization with this. |
||
| 286 | * @param Member $member If this parameter is not new record, it's organization |
||
| 287 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 288 | * model and create new `Member` model. |
||
| 289 | * @see createMemberModelWithUser |
||
| 290 | * @return Member |
||
| 291 | */ |
||
| 292 | public function createMemberModel($member) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Create member model with user, and set organization with this. |
||
| 303 | * @param User|string|integer $user |
||
| 304 | * @return Member |
||
| 305 | */ |
||
| 306 | 36 | public function createMemberModelWithUser($user) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Remove member. |
||
| 320 | * @param Member|User $member |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | 1 | public function removeMember(&$member) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Remove administrator. |
||
| 337 | * @param Member|User $member |
||
| 338 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
| 339 | * @return boolean |
||
| 340 | * @throws IntegrityException |
||
| 341 | */ |
||
| 342 | public function removeAdministrator(&$member, $keepMember = true) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * |
||
| 362 | * @param Event $event |
||
| 363 | */ |
||
| 364 | 37 | public function onAddProfile($event) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * |
||
| 375 | * @param Event $event |
||
| 376 | */ |
||
| 377 | 37 | public function onAssignCreator($event) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * |
||
| 384 | * @param Event $event |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | 17 | public function onRevokeCreator($event) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * |
||
| 399 | * @param Event $event |
||
| 400 | * @return boolean |
||
| 401 | */ |
||
| 402 | 17 | public function onRevokeAdministrators($event) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * |
||
| 417 | * @param Event $event |
||
| 418 | */ |
||
| 419 | 17 | public function onRevokePermissions($event) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Check whether current instance is an organization. |
||
| 426 | * @return boolean |
||
| 427 | */ |
||
| 428 | 2 | public function isOrganization() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Check whether current instance if a department. |
||
| 435 | * @return boolean |
||
| 436 | */ |
||
| 437 | 2 | public function isDepartment() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Check whether the current organization has a member. |
||
| 444 | * @param User|string|integer $user User instance, GUID or ID. |
||
| 445 | * @return boolean |
||
| 446 | */ |
||
| 447 | 36 | public function hasMember($user) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get member query which role is specified `Creator`. |
||
| 454 | * @return MemberQuery |
||
| 455 | */ |
||
| 456 | 18 | public function getMemberCreators() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get member query which role is specified `Administrator`. |
||
| 463 | * @return MemberQuery |
||
| 464 | */ |
||
| 465 | 18 | public function getMemberAdministrators() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get user query which role is specified `Creator`. |
||
| 472 | * @return BaseUserQuery |
||
| 473 | */ |
||
| 474 | 1 | public function getCreator() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get user query which role is specified `Administrator`. |
||
| 484 | * @return BaseUserQuery |
||
| 485 | */ |
||
| 486 | 1 | public function getAdministrators() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * |
||
| 496 | * @param User $user |
||
| 497 | * @return boolean |
||
| 498 | * @throws \Exception |
||
| 499 | * @throws IntegrityException |
||
| 500 | */ |
||
| 501 | 37 | protected function addCreator($user) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * |
||
| 528 | * @param User $user |
||
| 529 | * @return boolean |
||
| 530 | * @throws \Exception |
||
| 531 | * @throws IntegrityException |
||
| 532 | */ |
||
| 533 | 7 | public function addAdministrator($user) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * |
||
| 553 | * @param type $user |
||
| 554 | * @return boolean |
||
| 555 | */ |
||
| 556 | 2 | public function hasAdministrator($user) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Check whether this organization has reached the upper limit of subordinates. |
||
| 567 | * @return boolean |
||
| 568 | */ |
||
| 569 | public function hasReachedSubordinateLimit() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Check whether this organization has reached the upper limit of members. |
||
| 585 | * @return boolean |
||
| 586 | */ |
||
| 587 | public function hasReachedMemberLimit() |
||
| 600 | } |
||
| 601 |