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 |
||
| 44 | class Organization extends User |
||
| 45 | { |
||
| 46 | use SelfBlameableTrait; |
||
| 47 | |||
| 48 | const TYPE_ORGANIZATION = 1; |
||
| 49 | const TYPE_DEPARTMENT = 2; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean Organization does not need password and corresponding features. |
||
| 53 | */ |
||
| 54 | public $passwordHashAttribute = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean Organization does not need password and corresponding features. |
||
| 58 | */ |
||
| 59 | public $passwordResetTokenAttribute = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean Organization does not need password and corresponding features. |
||
| 63 | */ |
||
| 64 | public $passwordHistoryClass = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var boolean Organization does not need source. |
||
| 68 | */ |
||
| 69 | public $sourceAttribute = false; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean Organization does not need auth key. |
||
| 73 | */ |
||
| 74 | public $authKeyAttribute = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean Organization does not need access token. |
||
| 78 | */ |
||
| 79 | public $accessTokenAttribute = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * |
||
| 83 | * @var boolean Organization does not need login log. |
||
| 84 | */ |
||
| 85 | public $loginLogClass = false; |
||
| 86 | |||
| 87 | public $profileClass = Profile::class; |
||
| 88 | |||
| 89 | public $memberClass = Member::class; |
||
| 90 | private $noInitMember; |
||
| 91 | public $creatorModel; |
||
| 92 | public $profileConfig; |
||
| 93 | /** |
||
| 94 | * @return Member |
||
| 95 | */ |
||
| 96 | 21 | protected function getNoInitMember() |
|
| 104 | |||
| 105 | 33 | public function init() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | 1 | public function attributeLabels() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritdoc |
||
| 142 | */ |
||
| 143 | 33 | public static function tableName() |
|
| 147 | |||
| 148 | 32 | protected function getTypeRules() |
|
| 156 | |||
| 157 | 32 | public function rules() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get Member Query. |
||
| 164 | * @return MemberQuery |
||
| 165 | */ |
||
| 166 | 21 | public function getMembers() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Get organization member users' query. |
||
| 173 | * @return BaseUserQuery |
||
| 174 | */ |
||
| 175 | 2 | 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 | 14 | 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 | 31 | public function addMember(&$member) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Create member model, and set organization with this. |
||
| 218 | * @param Member $member If this parameter is not new record, it's organization |
||
| 219 | * will be set with this, and return it. Otherwise, it will extract `User` |
||
| 220 | * model and create new `Member` model. |
||
| 221 | * @see createMemberModelWithUser |
||
| 222 | * @return Member |
||
| 223 | */ |
||
| 224 | public function createMemberModel($member) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Create member model with user, and set organization with this. |
||
| 235 | * @param User|string|integer $user |
||
| 236 | * @return Member |
||
| 237 | */ |
||
| 238 | 31 | public function createMemberModelWithUser($user) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Remove member. |
||
| 253 | * @param Member|User $member |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | 1 | public function removeMember(&$member) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * |
||
| 270 | * @param Event $event |
||
| 271 | */ |
||
| 272 | 32 | public function onAddProfile($event) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * |
||
| 283 | * @param Event $event |
||
| 284 | */ |
||
| 285 | 32 | public function onAssignCreator($event) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * |
||
| 292 | * @param Event $event |
||
| 293 | */ |
||
| 294 | 18 | public function onRevokeCreator($event) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * |
||
| 306 | * @param Event $event |
||
| 307 | */ |
||
| 308 | 18 | public function onRevokeAdministrators($event) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | * @param Event $event |
||
| 324 | */ |
||
| 325 | 18 | public function onRevokePermissions($event) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * @return boolean |
||
| 333 | */ |
||
| 334 | 2 | public function isOrganization() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | 2 | public function isDepartment() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Check whether the current organization has a member. |
||
| 350 | * @param User $user |
||
| 351 | * @return boolean |
||
| 352 | */ |
||
| 353 | 1 | public function hasMember($user) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get member query which role is specified `Creator`. |
||
| 360 | * @return MemberQuery |
||
| 361 | */ |
||
| 362 | 18 | public function getMemberCreators() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get member query which role is specified `Administrator`. |
||
| 369 | * @return MemberQuery |
||
| 370 | */ |
||
| 371 | 18 | public function getMemberAdministrators() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Get user query which role is specified `Creator`. |
||
| 378 | * @return BaseUserQuery |
||
| 379 | */ |
||
| 380 | 1 | public function getCreator() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Get user query which role is specified `Administrator`. |
||
| 390 | * @return BaseUserQuery |
||
| 391 | */ |
||
| 392 | 1 | public function getAdministrators() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * |
||
| 402 | * @param User $user |
||
| 403 | * @return boolean |
||
| 404 | * @throws \Exception |
||
| 405 | * @throws IntegrityException |
||
| 406 | */ |
||
| 407 | 32 | protected function addCreator($user) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * |
||
| 434 | * @param User $user |
||
| 435 | * @return boolean |
||
| 436 | * @throws \Exception |
||
| 437 | * @throws IntegrityException |
||
| 438 | */ |
||
| 439 | 4 | public function addAdministrator($user) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * |
||
| 463 | * @param type $user |
||
| 464 | * @return boolean |
||
| 465 | */ |
||
| 466 | 1 | public function hasAdministrator($user) |
|
| 474 | } |
||
| 475 |