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 |
||
| 42 | class Organization extends User |
||
| 43 | { |
||
| 44 | use SelfBlameableTrait; |
||
| 45 | |||
| 46 | const TYPE_ORGANIZATION = 1; |
||
| 47 | const TYPE_DEPARTMENT = 2; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean Organization does not need password and corresponding features. |
||
| 51 | */ |
||
| 52 | public $passwordHashAttribute = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var boolean Organization does not need password and corresponding features. |
||
| 56 | */ |
||
| 57 | public $passwordResetTokenAttribute = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean Organization does not need password and corresponding features. |
||
| 61 | */ |
||
| 62 | public $passwordHistoryClass = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var boolean Organization does not need source. |
||
| 66 | */ |
||
| 67 | public $sourceAttribute = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean Organization does not need auth key. |
||
| 71 | */ |
||
| 72 | public $authKeyAttribute = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var boolean Organization does not need access token. |
||
| 76 | */ |
||
| 77 | public $accessTokenAttribute = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @var boolean Organization does not need login log. |
||
| 82 | */ |
||
| 83 | public $loginLogClass = false; |
||
| 84 | |||
| 85 | public $profileClass = Profile::class; |
||
| 86 | |||
| 87 | public $memberClass = Member::class; |
||
| 88 | private $noInitMember; |
||
| 89 | public $creator; |
||
| 90 | public $profileConfig; |
||
| 91 | /** |
||
| 92 | * @return Member |
||
| 93 | */ |
||
| 94 | 16 | protected function getNoInitMember() |
|
| 102 | |||
| 103 | 25 | public function init() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | 1 | public function attributeLabels() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritdoc |
||
| 140 | */ |
||
| 141 | 25 | public static function tableName() |
|
| 145 | |||
| 146 | 24 | protected function getTypeRules() |
|
| 154 | |||
| 155 | 24 | public function rules() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get Member Query. |
||
| 162 | * @return MemberQuery |
||
| 163 | */ |
||
| 164 | 16 | public function getMembers() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get organization member users' query. |
||
| 171 | * @return BaseUserQuery |
||
| 172 | */ |
||
| 173 | 2 | public function getMemberUsers() |
|
| 180 | |||
| 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 | 7 | public function getMember($user) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Add member to organization. |
||
| 193 | * @param Member|User|string|integer $member |
||
| 194 | * @see createMemberModel |
||
| 195 | * @see createMemberModelWithUser |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | 23 | public function addMember(&$member) |
|
| 213 | |||
| 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 | 23 | public function createMemberModelWithUser($user) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Remove member. |
||
| 251 | * @param Member|User $member |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | 1 | public function removeMember(&$member) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * |
||
| 268 | * @param Event $event |
||
| 269 | */ |
||
| 270 | 24 | public function onAddProfile($event) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * |
||
| 281 | * @param Event $event |
||
| 282 | */ |
||
| 283 | 24 | public function onAssignCreator($event) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * |
||
| 290 | * @param Event $event |
||
| 291 | */ |
||
| 292 | 13 | public function onRevokeCreator($event) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * |
||
| 304 | * @param Event $event |
||
| 305 | */ |
||
| 306 | 13 | public function onRevokeAdministrators($event) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * |
||
| 321 | * @param Event $event |
||
| 322 | */ |
||
| 323 | 13 | public function onRevokePermissions($event) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * |
||
| 330 | * @return boolean |
||
| 331 | */ |
||
| 332 | 2 | public function isOrganization() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | 2 | public function isDepartment() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Check whether the current organization has a member. |
||
| 348 | * @param User $user |
||
| 349 | * @return boolean |
||
| 350 | */ |
||
| 351 | 1 | public function hasMember($user) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get creator query. |
||
| 358 | * @return MemberQuery |
||
| 359 | */ |
||
| 360 | 13 | public function getMemberCreators() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Get administrator query. |
||
| 367 | * @return MemberQuery |
||
| 368 | */ |
||
| 369 | 13 | public function getMemberAdministrators() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * |
||
| 376 | * @return BaseUserQuery |
||
| 377 | */ |
||
| 378 | public function getCreator() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * |
||
| 388 | * @return BaseUserQuery |
||
| 389 | */ |
||
| 390 | public function getAdministrators() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * |
||
| 400 | * @param User $user |
||
| 401 | * @return boolean |
||
| 402 | * @throws \Exception |
||
| 403 | * @throws IntegrityException |
||
| 404 | */ |
||
| 405 | 24 | protected function addCreator($user) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * |
||
| 432 | * @param User $user |
||
| 433 | * @return boolean |
||
| 434 | * @throws \Exception |
||
| 435 | * @throws IntegrityException |
||
| 436 | */ |
||
| 437 | 1 | public function addAdministrator($user) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * |
||
| 461 | * @param type $user |
||
| 462 | * @return boolean |
||
| 463 | */ |
||
| 464 | public function hasAdministrator($user) |
||
| 472 | } |
||
| 473 |