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 |
||
43 | class Organization extends User |
||
44 | { |
||
45 | use SelfBlameableTrait; |
||
46 | |||
47 | const TYPE_ORGANIZATION = 1; |
||
48 | const TYPE_DEPARTMENT = 2; |
||
49 | |||
50 | /** |
||
51 | * @var boolean Organization does not need password and corresponding features. |
||
52 | */ |
||
53 | public $passwordHashAttribute = false; |
||
54 | |||
55 | /** |
||
56 | * @var boolean Organization does not need password and corresponding features. |
||
57 | */ |
||
58 | public $passwordResetTokenAttribute = false; |
||
59 | |||
60 | /** |
||
61 | * @var boolean Organization does not need password and corresponding features. |
||
62 | */ |
||
63 | public $passwordHistoryClass = false; |
||
64 | |||
65 | /** |
||
66 | * @var boolean Organization does not need source. |
||
67 | */ |
||
68 | public $sourceAttribute = false; |
||
69 | |||
70 | /** |
||
71 | * @var boolean Organization does not need auth key. |
||
72 | */ |
||
73 | public $authKeyAttribute = false; |
||
74 | |||
75 | /** |
||
76 | * @var boolean Organization does not need access token. |
||
77 | */ |
||
78 | public $accessTokenAttribute = false; |
||
79 | |||
80 | /** |
||
81 | * |
||
82 | * @var boolean Organization does not need login log. |
||
83 | */ |
||
84 | public $loginLogClass = false; |
||
85 | |||
86 | public $profileClass = Profile::class; |
||
87 | |||
88 | public $memberClass = Member::class; |
||
89 | private $noInitMember; |
||
90 | public $creatorModel; |
||
91 | public $profileConfig; |
||
92 | /** |
||
93 | * @return Member |
||
94 | */ |
||
95 | 36 | protected function getNoInitMember() |
|
96 | { |
||
97 | 36 | if (!$this->noInitMember) { |
|
98 | 36 | $class = $this->memberClass; |
|
99 | 36 | $this->noInitMember = $class::buildNoInitModel(); |
|
100 | } |
||
101 | 36 | return $this->noInitMember; |
|
102 | } |
||
103 | |||
104 | 38 | public function init() |
|
105 | { |
||
106 | 38 | $this->parentAttribute = 'parent_guid'; |
|
107 | 38 | if (class_exists($this->memberClass)) { |
|
108 | 38 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
|
109 | } |
||
110 | 38 | if ($this->skipInit) { |
|
111 | 38 | return; |
|
112 | } |
||
113 | 38 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
114 | 38 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
115 | 38 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeCreator']); |
|
116 | 38 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeAdministrators']); |
|
117 | 38 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokePermissions']); |
|
118 | 38 | $this->initSelfBlameableEvents(); |
|
119 | 38 | parent::init(); |
|
120 | 38 | } |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 1 | public function attributeLabels() |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 38 | public static function tableName() |
|
147 | |||
148 | 37 | protected function getTypeRules() |
|
156 | |||
157 | 37 | public function rules() |
|
161 | |||
162 | /** |
||
163 | * Get Member Query. |
||
164 | * @return MemberQuery |
||
165 | */ |
||
166 | 36 | public function getMembers() |
|
170 | |||
171 | /** |
||
172 | * Get organization member users' query. |
||
173 | * @return BaseUserQuery |
||
174 | */ |
||
175 | 4 | public function getMemberUsers() |
|
182 | |||
183 | /** |
||
184 | * Get member with specified user. |
||
185 | * @param User|string|integer $user |
||
186 | * @return Member Null if `user` is not in this organization. |
||
187 | */ |
||
188 | 36 | public function getMember($user) |
|
192 | |||
193 | /** |
||
194 | * Add member to organization. |
||
195 | * @param Member|User|string|integer $member |
||
196 | * @see createMemberModel |
||
197 | * @see createMemberModelWithUser |
||
198 | * @return boolean |
||
199 | */ |
||
200 | 36 | public function addMember(&$member) |
|
221 | |||
222 | /** |
||
223 | * Create member model, and set organization with this. |
||
224 | * @param Member $member If this parameter is not new record, it's organization |
||
225 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
226 | * model and create new `Member` model. |
||
227 | * @see createMemberModelWithUser |
||
228 | * @return Member |
||
229 | */ |
||
230 | public function createMemberModel($member) |
||
238 | |||
239 | /** |
||
240 | * Create member model with user, and set organization with this. |
||
241 | * @param User|string|integer $user |
||
242 | * @return Member |
||
243 | */ |
||
244 | 36 | public function createMemberModelWithUser($user) |
|
255 | |||
256 | /** |
||
257 | * Remove member. |
||
258 | * @param Member|User $member |
||
259 | * @return boolean |
||
260 | */ |
||
261 | 1 | public function removeMember(&$member) |
|
272 | |||
273 | /** |
||
274 | * Remove administrator. |
||
275 | * @param Member|User $member |
||
276 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
277 | * @return boolean |
||
278 | * @throws IntegrityException |
||
279 | */ |
||
280 | public function removeAdministrator(&$member, $keepMember = true) |
||
297 | |||
298 | /** |
||
299 | * |
||
300 | * @param Event $event |
||
301 | */ |
||
302 | 37 | public function onAddProfile($event) |
|
310 | |||
311 | /** |
||
312 | * |
||
313 | * @param Event $event |
||
314 | */ |
||
315 | 37 | public function onAssignCreator($event) |
|
319 | |||
320 | /** |
||
321 | * |
||
322 | * @param Event $event |
||
323 | */ |
||
324 | 17 | public function onRevokeCreator($event) |
|
333 | |||
334 | /** |
||
335 | * |
||
336 | * @param Event $event |
||
337 | */ |
||
338 | 17 | public function onRevokeAdministrators($event) |
|
349 | |||
350 | /** |
||
351 | * |
||
352 | * @param Event $event |
||
353 | */ |
||
354 | 17 | public function onRevokePermissions($event) |
|
358 | |||
359 | /** |
||
360 | * Check whether current instance is an organization. |
||
361 | * @return boolean |
||
362 | */ |
||
363 | 2 | public function isOrganization() |
|
367 | |||
368 | /** |
||
369 | * Check whether current instance if a department. |
||
370 | * @return boolean |
||
371 | */ |
||
372 | 2 | public function isDepartment() |
|
376 | |||
377 | /** |
||
378 | * Check whether the current organization has a member. |
||
379 | * @param User|string|integer $user User instance, GUID or ID. |
||
380 | * @return boolean |
||
381 | */ |
||
382 | 36 | public function hasMember($user) |
|
386 | |||
387 | /** |
||
388 | * Get member query which role is specified `Creator`. |
||
389 | * @return MemberQuery |
||
390 | */ |
||
391 | 18 | public function getMemberCreators() |
|
395 | |||
396 | /** |
||
397 | * Get member query which role is specified `Administrator`. |
||
398 | * @return MemberQuery |
||
399 | */ |
||
400 | 18 | public function getMemberAdministrators() |
|
404 | |||
405 | /** |
||
406 | * Get user query which role is specified `Creator`. |
||
407 | * @return BaseUserQuery |
||
408 | */ |
||
409 | 1 | public function getCreator() |
|
416 | |||
417 | /** |
||
418 | * Get user query which role is specified `Administrator`. |
||
419 | * @return BaseUserQuery |
||
420 | */ |
||
421 | 1 | public function getAdministrators() |
|
428 | |||
429 | /** |
||
430 | * |
||
431 | * @param User $user |
||
432 | * @return boolean |
||
433 | * @throws \Exception |
||
434 | * @throws IntegrityException |
||
435 | */ |
||
436 | 37 | protected function addCreator($user) |
|
460 | |||
461 | /** |
||
462 | * |
||
463 | * @param User $user |
||
464 | * @return boolean |
||
465 | * @throws \Exception |
||
466 | * @throws IntegrityException |
||
467 | */ |
||
468 | 7 | public function addAdministrator($user) |
|
485 | |||
486 | /** |
||
487 | * |
||
488 | * @param type $user |
||
489 | * @return boolean |
||
490 | */ |
||
491 | 2 | public function hasAdministrator($user) |
|
499 | } |
||
500 |