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 |
||
41 | class Organization extends User |
||
42 | { |
||
43 | use SelfBlameableTrait; |
||
44 | |||
45 | const TYPE_ORGANIZATION = 1; |
||
46 | const TYPE_DEPARTMENT = 2; |
||
47 | |||
48 | /** |
||
49 | * @var boolean Organization does not need password and corresponding features. |
||
50 | */ |
||
51 | public $passwordHashAttribute = false; |
||
52 | |||
53 | /** |
||
54 | * @var boolean Organization does not need password and corresponding features. |
||
55 | */ |
||
56 | public $passwordResetTokenAttribute = false; |
||
57 | |||
58 | /** |
||
59 | * @var boolean Organization does not need password and corresponding features. |
||
60 | */ |
||
61 | public $passwordHistoryClass = false; |
||
62 | |||
63 | /** |
||
64 | * @var boolean Organization does not need source. |
||
65 | */ |
||
66 | public $sourceAttribute = false; |
||
67 | |||
68 | /** |
||
69 | * @var boolean Organization does not need auth key. |
||
70 | */ |
||
71 | public $authKeyAttribute = false; |
||
72 | |||
73 | /** |
||
74 | * @var boolean Organization does not need access token. |
||
75 | */ |
||
76 | public $accessTokenAttribute = false; |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @var boolean Organization does not need login log. |
||
81 | */ |
||
82 | public $loginLogClass = false; |
||
83 | |||
84 | public $profileClass = Profile::class; |
||
85 | |||
86 | public $memberClass = Member::class; |
||
87 | private $noInitMember; |
||
88 | public $creatorModel; |
||
89 | public $profileConfig; |
||
90 | /** |
||
91 | * @return Member |
||
92 | */ |
||
93 | protected function getNoInitMember() |
||
94 | { |
||
95 | if (!$this->noInitMember) { |
||
96 | 24 | $class = $this->memberClass; |
|
97 | $this->noInitMember = $class::buildNoInitModel(); |
||
98 | 24 | } |
|
99 | 24 | return $this->noInitMember; |
|
100 | 24 | } |
|
101 | |||
102 | 24 | public function init() |
|
103 | { |
||
104 | $this->parentAttribute = 'parent_guid'; |
||
105 | 34 | if (class_exists($this->memberClass)) { |
|
106 | $this->addSubsidiaryClass('Member', ['class' => $this->memberClass]); |
||
107 | 34 | } |
|
108 | 34 | if ($this->skipInit) { |
|
109 | 34 | return; |
|
110 | } |
||
111 | 34 | $this->on(static::$eventAfterRegister, [$this, 'onAddProfile'], $this->profileConfig); |
|
112 | 34 | $this->on(static::$eventAfterRegister, [$this, 'onAssignCreator'], $this->creatorModel); |
|
113 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeCreator']); |
||
114 | 34 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokeAdministrators']); |
|
115 | 34 | $this->on(static::$eventBeforeDeregister, [$this, 'onRevokePermissions']); |
|
116 | 34 | $this->initSelfBlameableEvents(); |
|
117 | 34 | parent::init(); |
|
118 | 34 | } |
|
119 | 34 | ||
120 | 34 | /** |
|
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function attributeLabels() |
||
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | public static function tableName() |
||
142 | { |
||
143 | 34 | return '{{%organization}}'; |
|
144 | } |
||
145 | 34 | ||
146 | protected function getTypeRules() |
||
154 | |||
155 | public function rules() |
||
156 | { |
||
157 | 33 | return array_merge(parent::rules(), $this->getTypeRules(), $this->getSelfBlameableRules()); |
|
159 | 33 | ||
160 | /** |
||
161 | * Get Member Query. |
||
162 | * @return MemberQuery |
||
163 | */ |
||
164 | public function getMembers() |
||
168 | 24 | ||
169 | /** |
||
170 | * Get organization member users' query. |
||
171 | * @return BaseUserQuery |
||
172 | */ |
||
173 | public function getMemberUsers() |
||
180 | 3 | ||
181 | /** |
||
182 | * Get member with specified user. |
||
183 | * @param User|string|integer $user |
||
184 | * @return Member Null if `user` is not in this organization. |
||
185 | */ |
||
186 | public function getMember($user) |
||
190 | 19 | ||
191 | /** |
||
192 | * Add member to organization. |
||
193 | * @param Member|User|string|integer $member |
||
194 | * @see createMemberModel |
||
195 | * @see createMemberModelWithUser |
||
196 | * @return boolean |
||
197 | */ |
||
198 | public function addMember(&$member) |
||
213 | 32 | ||
214 | /** |
||
215 | * Create member model, and set organization with this. |
||
216 | * @param Member $member If this parameter is not new record, it's organization |
||
217 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
218 | * model and create new `Member` model. |
||
219 | * @see createMemberModelWithUser |
||
220 | * @return Member |
||
221 | */ |
||
222 | public function createMemberModel($member) |
||
230 | |||
231 | /** |
||
232 | * Create member model with user, and set organization with this. |
||
233 | * @param User|string|integer $user |
||
234 | * @return Member |
||
235 | */ |
||
236 | public function createMemberModelWithUser($user) |
||
248 | 32 | ||
249 | /** |
||
250 | * Remove member. |
||
251 | * @param Member|User $member |
||
252 | * @return boolean |
||
253 | */ |
||
254 | public function removeMember(&$member) |
||
265 | 1 | ||
266 | /** |
||
267 | * Remove administrator. |
||
268 | * @param Member|User $member |
||
269 | * @param boolean $keepMember Keep member after administrator being revoked. |
||
270 | * @return boolean |
||
271 | * @throws IntegrityException |
||
272 | 33 | */ |
|
273 | public function removeAdministrator(&$member, $keepMember = true) |
||
290 | |||
291 | /** |
||
292 | * |
||
293 | * @param Event $event |
||
294 | 18 | */ |
|
295 | public function onAddProfile($event) |
||
303 | |||
304 | /** |
||
305 | * |
||
306 | * @param Event $event |
||
307 | */ |
||
308 | 18 | public function onAssignCreator($event) |
|
312 | 18 | ||
313 | /** |
||
314 | 18 | * |
|
315 | 18 | * @param Event $event |
|
316 | */ |
||
317 | 3 | public function onRevokeCreator($event) |
|
326 | |||
327 | /** |
||
328 | 18 | * |
|
329 | * @param Event $event |
||
330 | */ |
||
331 | public function onRevokeAdministrators($event) |
||
342 | |||
343 | 2 | /** |
|
344 | * |
||
345 | 2 | * @param Event $event |
|
346 | */ |
||
347 | public function onRevokePermissions($event) |
||
351 | |||
352 | /** |
||
353 | 6 | * |
|
354 | * @return boolean |
||
355 | 6 | */ |
|
356 | public function isOrganization() |
||
360 | |||
361 | /** |
||
362 | 18 | * |
|
363 | * @return boolean |
||
364 | 18 | */ |
|
365 | public function isDepartment() |
||
369 | |||
370 | /** |
||
371 | 18 | * Check whether the current organization has a member. |
|
372 | * @param User $user |
||
373 | 18 | * @return boolean |
|
374 | */ |
||
375 | public function hasMember($user) |
||
379 | |||
380 | 1 | /** |
|
381 | * Get member query which role is specified `Creator`. |
||
382 | 1 | * @return MemberQuery |
|
383 | 1 | */ |
|
384 | 1 | public function getMemberCreators() |
|
388 | |||
389 | /** |
||
390 | * Get member query which role is specified `Administrator`. |
||
391 | * @return MemberQuery |
||
392 | 1 | */ |
|
393 | public function getMemberAdministrators() |
||
397 | 1 | ||
398 | /** |
||
399 | * Get user query which role is specified `Creator`. |
||
400 | * @return BaseUserQuery |
||
401 | */ |
||
402 | public function getCreator() |
||
409 | 33 | ||
410 | 1 | /** |
|
411 | * Get user query which role is specified `Administrator`. |
||
412 | 32 | * @return BaseUserQuery |
|
413 | 32 | */ |
|
414 | public function getAdministrators() |
||
421 | |||
422 | /** |
||
423 | 32 | * |
|
424 | * @param User $user |
||
425 | * @return boolean |
||
426 | * @throws \Exception |
||
427 | * @throws IntegrityException |
||
428 | */ |
||
429 | 32 | protected function addCreator($user) |
|
453 | |||
454 | /** |
||
455 | * |
||
456 | * @param User $user |
||
457 | * @return boolean |
||
458 | 5 | * @throws \Exception |
|
459 | * @throws IntegrityException |
||
460 | */ |
||
461 | public function addAdministrator($user) |
||
478 | |||
479 | /** |
||
480 | * |
||
481 | * @param type $user |
||
482 | * @return boolean |
||
483 | */ |
||
484 | public function hasAdministrator($user) |
||
492 | } |
||
493 |