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 | * @return Member |
||
112 | */ |
||
113 | 39 | protected function getNoInitMember() |
|
121 | |||
122 | /** |
||
123 | * @return SubordinateLimit |
||
124 | */ |
||
125 | 2 | protected function getNoInitSubordinateLimit() |
|
133 | |||
134 | /** |
||
135 | * @return MemberLimit |
||
136 | */ |
||
137 | 1 | protected function getNoInitMemberLimit() |
|
145 | |||
146 | /** |
||
147 | * @return null|OrganizationSearch |
||
148 | */ |
||
149 | public function getSearchModel() |
||
157 | |||
158 | 41 | public function init() |
|
175 | |||
176 | /** |
||
177 | * @inheritdoc |
||
178 | */ |
||
179 | 1 | public function attributeLabels() |
|
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 41 | public static function tableName() |
|
201 | |||
202 | /** |
||
203 | * Find. |
||
204 | * Friendly to IDE. |
||
205 | * @return OrganizationQuery |
||
206 | */ |
||
207 | 41 | public static function find() |
|
211 | |||
212 | 40 | protected function getTypeRules() |
|
220 | |||
221 | 40 | public function rules() |
|
225 | |||
226 | /** |
||
227 | * Get Member Query. |
||
228 | * @return MemberQuery |
||
229 | */ |
||
230 | 39 | public function getMembers() |
|
234 | |||
235 | /** |
||
236 | * Get organization member users' query. |
||
237 | * @return BaseUserQuery |
||
238 | */ |
||
239 | 4 | public function getMemberUsers() |
|
246 | |||
247 | /** |
||
248 | * Get subordinate limit query. |
||
249 | * @return null|BaseBlameableQuery |
||
250 | */ |
||
251 | 2 | public function getSubordinateLimit() |
|
258 | |||
259 | /** |
||
260 | * Get member limit query. |
||
261 | * @return null|BaseBlameableQuery |
||
262 | */ |
||
263 | 1 | public function getMemberLimit() |
|
270 | |||
271 | /** |
||
272 | * Get member with specified user. |
||
273 | * @param User|string|integer $user |
||
274 | * @return Member Null if `user` is not in this organization. |
||
275 | */ |
||
276 | 39 | public function getMember($user) |
|
280 | |||
281 | /** |
||
282 | * Add member to organization. |
||
283 | * @param Member|User|string|integer $member Member or User model, or User ID or GUID. |
||
284 | * If member is created, it will be re-assigned to this parameter. |
||
285 | * @see createMemberModel |
||
286 | * @see createMemberModelWithUser |
||
287 | * @return boolean |
||
288 | */ |
||
289 | 39 | public function addMember(&$member) |
|
313 | |||
314 | /** |
||
315 | * Create member model, and set organization with this. |
||
316 | * @param Member $member If this parameter is not new record, it's organization |
||
317 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
318 | * model and create new `Member` model. |
||
319 | * @see createMemberModelWithUser |
||
320 | * @return Member |
||
321 | */ |
||
322 | public function createMemberModel($member) |
||
330 | |||
331 | /** |
||
332 | * Create member model with user, and set organization with this. |
||
333 | * @param User|string|integer $user |
||
334 | * @return Member |
||
335 | */ |
||
336 | 39 | public function createMemberModelWithUser($user) |
|
347 | |||
348 | /** |
||
349 | * Remove member. |
||
350 | * Note: the creator cannot be removed. |
||
351 | * @param Member|User $member |
||
352 | * @return boolean |
||
353 | */ |
||
354 | 1 | public function removeMember(&$member) |
|
368 | |||
369 | /** |
||
370 | * Remove administrator. |
||
371 | * @param Member|User $member |
||
372 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
373 | * @return boolean |
||
374 | * @throws IntegrityException |
||
375 | */ |
||
376 | public function removeAdministrator(&$member, $keepMember = true) |
||
393 | |||
394 | /** |
||
395 | * |
||
396 | * @param Event $event |
||
397 | */ |
||
398 | 40 | public function onAddProfile($event) |
|
406 | |||
407 | /** |
||
408 | * |
||
409 | * @param Event $event |
||
410 | */ |
||
411 | 40 | public function onAssignCreator($event) |
|
415 | |||
416 | /** |
||
417 | * |
||
418 | * @param Event $event |
||
419 | * @return boolean |
||
420 | */ |
||
421 | 20 | public function onRevokeCreator($event) |
|
430 | |||
431 | /** |
||
432 | * |
||
433 | * @param Event $event |
||
434 | * @return boolean |
||
435 | */ |
||
436 | 20 | public function onRevokeAdministrators($event) |
|
448 | |||
449 | /** |
||
450 | * |
||
451 | * @param Event $event |
||
452 | */ |
||
453 | 20 | public function onRevokePermissions($event) |
|
457 | |||
458 | /** |
||
459 | * Check whether current instance is an organization. |
||
460 | * @return boolean |
||
461 | */ |
||
462 | 2 | public function isOrganization() |
|
466 | |||
467 | /** |
||
468 | * Check whether current instance if a department. |
||
469 | * @return boolean |
||
470 | */ |
||
471 | 2 | public function isDepartment() |
|
475 | |||
476 | /** |
||
477 | * Check whether the current organization has a member. |
||
478 | * @param User|string|integer $user User instance, GUID or ID. |
||
479 | * @return boolean |
||
480 | */ |
||
481 | 39 | public function hasMember($user) |
|
485 | |||
486 | /** |
||
487 | * Get member query which role is specified `Creator`. |
||
488 | * @return MemberQuery |
||
489 | */ |
||
490 | 21 | public function getMemberCreators() |
|
494 | |||
495 | /** |
||
496 | * Get member query which role is specified `Administrator`. |
||
497 | * @return MemberQuery |
||
498 | */ |
||
499 | 21 | public function getMemberAdministrators() |
|
503 | |||
504 | /** |
||
505 | * Get user query which role is specified `Creator`. |
||
506 | * @return BaseUserQuery |
||
507 | */ |
||
508 | 1 | public function getCreator() |
|
515 | |||
516 | /** |
||
517 | * Get user query which role is specified `Administrator`. |
||
518 | * @return BaseUserQuery |
||
519 | */ |
||
520 | 1 | public function getAdministrators() |
|
527 | |||
528 | /** |
||
529 | * |
||
530 | * @param User $user |
||
531 | * @return boolean |
||
532 | * @throws \Exception |
||
533 | * @throws IntegrityException |
||
534 | */ |
||
535 | 40 | protected function addCreator($user) |
|
559 | |||
560 | /** |
||
561 | * |
||
562 | * @param User $user |
||
563 | * @return boolean |
||
564 | * @throws \Exception |
||
565 | * @throws IntegrityException |
||
566 | */ |
||
567 | 7 | public function addAdministrator($user) |
|
584 | |||
585 | /** |
||
586 | * |
||
587 | * @param type $user |
||
588 | * @return boolean |
||
589 | */ |
||
590 | 2 | public function hasAdministrator($user) |
|
598 | |||
599 | /** |
||
600 | * Check whether this organization has reached the upper limit of subordinates. |
||
601 | * @return boolean |
||
602 | */ |
||
603 | 9 | public function hasReachedSubordinateLimit() |
|
616 | |||
617 | /** |
||
618 | * Check whether this organization has reached the upper limit of members. |
||
619 | * @return boolean |
||
620 | */ |
||
621 | 39 | public function hasReachedMemberLimit() |
|
634 | } |
||
635 |