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 |
||
85 | class Organization extends User |
||
86 | { |
||
87 | use SelfBlameableTrait; |
||
88 | |||
89 | const TYPE_ORGANIZATION = 1; |
||
90 | const TYPE_DEPARTMENT = 2; |
||
91 | |||
92 | /** |
||
93 | * @var boolean Organization does not need password and corresponding features. |
||
94 | */ |
||
95 | public $passwordHashAttribute = false; |
||
96 | |||
97 | /** |
||
98 | * @var boolean Organization does not need password and corresponding features. |
||
99 | */ |
||
100 | public $passwordResetTokenAttribute = false; |
||
101 | |||
102 | /** |
||
103 | * @var boolean Organization does not need password and corresponding features. |
||
104 | */ |
||
105 | public $passwordHistoryClass = false; |
||
106 | |||
107 | /** |
||
108 | * @var boolean Organization does not need source. |
||
109 | */ |
||
110 | public $sourceAttribute = false; |
||
111 | |||
112 | /** |
||
113 | * @var boolean Organization does not need auth key. |
||
114 | */ |
||
115 | public $authKeyAttribute = false; |
||
116 | |||
117 | /** |
||
118 | * @var boolean Organization does not need access token. |
||
119 | */ |
||
120 | public $accessTokenAttribute = false; |
||
121 | |||
122 | /** |
||
123 | * @var boolean Organization does not need login log. |
||
124 | */ |
||
125 | public $loginLogClass = false; |
||
126 | |||
127 | /** |
||
128 | * @var string The Organization Profile Class |
||
129 | */ |
||
130 | public $profileClass = Profile::class; |
||
131 | |||
132 | /** |
||
133 | * @var string The Member Class. |
||
134 | */ |
||
135 | public $memberClass = Member::class; |
||
136 | |||
137 | /** |
||
138 | * @var string The Subordinate Limit Class |
||
139 | */ |
||
140 | public $subordinateLimitClass = SubordinateLimit::class; |
||
141 | |||
142 | /** |
||
143 | * @var string The Member Limit Class |
||
144 | */ |
||
145 | public $memberLimitClass = MemberLimit::class; |
||
146 | |||
147 | /** |
||
148 | * @var string The Organization Search Class |
||
149 | */ |
||
150 | public $searchClass = OrganizationSearch::class; |
||
151 | |||
152 | /** |
||
153 | * @var string The Organization Setting Class |
||
154 | */ |
||
155 | public $organizationSettingClass = OrganizationSetting::class; |
||
156 | |||
157 | /** |
||
158 | * @var Member |
||
159 | */ |
||
160 | private $noInitMember; |
||
161 | |||
162 | /** |
||
163 | * @var SubordinateLimit |
||
164 | */ |
||
165 | private $noInitSubordinateLimit; |
||
166 | |||
167 | /** |
||
168 | * @var MemberLimit |
||
169 | */ |
||
170 | private $noInitMemberLimit; |
||
171 | |||
172 | /** |
||
173 | * @var OrganizationSetting |
||
174 | */ |
||
175 | private $noInitOrganizationSetting; |
||
176 | |||
177 | /** |
||
178 | * @var User the creator of current Organization or Department. |
||
179 | * This property is only available after registration. |
||
180 | * Please do not access it at other times. |
||
181 | * If you want to get creator model except registration, please |
||
182 | * access [[$creator]] magic-property instead. |
||
183 | */ |
||
184 | public $creatorModel; |
||
185 | |||
186 | /** |
||
187 | * @var array The configuration array of Organization Profile. |
||
188 | * This property is only available after registration. |
||
189 | * Please do not access it at other times. |
||
190 | * If you want to get profile model except registration, please |
||
191 | * access [[$profile]] magic-property instead. |
||
192 | */ |
||
193 | public $profileConfig; |
||
194 | |||
195 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
196 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
197 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
198 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
199 | |||
200 | public $cacheTagPrefix = 'tag_organization_'; |
||
201 | |||
202 | 51 | /** |
|
203 | * @return Member |
||
204 | 51 | */ |
|
205 | 51 | public function getNoInitMember() |
|
213 | |||
214 | 2 | /** |
|
215 | * @return SubordinateLimit |
||
216 | 2 | */ |
|
217 | 2 | public function getNoInitSubordinateLimit() |
|
225 | |||
226 | 1 | /** |
|
227 | * @return MemberLimit |
||
228 | 1 | */ |
|
229 | 1 | public function getNoInitMemberLimit() |
|
237 | |||
238 | 31 | /** |
|
239 | * @return null|OrganizationSetting |
||
240 | 31 | */ |
|
241 | 31 | public function getNoInitOrganizationSetting() |
|
252 | |||
253 | /** |
||
254 | * @return null|OrganizationSearch |
||
255 | */ |
||
256 | public function getSearchModel() |
||
264 | |||
265 | 52 | /** |
|
266 | * @inheritdoc |
||
267 | 52 | */ |
|
268 | 52 | public function init() |
|
285 | |||
286 | 1 | /** |
|
287 | * @inheritdoc |
||
288 | */ |
||
289 | 1 | public function attributeLabels() |
|
307 | |||
308 | 52 | /** |
|
309 | * @inheritdoc |
||
310 | 52 | */ |
|
311 | public static function tableName() |
||
315 | |||
316 | /** |
||
317 | * Find. |
||
318 | 52 | * Friendly to IDE. |
|
319 | * @return OrganizationQuery |
||
320 | 52 | */ |
|
321 | public static function find() |
||
325 | |||
326 | 51 | /** |
|
327 | * Get rules associated with type attribute. |
||
328 | 51 | * @return array |
|
329 | */ |
||
330 | protected function getTypeRules() |
||
338 | |||
339 | /** |
||
340 | * @inheritdoc |
||
341 | 50 | */ |
|
342 | public function rules() |
||
346 | |||
347 | /** |
||
348 | * Get Member Query. |
||
349 | * @return MemberQuery |
||
350 | */ |
||
351 | public function getMembers() |
||
357 | 6 | ||
358 | 6 | /** |
|
359 | 6 | * Get organization member users' query. |
|
360 | * @return BaseUserQuery |
||
361 | */ |
||
362 | public function getMemberUsers() |
||
371 | 2 | ||
372 | 2 | /** |
|
373 | * Get subordinate limit query. |
||
374 | * @return null|BaseBlameableQuery |
||
375 | */ |
||
376 | public function getSubordinateLimit() |
||
385 | 1 | ||
386 | 1 | /** |
|
387 | * Get member limit query. |
||
388 | * @return null|BaseBlameableQuery |
||
389 | */ |
||
390 | public function getMemberLimit() |
||
399 | 31 | ||
400 | 31 | /** |
|
401 | 31 | * @param string|null $item If you want to get all settings, please set it null. |
|
402 | * @return null |
||
403 | 31 | */ |
|
404 | public function getSettings($item = null) |
||
415 | |||
416 | 31 | /** |
|
417 | * @param $item |
||
418 | 31 | * @param $value |
|
419 | 31 | * @return bool|null Null if organization setting not enabled. |
|
420 | 31 | */ |
|
421 | public function setSetting($item, $value) |
||
436 | |||
437 | /** |
||
438 | * Get member with specified user. |
||
439 | * @param User|string|integer $user |
||
440 | * @return Member Null if `user` is not in this organization. |
||
441 | */ |
||
442 | public function getMember($user) |
||
446 | |||
447 | /** |
||
448 | * Add member to organization. |
||
449 | 50 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
|
450 | * If member is created, it will be re-assigned to this parameter. |
||
451 | 50 | * @see createMemberModel |
|
452 | * @see createMemberModelWithUser |
||
453 | * @return boolean |
||
454 | 50 | * @throws DisallowMemberJoinOtherException |
|
455 | 1 | * @throws ExcludeOtherMembersException |
|
456 | * @throws OnlyAcceptCurrentOrgMemberException |
||
457 | 50 | * @throws OnlyAcceptSuperiorOrgMemberException |
|
458 | 50 | */ |
|
459 | public function addMember(&$member) |
||
513 | |||
514 | /** |
||
515 | * Create member model, and set organization with this. |
||
516 | * @param Member $member If this parameter is not new record, it's organization |
||
517 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
518 | * model and create new `Member` model. |
||
519 | * @see createMemberModelWithUser |
||
520 | * @return Member |
||
521 | */ |
||
522 | public function createMemberModel($member) |
||
530 | 50 | ||
531 | 50 | /** |
|
532 | * Create member model with user, and set organization with this. |
||
533 | 50 | * @param User|string|integer $user |
|
534 | 50 | * @return Member |
|
535 | 50 | */ |
|
536 | public function createMemberModelWithUser($user) |
||
547 | |||
548 | /** |
||
549 | 4 | * Remove member. |
|
550 | 4 | * Note: the creator cannot be removed. |
|
551 | 4 | * @param Member|User $member |
|
552 | * @return boolean |
||
553 | 4 | */ |
|
554 | 4 | public function removeMember(&$member) |
|
571 | |||
572 | /** |
||
573 | * Remove administrator. |
||
574 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
575 | * @param boolean $keep Keep member after administrator being revoked. |
||
576 | * @return boolean |
||
577 | * @throws IntegrityException |
||
578 | */ |
||
579 | public function removeAdministrator(&$member, $keep = true) |
||
596 | 51 | ||
597 | /** |
||
598 | * |
||
599 | 51 | * @param Event $event |
|
600 | * @throws IntegrityException |
||
601 | * @return boolean |
||
602 | */ |
||
603 | public function onAddProfile($event) |
||
611 | |||
612 | /** |
||
613 | * |
||
614 | * @param Event $event |
||
615 | */ |
||
616 | 20 | public function onAssignCreator($event) |
|
620 | 20 | ||
621 | /** |
||
622 | 20 | * |
|
623 | 20 | * @param Event $event |
|
624 | * @return boolean |
||
625 | */ |
||
626 | public function onRevokeCreator($event) |
||
635 | 20 | ||
636 | /** |
||
637 | 20 | * |
|
638 | * @param Event $event |
||
639 | 1 | * @return boolean |
|
640 | */ |
||
641 | 20 | public function onRevokeAdministrators($event) |
|
653 | |||
654 | /** |
||
655 | * |
||
656 | * @param Event $event |
||
657 | 50 | */ |
|
658 | public function onRevokePermissions($event) |
||
662 | |||
663 | /** |
||
664 | * Check whether current instance is an organization. |
||
665 | * @return boolean |
||
666 | 50 | */ |
|
667 | public function isOrganization() |
||
671 | |||
672 | /** |
||
673 | * Check whether current instance if a department. |
||
674 | * @return boolean |
||
675 | */ |
||
676 | 50 | public function isDepartment() |
|
680 | |||
681 | /** |
||
682 | * Check whether the current organization has a member. |
||
683 | * @param User|string|integer $user User instance, GUID or ID. |
||
684 | * @return boolean |
||
685 | 24 | */ |
|
686 | public function hasMember($user) |
||
690 | |||
691 | /** |
||
692 | * Get member query which role is specified `Creator`. |
||
693 | * @return MemberQuery |
||
694 | 22 | */ |
|
695 | public function getMemberCreators() |
||
699 | |||
700 | /** |
||
701 | * Get member query which role is specified `Administrator`. |
||
702 | * @return MemberQuery |
||
703 | 4 | */ |
|
704 | public function getMemberAdministrators() |
||
708 | 4 | ||
709 | 4 | /** |
|
710 | 4 | * Get user query which role is specified `Creator`. |
|
711 | * @return BaseUserQuery |
||
712 | */ |
||
713 | public function getCreator() |
||
722 | 2 | ||
723 | 2 | /** |
|
724 | 2 | * Get user query which role is specified `Administrator`. |
|
725 | * @return BaseUserQuery |
||
726 | */ |
||
727 | public function getAdministrators() |
||
736 | 51 | ||
737 | 1 | /** |
|
738 | * |
||
739 | 50 | * @param User $user |
|
740 | 50 | * @return boolean |
|
741 | * @throws \Exception |
||
742 | 50 | * @throws IntegrityException |
|
743 | */ |
||
744 | protected function addCreator($user) |
||
768 | 17 | ||
769 | /** |
||
770 | 17 | * Add administrator. |
|
771 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
772 | * @return boolean |
||
773 | 17 | * @throws \Exception |
|
774 | 17 | * @throws IntegrityException |
|
775 | 17 | */ |
|
776 | 2 | public function addAdministrator($user) |
|
793 | |||
794 | /** |
||
795 | 2 | * Check whether the current organization has administrator. |
|
796 | * @param User|integer|string $user |
||
797 | * @return boolean |
||
798 | */ |
||
799 | public function hasAdministrator($user) |
||
807 | |||
808 | 19 | /** |
|
809 | * Check whether this organization has reached the upper limit of subordinates. |
||
810 | * @return boolean |
||
811 | */ |
||
812 | public function hasReachedSubordinateLimit() |
||
820 | |||
821 | 19 | /** |
|
822 | 19 | * Get the remaining places of subordinates. |
|
823 | * @return bool|int False if no limit |
||
824 | */ |
||
825 | 19 | public function getRemainingSubordinatePlaces() |
|
838 | |||
839 | 50 | /** |
|
840 | * Check whether this organization has reached the upper limit of members. |
||
841 | * @return boolean |
||
842 | */ |
||
843 | public function hasReachedMemberLimit() |
||
851 | |||
852 | 50 | /** |
|
853 | 50 | * Get the remaining places of members. |
|
854 | * @return bool|int False if no limit. |
||
855 | */ |
||
856 | 50 | public function getRemainingMemberPlaces() |
|
869 | 31 | ||
870 | 31 | const SETTING_ITEM_EXCLUDE_OTHER_MEMBERS = 'exclude_other_members'; |
|
871 | |||
872 | 31 | /** |
|
873 | * @return bool |
||
874 | */ |
||
875 | public function getIsExcludeOtherMembers() |
||
884 | |||
885 | /** |
||
886 | * @param bool $value |
||
887 | * @return bool |
||
888 | */ |
||
889 | 31 | public function setIsExcludeOtherMembers($value = true) |
|
893 | 31 | ||
894 | 31 | const SETTING_ITEM_DISALLOW_MEMBER_JOIN_OTHER = 'disallow_member_join_other'; |
|
895 | |||
896 | 31 | /** |
|
897 | * @return bool |
||
898 | */ |
||
899 | public function getIsDisallowMemberJoinOther() |
||
908 | |||
909 | /** |
||
910 | * @param bool $value |
||
911 | * @return bool |
||
912 | */ |
||
913 | 18 | public function setIsDisallowMemberJoinOther($value = true) |
|
917 | 18 | ||
918 | 18 | const SETTING_ITEM_ONLY_ACCEPT_CURRENT_ORG_MEMBER = 'only_accept_current_org_member'; |
|
919 | |||
920 | 18 | /** |
|
921 | * @return bool |
||
922 | */ |
||
923 | public function getIsOnlyAcceptCurrentOrgMember() |
||
932 | |||
933 | /** |
||
934 | * @param bool $value |
||
935 | * @return bool |
||
936 | */ |
||
937 | 18 | public function setIsOnlyAcceptCurrentOrgMember($value = true) |
|
941 | 18 | ||
942 | 18 | const SETTING_ITEM_ONLY_ACCEPT_SUPERIOR_ORG_MEMBER = 'only_accept_superior_org_member'; |
|
943 | |||
944 | 18 | /** |
|
945 | * @return bool |
||
946 | */ |
||
947 | public function getIsOnlyAcceptSuperiorOrgMember() |
||
959 | 31 | ||
960 | /** |
||
961 | 31 | * @param bool $value |
|
962 | 31 | * @return bool |
|
963 | */ |
||
964 | 18 | public function setIsOnlyAcceptSuperiorOrgMember($value = true) |
|
971 | |||
972 | const SETTING_ITEM_JOIN_PASSWORD = 'join_password'; |
||
973 | |||
974 | 2 | /** |
|
975 | * Get join password. |
||
976 | 2 | * @return mixed |
|
977 | 2 | */ |
|
978 | 1 | public function getJoinPassword() |
|
987 | 2 | ||
988 | /** |
||
989 | * Set join password. |
||
990 | * @param string $value |
||
991 | * @return bool|null |
||
992 | */ |
||
993 | public function setJoinPassword($value = '') |
||
997 | |||
998 | const SETTING_ITEM_JOIN_IP_ADDRESS = 'join_ip_address'; |
||
999 | |||
1000 | /** |
||
1001 | * Get Join IP address |
||
1002 | * @return mixed |
||
1003 | */ |
||
1004 | public function getJoinIpAddress() |
||
1013 | |||
1014 | /** |
||
1015 | * Set join IP address. |
||
1016 | * @param $value |
||
1017 | * @return bool|null |
||
1018 | */ |
||
1019 | public function setJoinIpAddress($value = '') |
||
1023 | |||
1024 | const SETTING_ITEM_JOIN_ENTRANCE_URL = 'join_entrance_url'; |
||
1025 | |||
1026 | /** |
||
1027 | * Get join entrance URL. |
||
1028 | * @return string |
||
1029 | */ |
||
1030 | public function getJoinEntranceUrl() |
||
1039 | |||
1040 | /** |
||
1041 | * Set join entrance URL. |
||
1042 | * @param string $value |
||
1043 | * @return bool|null |
||
1044 | */ |
||
1045 | public function setJoinEntranceUrl($value = '') |
||
1049 | |||
1050 | /** |
||
1051 | * @return $this|null|static |
||
1052 | */ |
||
1053 | public function getTopOrganization() |
||
1061 | |||
1062 | /** |
||
1063 | * Check whether the subordinates have the [[$user]] |
||
1064 | * Note, this operation may consume the quantity of database selection. |
||
1065 | * @param User $user |
||
1066 | * @return bool |
||
1067 | */ |
||
1068 | public function hasMemberInSubordinates($user) |
||
1083 | } |
||
1084 |