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 | public $searchClass = OrganizationSearch::class; |
||
96 | /** |
||
97 | * @var Member |
||
98 | */ |
||
99 | private $noInitMember; |
||
100 | /** |
||
101 | * @var SubordinateLimit |
||
102 | */ |
||
103 | private $noInitSubordinateLimit; |
||
104 | /** |
||
105 | * @var MemberLimit |
||
106 | */ |
||
107 | private $noInitMemberLimit; |
||
108 | public $creatorModel; |
||
109 | public $profileConfig; |
||
110 | |||
111 | const EVENT_BEFORE_ADD_MEMBER = 'eventBeforeAddMember'; |
||
112 | const EVENT_AFTER_ADD_MEMBER = 'eventAfterAddMember'; |
||
113 | const EVENT_BEFORE_REMOVE_MEMBER = 'eventBeforeRemoveMember'; |
||
114 | const EVENT_AFTER_REMOVE_MEMBER = 'eventAfterRemoveMember'; |
||
115 | |||
116 | /** |
||
117 | * @return Member |
||
118 | */ |
||
119 | 42 | public function getNoInitMember() |
|
127 | |||
128 | /** |
||
129 | * @return SubordinateLimit |
||
130 | */ |
||
131 | 2 | public function getNoInitSubordinateLimit() |
|
139 | |||
140 | /** |
||
141 | * @return MemberLimit |
||
142 | */ |
||
143 | 1 | public function getNoInitMemberLimit() |
|
151 | |||
152 | /** |
||
153 | * @return null|OrganizationSearch |
||
154 | */ |
||
155 | public function getSearchModel() |
||
163 | |||
164 | 43 | public function init() |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | 1 | public function attributeLabels() |
|
199 | |||
200 | /** |
||
201 | * @inheritdoc |
||
202 | */ |
||
203 | 43 | public static function tableName() |
|
207 | |||
208 | /** |
||
209 | * Find. |
||
210 | * Friendly to IDE. |
||
211 | * @return OrganizationQuery |
||
212 | */ |
||
213 | 43 | public static function find() |
|
217 | |||
218 | 42 | protected function getTypeRules() |
|
226 | |||
227 | 42 | public function rules() |
|
231 | |||
232 | /** |
||
233 | * Get Member Query. |
||
234 | * @return MemberQuery |
||
235 | */ |
||
236 | 41 | public function getMembers() |
|
240 | |||
241 | /** |
||
242 | * Get organization member users' query. |
||
243 | * @return BaseUserQuery |
||
244 | */ |
||
245 | 4 | public function getMemberUsers() |
|
252 | |||
253 | /** |
||
254 | * Get subordinate limit query. |
||
255 | * @return null|BaseBlameableQuery |
||
256 | */ |
||
257 | 2 | public function getSubordinateLimit() |
|
264 | |||
265 | /** |
||
266 | * Get member limit query. |
||
267 | * @return null|BaseBlameableQuery |
||
268 | */ |
||
269 | 1 | public function getMemberLimit() |
|
276 | |||
277 | /** |
||
278 | * Get member with specified user. |
||
279 | * @param User|string|integer $user |
||
280 | * @return Member Null if `user` is not in this organization. |
||
281 | */ |
||
282 | 41 | public function getMember($user) |
|
286 | |||
287 | /** |
||
288 | * Add member to organization. |
||
289 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
290 | * If member is created, it will be re-assigned to this parameter. |
||
291 | * @see createMemberModel |
||
292 | * @see createMemberModelWithUser |
||
293 | * @return boolean |
||
294 | */ |
||
295 | 41 | public function addMember(&$member) |
|
322 | |||
323 | /** |
||
324 | * Create member model, and set organization with this. |
||
325 | * @param Member $member If this parameter is not new record, it's organization |
||
326 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
327 | * model and create new `Member` model. |
||
328 | * @see createMemberModelWithUser |
||
329 | * @return Member |
||
330 | */ |
||
331 | public function createMemberModel($member) |
||
339 | |||
340 | /** |
||
341 | * Create member model with user, and set organization with this. |
||
342 | * @param User|string|integer $user |
||
343 | * @return Member |
||
344 | */ |
||
345 | 41 | public function createMemberModelWithUser($user) |
|
356 | |||
357 | /** |
||
358 | * Remove member. |
||
359 | * Note: the creator cannot be removed. |
||
360 | * @param Member|User $member |
||
361 | * @return boolean |
||
362 | */ |
||
363 | 1 | public function removeMember(&$member) |
|
380 | |||
381 | /** |
||
382 | * Remove administrator. |
||
383 | * @param Member|User|integer|string $member Member instance, or User instance or its GUID or ID. |
||
384 | * @param boolean $keep Keep member after administrator being revoked. |
||
385 | * @return boolean |
||
386 | * @throws IntegrityException |
||
387 | */ |
||
388 | public function removeAdministrator(&$member, $keep = true) |
||
405 | |||
406 | /** |
||
407 | * |
||
408 | * @param Event $event |
||
409 | * @throws IntegrityException |
||
410 | * @return boolean |
||
411 | */ |
||
412 | 42 | public function onAddProfile($event) |
|
420 | |||
421 | /** |
||
422 | * |
||
423 | * @param Event $event |
||
424 | */ |
||
425 | 42 | public function onAssignCreator($event) |
|
429 | |||
430 | /** |
||
431 | * |
||
432 | * @param Event $event |
||
433 | * @return boolean |
||
434 | */ |
||
435 | 20 | public function onRevokeCreator($event) |
|
444 | |||
445 | /** |
||
446 | * |
||
447 | * @param Event $event |
||
448 | * @return boolean |
||
449 | */ |
||
450 | 20 | public function onRevokeAdministrators($event) |
|
462 | |||
463 | /** |
||
464 | * |
||
465 | * @param Event $event |
||
466 | */ |
||
467 | 20 | public function onRevokePermissions($event) |
|
471 | |||
472 | /** |
||
473 | * Check whether current instance is an organization. |
||
474 | * @return boolean |
||
475 | */ |
||
476 | 2 | public function isOrganization() |
|
480 | |||
481 | /** |
||
482 | * Check whether current instance if a department. |
||
483 | * @return boolean |
||
484 | */ |
||
485 | 2 | public function isDepartment() |
|
489 | |||
490 | /** |
||
491 | * Check whether the current organization has a member. |
||
492 | * @param User|string|integer $user User instance, GUID or ID. |
||
493 | * @return boolean |
||
494 | */ |
||
495 | 41 | public function hasMember($user) |
|
499 | |||
500 | /** |
||
501 | * Get member query which role is specified `Creator`. |
||
502 | * @return MemberQuery |
||
503 | */ |
||
504 | 23 | public function getMemberCreators() |
|
508 | |||
509 | /** |
||
510 | * Get member query which role is specified `Administrator`. |
||
511 | * @return MemberQuery |
||
512 | */ |
||
513 | 22 | public function getMemberAdministrators() |
|
517 | |||
518 | /** |
||
519 | * Get user query which role is specified `Creator`. |
||
520 | * @return BaseUserQuery |
||
521 | */ |
||
522 | 3 | public function getCreator() |
|
529 | |||
530 | /** |
||
531 | * Get user query which role is specified `Administrator`. |
||
532 | * @return BaseUserQuery |
||
533 | */ |
||
534 | 2 | public function getAdministrators() |
|
541 | |||
542 | /** |
||
543 | * |
||
544 | * @param User $user |
||
545 | * @return boolean |
||
546 | * @throws \Exception |
||
547 | * @throws IntegrityException |
||
548 | */ |
||
549 | 42 | protected function addCreator($user) |
|
573 | |||
574 | /** |
||
575 | * Add administrator. |
||
576 | * @param User|integer|string $user User instance, or its GUID or ID. |
||
577 | * @return boolean |
||
578 | * @throws \Exception |
||
579 | * @throws IntegrityException |
||
580 | */ |
||
581 | 8 | public function addAdministrator($user) |
|
598 | |||
599 | /** |
||
600 | * Check whether the current organization has administrator. |
||
601 | * @param User|integer|string $user |
||
602 | * @return boolean |
||
603 | */ |
||
604 | 2 | public function hasAdministrator($user) |
|
612 | |||
613 | /** |
||
614 | * Check whether this organization has reached the upper limit of subordinates. |
||
615 | * @return boolean |
||
616 | */ |
||
617 | 9 | public function hasReachedSubordinateLimit() |
|
630 | |||
631 | /** |
||
632 | * Check whether this organization has reached the upper limit of members. |
||
633 | * @return boolean |
||
634 | */ |
||
635 | 41 | public function hasReachedMemberLimit() |
|
648 | } |
||
649 |