Complex classes like UserOrganizationTrait 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 UserOrganizationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | trait UserOrganizationTrait |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var string The organization class. |
||
| 49 | * Note: Please assign it with your own Organization class. |
||
| 50 | */ |
||
| 51 | public $organizationClass = Organization::class; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string The organization limit class. |
||
| 55 | * Note: Please assign it with your own OrganizationLimit class. |
||
| 56 | */ |
||
| 57 | public $organizationLimitClass = OrganizationLimit::class; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string The member class. |
||
| 61 | * Note: Please assign it with your own Member class. |
||
| 62 | */ |
||
| 63 | public $memberClass = Member::class; |
||
| 64 | private $noInitOrganizationLimit; |
||
| 65 | private $noInitOrganization; |
||
| 66 | private $noInitMember; |
||
| 67 | public $lastSetUpOrganization; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return OrganizationLimit |
||
| 71 | */ |
||
| 72 | public function getNoInitOrganizationLimit() |
||
| 73 | { |
||
| 74 | if (!$this->noInitOrganizationLimit) { |
||
| 75 | $class = $this->organizationLimitClass; |
||
| 76 | $this->noInitOrganizationLimit = $class::buildNoInitModel(); |
||
| 77 | } |
||
| 78 | return $this->noInitOrganizationLimit; |
||
| 79 | } |
||
| 80 | /** |
||
| 81 | * @return Organization |
||
| 82 | */ |
||
| 83 | public function getNoInitOrganization() |
||
| 84 | { |
||
| 85 | if (!$this->noInitOrganization) { |
||
| 86 | $class = $this->organizationClass; |
||
| 87 | $this->noInitOrganization = $class::buildNoInitModel(); |
||
| 88 | } |
||
| 89 | return $this->noInitOrganization; |
||
| 90 | } |
||
| 91 | /** |
||
| 92 | * @return Member |
||
| 93 | */ |
||
| 94 | 37 | public function getNoInitMember() |
|
| 95 | { |
||
| 96 | 37 | if (!$this->noInitMember) { |
|
| 97 | 37 | $class = $this->memberClass; |
|
| 98 | 37 | $this->noInitMember = $class::buildNoInitModel(); |
|
| 99 | 37 | } |
|
| 100 | 37 | return $this->noInitMember; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get member query. |
||
| 105 | * @return MemberQuery |
||
| 106 | */ |
||
| 107 | 37 | public function getOfMembers() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Get query of member whose role is creator. |
||
| 114 | * @return MemberQuery |
||
| 115 | */ |
||
| 116 | 37 | public function getOfCreators() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Get query of member whose role is administrator. |
||
| 123 | * @return MemberQuery |
||
| 124 | */ |
||
| 125 | 2 | public function getOfAdministrators() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Get query of organization of which this user has been a member. |
||
| 132 | * If you access this method as magic-property `atOrganizations`, you will |
||
| 133 | * get all organizations the current user has joined in. |
||
| 134 | * @return OrganizationQuery |
||
| 135 | */ |
||
| 136 | 12 | public function getAtOrganizations() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @return OrganizationQuery |
||
| 144 | */ |
||
| 145 | public function getAtOrganizationsOnly() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * |
||
| 152 | * @return OrganizationQuery |
||
| 153 | */ |
||
| 154 | public function getAtDepartmentsOnly() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | * @return OrganizationQuery |
||
| 162 | */ |
||
| 163 | 37 | public function getCreatorsAtOrganizations() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * |
||
| 170 | * @return OrganizationQuery |
||
| 171 | */ |
||
| 172 | 37 | public function getCreatorsAtOrganizationsOnly() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * |
||
| 179 | * @return OrganizationQuery |
||
| 180 | */ |
||
| 181 | 2 | public function getAdministratorsAtOrganizations() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Get Organization Limit Query. |
||
| 188 | * @return BaseBlameableQuery |
||
| 189 | */ |
||
| 190 | public function getOrganizationLimit() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set up organization. |
||
| 200 | * @param string $name |
||
| 201 | * @param string $nickname |
||
| 202 | * @param integer $gravatar_type |
||
| 203 | * @param string $gravatar |
||
| 204 | * @param string $timezone |
||
| 205 | * @param string $description |
||
| 206 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 207 | * @throws InvalidParamException |
||
| 208 | * @throws \Exception |
||
| 209 | */ |
||
| 210 | 37 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Set up organization. |
||
| 232 | * @param string $name Department name. |
||
| 233 | * @param Organization $parent Parent organization or department. |
||
| 234 | * @param string $nickname |
||
| 235 | * @param integer $gravatar_type |
||
| 236 | * @param string $gravatar |
||
| 237 | * @param string $timezone |
||
| 238 | * @param string $description |
||
| 239 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 240 | * @throws InvalidParamException |
||
| 241 | * @throws \Exception |
||
| 242 | */ |
||
| 243 | 8 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Set up base organization. |
||
| 268 | * @param Organization $models |
||
| 269 | * @return boolean |
||
| 270 | * @throws InvalidConfigException |
||
| 271 | * @throws \Exception |
||
| 272 | */ |
||
| 273 | 37 | protected function setUpBaseOrganization($models) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Create organization. |
||
| 298 | * @param string $name |
||
| 299 | * @param Organization $parent |
||
| 300 | * @param string $nickname |
||
| 301 | * @param string $gravatar_type |
||
| 302 | * @param string $gravatar |
||
| 303 | * @param string $timezone |
||
| 304 | * @param string $description |
||
| 305 | * @return Organization |
||
| 306 | */ |
||
| 307 | 36 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Create department. |
||
| 314 | * @param string $name |
||
| 315 | * @param Organization $parent |
||
| 316 | * @param string $nickname |
||
| 317 | * @param string $gravatar_type |
||
| 318 | * @param string $gravatar |
||
| 319 | * @param string $timezone |
||
| 320 | * @param string $description |
||
| 321 | * @return Organization |
||
| 322 | */ |
||
| 323 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Create Base Organization. |
||
| 330 | * @param string $name |
||
| 331 | * @param Organization $parent |
||
| 332 | * @param string $nickname |
||
| 333 | * @param integer $gravatar_type |
||
| 334 | * @param string $gravatar |
||
| 335 | * @param string $timezone |
||
| 336 | * @param string $description |
||
| 337 | * @param integer $type |
||
| 338 | * @return Organization |
||
| 339 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 340 | * - The parent is itself. |
||
| 341 | * - The parent has already been its ancestor. |
||
| 342 | * - The current organization has reached the limit of ancestors. |
||
| 343 | */ |
||
| 344 | 36 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Revoke organization or department. |
||
| 366 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
| 367 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
| 368 | * @return boolean True if revocation is successful. |
||
| 369 | * @throws InvalidParamException throws if organization is invalid. |
||
| 370 | * @throws \Exception |
||
| 371 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
| 372 | * same time the current organization or department has subordinates. |
||
| 373 | * @throws @var:$organization@mtd:deregister |
||
| 374 | */ |
||
| 375 | 14 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
| 376 | { |
||
| 377 | 11 | if (!($organization instanceof $this->organizationClass)) |
|
| 378 | 11 | { |
|
| 379 | 2 | $class = $this->organizationClass; |
|
| 380 | 2 | $organization = $class::find()->guidOrId($organization)->one(); |
|
| 381 | 2 | } |
|
| 382 | 11 | if (!($organization instanceof $this->organizationClass)) { |
|
| 383 | throw new InvalidParamException('Invalid Organization.'); |
||
| 384 | } |
||
| 385 | 11 | if (!Yii::$app->authManager->checkAccess( |
|
| 386 | 11 | $this, |
|
| 387 | 11 | $organization->type == Organization::TYPE_ORGANIZATION ? (new RevokeOrganization)->name : (new RevokeDepartment)->name, |
|
| 388 | 11 | ['organization' => $organization])) { |
|
| 389 | 1 | throw new InvalidParamException("You do not have permission to revoke it."); |
|
| 390 | } |
||
| 391 | 10 | $transaction = Yii::$app->db->beginTransaction(); |
|
| 392 | try { |
||
| 393 | 10 | if (!$revokeIfHasChildren && ((int)($organization->getChildren()->count())) > 0) { |
|
| 394 | $type = $organization->isOrganization() ? "organization" : "department"; |
||
| 395 | throw new RevokePreventedException("The $type has children. Revoking prevented."); |
||
| 396 | } |
||
| 397 | 10 | $result = $organization->deregister(); |
|
| 398 | 14 | if ($result instanceof \Exception){ |
|
| 399 | 6 | throw $result; |
|
| 400 | } |
||
| 401 | 10 | if ($result !== true) { |
|
| 402 | 6 | throw new InvalidParamException("Failed to revoke."); |
|
| 403 | } |
||
| 404 | 10 | $transaction->commit(); |
|
| 405 | 10 | } catch (\Exception $ex) { |
|
| 406 | $transaction->rollBack(); |
||
| 407 | Yii::error($ex->getMessage(), __METHOD__); |
||
| 408 | throw $ex; |
||
| 409 | } |
||
| 410 | 10 | return true; |
|
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Check whether current user is organization or department creator. |
||
| 415 | * @param Organization $organization |
||
| 416 | * @return boolean True if current is organization or department creator. |
||
| 417 | */ |
||
| 418 | 16 | public function isOrganizationCreator($organization) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Check whether current user is organization or department administrator. |
||
| 429 | * @param Organization $organization |
||
| 430 | * @return boolean True if current is organization or department administrator. |
||
| 431 | */ |
||
| 432 | 5 | public function isOrganizationAdministrator($organization) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Attach events associated with organization. |
||
| 443 | */ |
||
| 444 | 38 | public function initOrganizationEvents() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Revoke Organization Event. |
||
| 451 | * It should be triggered when deleting (not deregistering). |
||
| 452 | * @param Event $event |
||
| 453 | */ |
||
| 454 | 17 | public function onRevokeOrganizationsByCreator($event) |
|
| 455 | { |
||
| 456 | 17 | $sender = $event->sender; |
|
| 457 | /* @var $sender static */ |
||
| 458 | 17 | $organizations = $this->creatorsAtOrganizations; |
|
| 459 | 17 | foreach ($organizations as $org) |
|
| 460 | { |
||
| 461 | 4 | $sender->revokeOrganization($org); |
|
| 462 | 17 | } |
|
| 463 | 17 | } |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Check whether the current user has reached the upper limit of organizations. |
||
| 467 | * @return boolean the upper limit of organizations which current could be set up. |
||
| 468 | */ |
||
| 469 | 37 | public function hasReachedOrganizationLimit() |
|
| 482 | } |
||
| 483 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.