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 Member or User model, or User ID or GUID. |
||
269 | * If member is created, it will be re-assigned to this parameter. |
||
270 | * @see createMemberModel |
||
271 | * @see createMemberModelWithUser |
||
272 | * @return boolean |
||
273 | */ |
||
274 | 36 | public function addMember(&$member) |
|
298 | |||
299 | /** |
||
300 | * Create member model, and set organization with this. |
||
301 | * @param Member $member If this parameter is not new record, it's organization |
||
302 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
303 | * model and create new `Member` model. |
||
304 | * @see createMemberModelWithUser |
||
305 | * @return Member |
||
306 | */ |
||
307 | public function createMemberModel($member) |
||
315 | |||
316 | /** |
||
317 | * Create member model with user, and set organization with this. |
||
318 | * @param User|string|integer $user |
||
319 | * @return Member |
||
320 | */ |
||
321 | 36 | public function createMemberModelWithUser($user) |
|
332 | |||
333 | /** |
||
334 | * Remove member. |
||
335 | * Note: the creator cannot be removed. |
||
336 | * @param Member|User $member |
||
337 | * @return boolean |
||
338 | */ |
||
339 | 1 | public function removeMember(&$member) |
|
353 | |||
354 | /** |
||
355 | * Remove administrator. |
||
356 | * @param Member|User $member |
||
357 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
358 | * @return boolean |
||
359 | * @throws IntegrityException |
||
360 | */ |
||
361 | public function removeAdministrator(&$member, $keepMember = true) |
||
378 | |||
379 | /** |
||
380 | * |
||
381 | * @param Event $event |
||
382 | */ |
||
383 | 37 | public function onAddProfile($event) |
|
391 | |||
392 | /** |
||
393 | * |
||
394 | * @param Event $event |
||
395 | */ |
||
396 | 37 | public function onAssignCreator($event) |
|
400 | |||
401 | /** |
||
402 | * |
||
403 | * @param Event $event |
||
404 | * @return boolean |
||
405 | */ |
||
406 | 17 | public function onRevokeCreator($event) |
|
415 | |||
416 | /** |
||
417 | * |
||
418 | * @param Event $event |
||
419 | * @return boolean |
||
420 | */ |
||
421 | 17 | public function onRevokeAdministrators($event) |
|
433 | |||
434 | /** |
||
435 | * |
||
436 | * @param Event $event |
||
437 | */ |
||
438 | 17 | public function onRevokePermissions($event) |
|
442 | |||
443 | /** |
||
444 | * Check whether current instance is an organization. |
||
445 | * @return boolean |
||
446 | */ |
||
447 | 2 | public function isOrganization() |
|
451 | |||
452 | /** |
||
453 | * Check whether current instance if a department. |
||
454 | * @return boolean |
||
455 | */ |
||
456 | 2 | public function isDepartment() |
|
460 | |||
461 | /** |
||
462 | * Check whether the current organization has a member. |
||
463 | * @param User|string|integer $user User instance, GUID or ID. |
||
464 | * @return boolean |
||
465 | */ |
||
466 | 36 | public function hasMember($user) |
|
470 | |||
471 | /** |
||
472 | * Get member query which role is specified `Creator`. |
||
473 | * @return MemberQuery |
||
474 | */ |
||
475 | 18 | public function getMemberCreators() |
|
479 | |||
480 | /** |
||
481 | * Get member query which role is specified `Administrator`. |
||
482 | * @return MemberQuery |
||
483 | */ |
||
484 | 18 | public function getMemberAdministrators() |
|
488 | |||
489 | /** |
||
490 | * Get user query which role is specified `Creator`. |
||
491 | * @return BaseUserQuery |
||
492 | */ |
||
493 | 1 | public function getCreator() |
|
500 | |||
501 | /** |
||
502 | * Get user query which role is specified `Administrator`. |
||
503 | * @return BaseUserQuery |
||
504 | */ |
||
505 | 1 | public function getAdministrators() |
|
512 | |||
513 | /** |
||
514 | * |
||
515 | * @param User $user |
||
516 | * @return boolean |
||
517 | * @throws \Exception |
||
518 | * @throws IntegrityException |
||
519 | */ |
||
520 | 37 | protected function addCreator($user) |
|
544 | |||
545 | /** |
||
546 | * |
||
547 | * @param User $user |
||
548 | * @return boolean |
||
549 | * @throws \Exception |
||
550 | * @throws IntegrityException |
||
551 | */ |
||
552 | 7 | public function addAdministrator($user) |
|
569 | |||
570 | /** |
||
571 | * |
||
572 | * @param type $user |
||
573 | * @return boolean |
||
574 | */ |
||
575 | 2 | public function hasAdministrator($user) |
|
583 | |||
584 | /** |
||
585 | * Check whether this organization has reached the upper limit of subordinates. |
||
586 | * @return boolean |
||
587 | */ |
||
588 | 7 | public function hasReachedSubordinateLimit() |
|
601 | |||
602 | /** |
||
603 | * Check whether this organization has reached the upper limit of members. |
||
604 | * @return boolean |
||
605 | */ |
||
606 | 36 | public function hasReachedMemberLimit() |
|
619 | } |
||
620 |