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 |
||
| 43 | trait UserOrganizationTrait |
||
| 44 | { |
||
| 45 | public $organizationClass = Organization::class; |
||
| 46 | public $memberClass = Member::class; |
||
| 47 | private $noInitOrganization; |
||
| 48 | private $noInitMember; |
||
| 49 | public $lastSetUpOrganization; |
||
| 50 | /** |
||
| 51 | * @return Organization |
||
| 52 | */ |
||
| 53 | public function getNoInitOrganization() |
||
| 61 | /** |
||
| 62 | * @return Member |
||
| 63 | */ |
||
| 64 | 28 | public function getNoInitMember() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Get member query. |
||
| 75 | * @return MemberQuery |
||
| 76 | */ |
||
| 77 | 28 | public function getOfMembers() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Get query of member whose role is creator. |
||
| 84 | * @return MemberQuery |
||
| 85 | */ |
||
| 86 | 16 | public function getOfCreators() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Get query of member whose role is administrator. |
||
| 93 | * @return MemberQuery |
||
| 94 | */ |
||
| 95 | 2 | public function getOfAdministrators() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get query of organization of which this user has been a member. |
||
| 102 | * If you access this method as magic-property `atOrganizations`, you will |
||
| 103 | * get all organizations the current user has joined in. |
||
| 104 | * @return OrganizationQuery |
||
| 105 | */ |
||
| 106 | 12 | public function getAtOrganizations() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | * @return OrganizationQuery |
||
| 114 | */ |
||
| 115 | public function getAtOrganizationsOnly() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @return OrganizationQuery |
||
| 123 | */ |
||
| 124 | public function getAtDepartmentsOnly() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * |
||
| 131 | * @return OrganizationQuery |
||
| 132 | */ |
||
| 133 | 16 | public function getCreatorsAtOrganizations() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | * @return OrganizationQuery |
||
| 141 | */ |
||
| 142 | 2 | public function getAdministratorsAtOrganizations() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Set up organization. |
||
| 149 | * @param string $name |
||
| 150 | * @param string $nickname |
||
| 151 | * @param integer $gravatar_type |
||
| 152 | * @param string $gravatar |
||
| 153 | * @param string $timezone |
||
| 154 | * @param string $description |
||
| 155 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 156 | * @throws InvalidParamException |
||
| 157 | * @throws \Exception |
||
| 158 | */ |
||
| 159 | 35 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Set up organization. |
||
| 181 | * @param string $name Department name. |
||
| 182 | * @param Organization $parent Parent organization or department. |
||
| 183 | * @param string $nickname |
||
| 184 | * @param integer $gravatar_type |
||
| 185 | * @param string $gravatar |
||
| 186 | * @param string $timezone |
||
| 187 | * @param string $description |
||
| 188 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 189 | * @throws InvalidParamException |
||
| 190 | * @throws \Exception |
||
| 191 | */ |
||
| 192 | 8 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set up base organization. |
||
| 217 | * @param Organization $models |
||
| 218 | * @return boolean |
||
| 219 | * @throws InvalidConfigException |
||
| 220 | * @throws \Exception |
||
| 221 | */ |
||
| 222 | 35 | protected function setUpBaseOrganization($models) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Create organization. |
||
| 247 | * @param string $name |
||
| 248 | * @param Organization $parent |
||
| 249 | * @param string $nickname |
||
| 250 | * @param string $gravatar_type |
||
| 251 | * @param string $gravatar |
||
| 252 | * @param string $timezone |
||
| 253 | * @param string $description |
||
| 254 | * @return Organization |
||
| 255 | */ |
||
| 256 | 34 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Create department. |
||
| 263 | * @param string $name |
||
| 264 | * @param Organization $parent |
||
| 265 | * @param string $nickname |
||
| 266 | * @param string $gravatar_type |
||
| 267 | * @param string $gravatar |
||
| 268 | * @param string $timezone |
||
| 269 | * @param string $description |
||
| 270 | * @return Organization |
||
| 271 | */ |
||
| 272 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Create Base Organization. |
||
| 279 | * @param string $name |
||
| 280 | * @param Organization $parent |
||
| 281 | * @param string $nickname |
||
| 282 | * @param integer $gravatar_type |
||
| 283 | * @param string $gravatar |
||
| 284 | * @param string $timezone |
||
| 285 | * @param string $description |
||
| 286 | * @param integer $type |
||
| 287 | * @return Organization |
||
| 288 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 289 | * - The parent is itself. |
||
| 290 | * - The parent has already been its ancestor. |
||
| 291 | * - The current organization has reached the limit of ancestors. |
||
| 292 | */ |
||
| 293 | 34 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Revoke organization or department. |
||
| 315 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
| 316 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
| 317 | * @return boolean True if revocation is successful. |
||
| 318 | * @throws InvalidParamException throws if organization is invalid. |
||
| 319 | * @throws \Exception |
||
| 320 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
| 321 | * same time the current organization or department has subordinates. |
||
| 322 | * @throws @var:$organization@mtd:deregister |
||
| 323 | */ |
||
| 324 | 9 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Check whether current user is organization or department creator. |
||
| 368 | * @param Organization $organization |
||
| 369 | * @return boolean True if current is organization or department creator. |
||
| 370 | */ |
||
| 371 | 14 | public function isOrganizationCreator($organization) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Check whether current user is organization or department administrator. |
||
| 382 | * @param Organization $organization |
||
| 383 | * @return boolean True if current is organization or department administrator. |
||
| 384 | */ |
||
| 385 | 5 | public function isOrganizationAdministrator($organization) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Attach events associated with organization. |
||
| 396 | */ |
||
| 397 | 36 | public function initOrganizationEvents() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Revoke Organization Event. |
||
| 404 | * It should be triggered when deleting (not deregistering). |
||
| 405 | * @param Event $event |
||
| 406 | */ |
||
| 407 | 15 | public function onRevokeOrganizationsByCreator($event) |
|
| 417 | } |
||
| 418 |
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.