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 |
||
66 | class Organization extends User |
||
67 | { |
||
68 | use SelfBlameableTrait; |
||
69 | |||
70 | const TYPE_ORGANIZATION = 1; |
||
71 | const TYPE_DEPARTMENT = 2; |
||
72 | |||
73 | /** |
||
74 | * @var boolean Organization does not need password and corresponding features. |
||
75 | */ |
||
76 | public $passwordHashAttribute = false; |
||
77 | |||
78 | /** |
||
79 | * @var boolean Organization does not need password and corresponding features. |
||
80 | */ |
||
81 | public $passwordResetTokenAttribute = false; |
||
82 | |||
83 | /** |
||
84 | * @var boolean Organization does not need password and corresponding features. |
||
85 | */ |
||
86 | public $passwordHistoryClass = false; |
||
87 | |||
88 | /** |
||
89 | * @var boolean Organization does not need source. |
||
90 | */ |
||
91 | public $sourceAttribute = false; |
||
92 | |||
93 | /** |
||
94 | * @var boolean Organization does not need auth key. |
||
95 | */ |
||
96 | public $authKeyAttribute = false; |
||
97 | |||
98 | /** |
||
99 | * @var boolean Organization does not need access token. |
||
100 | */ |
||
101 | public $accessTokenAttribute = false; |
||
102 | |||
103 | /** |
||
104 | * |
||
105 | * @var boolean Organization does not need login log. |
||
106 | */ |
||
107 | public $loginLogClass = false; |
||
108 | |||
109 | public $profileClass = Profile::class; |
||
110 | |||
111 | public $memberClass = Member::class; |
||
112 | public $subordinateLimitClass = SubordinateLimit::class; |
||
113 | public $memberLimitClass = MemberLimit::class; |
||
114 | public $searchClass = OrganizationSearch::class; |
||
115 | /** |
||
116 | * @var Member |
||
117 | */ |
||
118 | private $noInitMember; |
||
119 | /** |
||
120 | * @var SubordinateLimit |
||
121 | */ |
||
122 | private $noInitSubordinateLimit; |
||
123 | /** |
||
124 | * @var MemberLimit |
||
125 | */ |
||
126 | private $noInitMemberLimit; |
||
127 | public $creatorModel; |
||
128 | public $profileConfig; |
||
129 | |||
130 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
131 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
132 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
133 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
134 | |||
135 | /** |
||
136 | * @return Member |
||
137 | */ |
||
138 | 51 | public function getNoInitMember() |
|
146 | |||
147 | /** |
||
148 | * @return SubordinateLimit |
||
149 | */ |
||
150 | 2 | public function getNoInitSubordinateLimit() |
|
158 | |||
159 | /** |
||
160 | * @return MemberLimit |
||
161 | */ |
||
162 | 1 | public function getNoInitMemberLimit() |
|
170 | |||
171 | /** |
||
172 | * @return null|OrganizationSearch |
||
173 | */ |
||
174 | public function getSearchModel() |
||
182 | |||
183 | 52 | public function init() |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | 1 | public function attributeLabels() |
|
222 | |||
223 | /** |
||
224 | * @inheritdoc |
||
225 | */ |
||
226 | 52 | public static function tableName() |
|
230 | |||
231 | /** |
||
232 | * Find. |
||
233 | * Friendly to IDE. |
||
234 | * @return OrganizationQuery |
||
235 | */ |
||
236 | 52 | public static function find() |
|
240 | |||
241 | 51 | protected function getTypeRules() |
|
250 | |||
251 | 51 | public function rules() |
|
255 | |||
256 | /** |
||
257 | * Get Member Query. |
||
258 | * @return MemberQuery |
||
259 | */ |
||
260 | 50 | public function getMembers() |
|
266 | |||
267 | /** |
||
268 | * Get organization member users' query. |
||
269 | * @return BaseUserQuery |
||
270 | */ |
||
271 | 6 | public function getMemberUsers() |
|
280 | |||
281 | /** |
||
282 | * Get subordinate limit query. |
||
283 | * @return null|BaseBlameableQuery |
||
284 | */ |
||
285 | 2 | public function getSubordinateLimit() |
|
294 | |||
295 | /** |
||
296 | * Get member limit query. |
||
297 | * @return null|BaseBlameableQuery |
||
298 | */ |
||
299 | 1 | public function getMemberLimit() |
|
308 | |||
309 | /** |
||
310 | * Get member with specified user. |
||
311 | * @param User|string|integer $user |
||
312 | * @return Member Null if `user` is not in this organization. |
||
313 | */ |
||
314 | 50 | public function getMember($user) |
|
318 | |||
319 | /** |
||
320 | * Add member to organization. |
||
321 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
322 | * If member is created, it will be re-assigned to this parameter. |
||
323 | * @see createMemberModel |
||
324 | * @see createMemberModelWithUser |
||
325 | * @return boolean |
||
326 | * @throws DisallowMemberJoinOtherException |
||
327 | * @throws ExcludeOtherMembersException |
||
328 | * @throws OnlyAcceptCurrentOrgMemberException |
||
329 | * @throws OnlyAcceptSuperiorOrgMemberException |
||
330 | */ |
||
331 | 50 | public function addMember(&$member) |
|
385 | |||
386 | /** |
||
387 | * Create member model, and set organization with this. |
||
388 | * @param Member $member If this parameter is not new record, it's organization |
||
389 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
390 | * model and create new `Member` model. |
||
391 | * @see createMemberModelWithUser |
||
392 | * @return Member |
||
393 | */ |
||
394 | public function createMemberModel($member) |
||
402 | |||
403 | /** |
||
404 | * Create member model with user, and set organization with this. |
||
405 | * @param User|string|integer $user |
||
406 | * @return Member |
||
407 | */ |
||
408 | 50 | public function createMemberModelWithUser($user) |
|
419 | |||
420 | /** |
||
421 | * Remove member. |
||
422 | * Note: the creator cannot be removed. |
||
423 | * @param Member|User $member |
||
424 | * @return boolean |
||
425 | */ |
||
426 | 4 | public function removeMember(&$member) |
|
443 | |||
444 | /** |
||
445 | * Remove administrator. |
||
446 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
447 | * @param boolean $keep Keep member after administrator being revoked. |
||
448 | * @return boolean |
||
449 | * @throws IntegrityException |
||
450 | */ |
||
451 | public function removeAdministrator(&$member, $keep = true) |
||
468 | |||
469 | /** |
||
470 | * |
||
471 | * @param Event $event |
||
472 | * @throws IntegrityException |
||
473 | * @return boolean |
||
474 | */ |
||
475 | 51 | public function onAddProfile($event) |
|
483 | |||
484 | /** |
||
485 | * |
||
486 | * @param Event $event |
||
487 | */ |
||
488 | 51 | public function onAssignCreator($event) |
|
492 | |||
493 | /** |
||
494 | * |
||
495 | * @param Event $event |
||
496 | * @return boolean |
||
497 | */ |
||
498 | 20 | public function onRevokeCreator($event) |
|
507 | |||
508 | /** |
||
509 | * |
||
510 | * @param Event $event |
||
511 | * @return boolean |
||
512 | */ |
||
513 | 20 | public function onRevokeAdministrators($event) |
|
525 | |||
526 | /** |
||
527 | * |
||
528 | * @param Event $event |
||
529 | */ |
||
530 | 20 | public function onRevokePermissions($event) |
|
534 | |||
535 | /** |
||
536 | * Check whether current instance is an organization. |
||
537 | * @return boolean |
||
538 | */ |
||
539 | 50 | public function isOrganization() |
|
543 | |||
544 | /** |
||
545 | * Check whether current instance if a department. |
||
546 | * @return boolean |
||
547 | */ |
||
548 | 50 | public function isDepartment() |
|
552 | |||
553 | /** |
||
554 | * Check whether the current organization has a member. |
||
555 | * @param User|string|integer $user User instance, GUID or ID. |
||
556 | * @return boolean |
||
557 | */ |
||
558 | 50 | public function hasMember($user) |
|
562 | |||
563 | /** |
||
564 | * Get member query which role is specified `Creator`. |
||
565 | * @return MemberQuery |
||
566 | */ |
||
567 | 24 | public function getMemberCreators() |
|
571 | |||
572 | /** |
||
573 | * Get member query which role is specified `Administrator`. |
||
574 | * @return MemberQuery |
||
575 | */ |
||
576 | 22 | public function getMemberAdministrators() |
|
580 | |||
581 | /** |
||
582 | * Get user query which role is specified `Creator`. |
||
583 | * @return BaseUserQuery |
||
584 | */ |
||
585 | 4 | public function getCreator() |
|
594 | |||
595 | /** |
||
596 | * Get user query which role is specified `Administrator`. |
||
597 | * @return BaseUserQuery |
||
598 | */ |
||
599 | 2 | public function getAdministrators() |
|
608 | |||
609 | /** |
||
610 | * |
||
611 | * @param User $user |
||
612 | * @return boolean |
||
613 | * @throws \Exception |
||
614 | * @throws IntegrityException |
||
615 | */ |
||
616 | 51 | protected function addCreator($user) |
|
640 | |||
641 | /** |
||
642 | * Add administrator. |
||
643 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
644 | * @return boolean |
||
645 | * @throws \Exception |
||
646 | * @throws IntegrityException |
||
647 | */ |
||
648 | 17 | public function addAdministrator($user) |
|
665 | |||
666 | /** |
||
667 | * Check whether the current organization has administrator. |
||
668 | * @param User|integer|string $user |
||
669 | * @return boolean |
||
670 | */ |
||
671 | 2 | public function hasAdministrator($user) |
|
679 | |||
680 | /** |
||
681 | * Check whether this organization has reached the upper limit of subordinates. |
||
682 | * @return boolean |
||
683 | */ |
||
684 | 18 | public function hasReachedSubordinateLimit() |
|
697 | |||
698 | /** |
||
699 | * Check whether this organization has reached the upper limit of members. |
||
700 | * @return boolean |
||
701 | */ |
||
702 | 50 | public function hasReachedMemberLimit() |
|
715 | |||
716 | 31 | public function getIsExcludeOtherMembers() |
|
748 | |||
749 | /** |
||
750 | * @return $this|null|static |
||
751 | */ |
||
752 | 31 | public function getTopOrganization() |
|
760 | |||
761 | /** |
||
762 | * Check whether the subordinates have the [[$user]] |
||
763 | * Note, this operation may consume the quantity of database selection. |
||
764 | * @param User $user |
||
765 | * @return bool |
||
766 | */ |
||
767 | 2 | public function hasMemberInSubordinates($user) |
|
782 | } |
||
783 |