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 |
||
| 40 | trait UserOrganizationTrait |
||
| 41 | { |
||
| 42 | public $organizationClass = Organization::class; |
||
| 43 | public $memberClass = Member::class; |
||
| 44 | private $noInitOrganization; |
||
| 45 | private $noInitMember; |
||
| 46 | public $lastSetUpOrganization; |
||
| 47 | /** |
||
| 48 | * @return Organization |
||
| 49 | */ |
||
| 50 | protected function getNoInitOrganization() |
||
| 51 | { |
||
| 52 | if (!$this->noInitOrganization) { |
||
| 53 | $class = $this->organizationClass; |
||
| 54 | $this->noInitOrganization = $class::buildNoInitModel(); |
||
| 55 | } |
||
| 56 | return $this->noInitOrganization; |
||
| 57 | } |
||
| 58 | /** |
||
| 59 | * @return Member |
||
| 60 | */ |
||
| 61 | 25 | protected function getNoInitMember() |
|
| 62 | { |
||
| 63 | 25 | if (!$this->noInitMember) { |
|
| 64 | 25 | $class = $this->memberClass; |
|
| 65 | 25 | $this->noInitMember = $class::buildNoInitModel(); |
|
| 66 | 25 | } |
|
| 67 | 25 | return $this->noInitMember; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * |
||
| 72 | * @return MemberQuery |
||
| 73 | */ |
||
| 74 | 25 | public function getOfMembers() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @return MemberQuery |
||
| 82 | */ |
||
| 83 | 16 | public function getOfCreators() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @return MemberQuery |
||
| 91 | */ |
||
| 92 | 2 | public function getOfAdministrators() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get query of organization of which this user has been a member. |
||
| 99 | * If you access this method as magic-property `atOrganizations`, you will |
||
| 100 | * get all organizations the current user has joined in. |
||
| 101 | * @return OrganizationQuery |
||
| 102 | */ |
||
| 103 | 8 | public function getAtOrganizations() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | * @return OrganizationQuery |
||
| 111 | */ |
||
| 112 | 16 | public function getCreatorsAtOrganizations() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * |
||
| 119 | * @return OrganizationQuery |
||
| 120 | */ |
||
| 121 | 2 | public function getAdministratorsAtOrganizations() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Set up organization. |
||
| 128 | * @param string $name |
||
| 129 | * @param string $nickname |
||
| 130 | * @param integer $gravatar_type |
||
| 131 | * @param string $gravatar |
||
| 132 | * @param string $timezone |
||
| 133 | * @param string $description |
||
| 134 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 135 | * @throws InvalidParamException |
||
| 136 | * @throws \Exception |
||
| 137 | */ |
||
| 138 | 31 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set up organization. |
||
| 160 | * @param string $name |
||
| 161 | * @param Organization $parent |
||
| 162 | * @param string $nickname |
||
| 163 | * @param integer $gravatar_type |
||
| 164 | * @param string $gravatar |
||
| 165 | * @param string $timezone |
||
| 166 | * @param string $description |
||
| 167 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 168 | * @throws InvalidParamException |
||
| 169 | * @throws \Exception |
||
| 170 | */ |
||
| 171 | 8 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Set up base organization. |
||
| 196 | * @param Organization $models |
||
| 197 | * @return boolean |
||
| 198 | * @throws InvalidConfigException |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | 31 | protected function setUpBaseOrganization($models) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Create organization. |
||
| 226 | * @param string $name |
||
| 227 | * @param Organization $parent |
||
| 228 | * @param string $nickname |
||
| 229 | * @param string $gravatar_type |
||
| 230 | * @param string $gravatar |
||
| 231 | * @param string $timezone |
||
| 232 | * @param string $description |
||
| 233 | * @return Organization |
||
| 234 | */ |
||
| 235 | 30 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Create department. |
||
| 242 | * @param string $name |
||
| 243 | * @param Organization $parent |
||
| 244 | * @param string $nickname |
||
| 245 | * @param string $gravatar_type |
||
| 246 | * @param string $gravatar |
||
| 247 | * @param string $timezone |
||
| 248 | * @param string $description |
||
| 249 | * @return Organization |
||
| 250 | */ |
||
| 251 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Create Base Organization. |
||
| 258 | * @param string $name |
||
| 259 | * @param Organization $parent |
||
| 260 | * @param string $nickname |
||
| 261 | * @param integer $gravatar_type |
||
| 262 | * @param string $gravatar |
||
| 263 | * @param string $timezone |
||
| 264 | * @param string $description |
||
| 265 | * @param integer $type |
||
| 266 | * @return Organization |
||
| 267 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 268 | * - The parent is itself. |
||
| 269 | * - The parent has already been its ancestor. |
||
| 270 | * - The current organization has reached the limit of ancestors. |
||
| 271 | */ |
||
| 272 | 30 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Revoke organization or department. |
||
| 294 | * @param static|string|integer $organization |
||
| 295 | * @param boolean $revokeIfHasChildren |
||
| 296 | * @throws InvalidParamException throw if current user is not the creator of organization. |
||
| 297 | */ |
||
| 298 | 9 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * |
||
| 338 | * @param Organization $organization |
||
| 339 | */ |
||
| 340 | 14 | public function isOrganizationCreator($organization) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * |
||
| 351 | * @param Organization $organization |
||
| 352 | */ |
||
| 353 | 5 | public function isOrganizationAdministrator($organization) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Attach events associated with organization. |
||
| 364 | */ |
||
| 365 | 32 | public function initOrganizationEvents() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Revoke Organization Event. |
||
| 372 | * It should be triggered when deleting (not deregistering). |
||
| 373 | * @param Event $event |
||
| 374 | */ |
||
| 375 | 15 | public function onRevokeOrganizationsByCreator($event) |
|
| 385 | } |
||
| 386 |
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.