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() |
||
| 80 | /** |
||
| 81 | * @return Organization |
||
| 82 | */ |
||
| 83 | public function getNoInitOrganization() |
||
| 91 | /** |
||
| 92 | * @return Member |
||
| 93 | */ |
||
| 94 | 36 | public function getNoInitMember() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Get member query. |
||
| 105 | * @return MemberQuery |
||
| 106 | */ |
||
| 107 | 36 | public function getOfMembers() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Get query of member whose role is creator. |
||
| 114 | * @return MemberQuery |
||
| 115 | */ |
||
| 116 | 36 | 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 | 36 | public function getCreatorsAtOrganizations() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * |
||
| 170 | * @return OrganizationQuery |
||
| 171 | */ |
||
| 172 | 2 | public function getAdministratorsAtOrganizations() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Get Organization Limit Query. |
||
| 179 | * @return BaseBlameableQuery |
||
| 180 | */ |
||
| 181 | public function getOrganizationLimit() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set up organization. |
||
| 191 | * @param string $name |
||
| 192 | * @param string $nickname |
||
| 193 | * @param integer $gravatar_type |
||
| 194 | * @param string $gravatar |
||
| 195 | * @param string $timezone |
||
| 196 | * @param string $description |
||
| 197 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 198 | * @throws InvalidParamException |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | 36 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Set up organization. |
||
| 223 | * @param string $name Department name. |
||
| 224 | * @param Organization $parent Parent organization or department. |
||
| 225 | * @param string $nickname |
||
| 226 | * @param integer $gravatar_type |
||
| 227 | * @param string $gravatar |
||
| 228 | * @param string $timezone |
||
| 229 | * @param string $description |
||
| 230 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 231 | * @throws InvalidParamException |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | 8 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Set up base organization. |
||
| 259 | * @param Organization $models |
||
| 260 | * @return boolean |
||
| 261 | * @throws InvalidConfigException |
||
| 262 | * @throws \Exception |
||
| 263 | */ |
||
| 264 | 36 | protected function setUpBaseOrganization($models) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Create organization. |
||
| 289 | * @param string $name |
||
| 290 | * @param Organization $parent |
||
| 291 | * @param string $nickname |
||
| 292 | * @param string $gravatar_type |
||
| 293 | * @param string $gravatar |
||
| 294 | * @param string $timezone |
||
| 295 | * @param string $description |
||
| 296 | * @return Organization |
||
| 297 | */ |
||
| 298 | 35 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Create department. |
||
| 305 | * @param string $name |
||
| 306 | * @param Organization $parent |
||
| 307 | * @param string $nickname |
||
| 308 | * @param string $gravatar_type |
||
| 309 | * @param string $gravatar |
||
| 310 | * @param string $timezone |
||
| 311 | * @param string $description |
||
| 312 | * @return Organization |
||
| 313 | */ |
||
| 314 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Create Base Organization. |
||
| 321 | * @param string $name |
||
| 322 | * @param Organization $parent |
||
| 323 | * @param string $nickname |
||
| 324 | * @param integer $gravatar_type |
||
| 325 | * @param string $gravatar |
||
| 326 | * @param string $timezone |
||
| 327 | * @param string $description |
||
| 328 | * @param integer $type |
||
| 329 | * @return Organization |
||
| 330 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 331 | * - The parent is itself. |
||
| 332 | * - The parent has already been its ancestor. |
||
| 333 | * - The current organization has reached the limit of ancestors. |
||
| 334 | */ |
||
| 335 | 35 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Revoke organization or department. |
||
| 357 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
| 358 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
| 359 | * @return boolean True if revocation is successful. |
||
| 360 | * @throws InvalidParamException throws if organization is invalid. |
||
| 361 | * @throws \Exception |
||
| 362 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
| 363 | * same time the current organization or department has subordinates. |
||
| 364 | * @throws @var:$organization@mtd:deregister |
||
| 365 | */ |
||
| 366 | 10 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Check whether current user is organization or department creator. |
||
| 410 | * @param Organization $organization |
||
| 411 | * @return boolean True if current is organization or department creator. |
||
| 412 | */ |
||
| 413 | 15 | public function isOrganizationCreator($organization) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Check whether current user is organization or department administrator. |
||
| 424 | * @param Organization $organization |
||
| 425 | * @return boolean True if current is organization or department administrator. |
||
| 426 | */ |
||
| 427 | 5 | public function isOrganizationAdministrator($organization) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Attach events associated with organization. |
||
| 438 | */ |
||
| 439 | 37 | public function initOrganizationEvents() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Revoke Organization Event. |
||
| 446 | * It should be triggered when deleting (not deregistering). |
||
| 447 | * @param Event $event |
||
| 448 | */ |
||
| 449 | 16 | public function onRevokeOrganizationsByCreator($event) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Check whether the current user has reached the upper limit of organizations. |
||
| 462 | * @return boolean the upper limit of organizations which current could be set up. |
||
| 463 | */ |
||
| 464 | 36 | public function hasReachedOrganizationLimit() |
|
| 477 | } |
||
| 478 |
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.