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 |
||
| 35 | trait UserOrganizationTrait |
||
| 36 | { |
||
| 37 | public $organizationClass = Organization::class; |
||
| 38 | public $memberClass = Member::class; |
||
| 39 | private $noInitOrganization; |
||
| 40 | private $noInitMember; |
||
| 41 | public $lastSetUpOrganization; |
||
| 42 | /** |
||
| 43 | * @return Organization |
||
| 44 | */ |
||
| 45 | protected function getNoInitOrganization() |
||
| 53 | /** |
||
| 54 | * @return Member |
||
| 55 | */ |
||
| 56 | 25 | protected function getNoInitMember() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @return MemberQuery |
||
| 68 | */ |
||
| 69 | 25 | public function getOfMembers() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @return MemberQuery |
||
| 77 | */ |
||
| 78 | 2 | public function getOfCreators() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * |
||
| 85 | * @return MemberQuery |
||
| 86 | */ |
||
| 87 | 2 | public function getOfAdministrators() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | * @return OrganizationQuery |
||
| 95 | */ |
||
| 96 | 10 | public function getAtOrganizations() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * |
||
| 103 | * @return OrganizationQuery |
||
| 104 | */ |
||
| 105 | 2 | public function getCreatorsAtOrganizations() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | * @return OrganizationQuery |
||
| 113 | */ |
||
| 114 | 2 | public function getAdministratorsAtOrganizations() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Set up organization. |
||
| 121 | * @param string $name |
||
| 122 | * @param Organization $parent |
||
| 123 | * @param string $nickname |
||
| 124 | * @param integer $gravatar_type |
||
| 125 | * @param string $gravatar |
||
| 126 | * @param string $timezone |
||
| 127 | * @param string $description |
||
| 128 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 129 | */ |
||
| 130 | 32 | public function setUpOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set up organization. |
||
| 148 | * @param string $name |
||
| 149 | * @param Organization $parent |
||
| 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 | */ |
||
| 157 | 3 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Set up base organization. |
||
| 178 | * @param Organization $models |
||
| 179 | * @return boolean |
||
| 180 | * @throws InvalidConfigException |
||
| 181 | * @throws \Exception |
||
| 182 | */ |
||
| 183 | 32 | protected function setUpBaseOrganization($models) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Create organization. |
||
| 208 | * @param string $name |
||
| 209 | * @param Organization $parent |
||
| 210 | * @param string $nickname |
||
| 211 | * @param string $gravatar_type |
||
| 212 | * @param string $gravatar |
||
| 213 | * @param string $timezone |
||
| 214 | * @param string $description |
||
| 215 | * @return Organization |
||
| 216 | */ |
||
| 217 | 31 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Create department. |
||
| 224 | * @param string $name |
||
| 225 | * @param Organization $parent |
||
| 226 | * @param string $nickname |
||
| 227 | * @param string $gravatar_type |
||
| 228 | * @param string $gravatar |
||
| 229 | * @param string $timezone |
||
| 230 | * @param string $description |
||
| 231 | * @return Organization |
||
| 232 | */ |
||
| 233 | 1 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Create Base Organization. |
||
| 240 | * @param string $name |
||
| 241 | * @param Organization $parent |
||
| 242 | * @param string $nickname |
||
| 243 | * @param integer $gravatar_type |
||
| 244 | * @param string $gravatar |
||
| 245 | * @param string $timezone |
||
| 246 | * @param string $description |
||
| 247 | * @param integer $type |
||
| 248 | * @return Organization |
||
| 249 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 250 | * - The parent is itself. |
||
| 251 | * - The parent has already been its ancestor. |
||
| 252 | * - The current organization has reached the limit of ancestors. |
||
| 253 | */ |
||
| 254 | 31 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Revoke organization. |
||
| 276 | * @param static|string|integer $organization |
||
| 277 | * @param boolean $revokeIfHasChildren |
||
| 278 | * @throws InvalidParamException throw if current user is not the creator of organization. |
||
| 279 | */ |
||
| 280 | 8 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * |
||
| 317 | * @param Organization $organization |
||
| 318 | */ |
||
| 319 | 8 | public function isOrganizationCreator($organization) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * |
||
| 330 | * @param Organization $organization |
||
| 331 | */ |
||
| 332 | 2 | public function isOrganizationAdministrator($organization) |
|
| 340 | } |
||
| 341 |
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.