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 |
||
| 39 | trait UserOrganizationTrait |
||
| 40 | { |
||
| 41 | public $organizationClass = Organization::class; |
||
| 42 | public $memberClass = Member::class; |
||
| 43 | private $noInitOrganization; |
||
| 44 | private $noInitMember; |
||
| 45 | public $lastSetUpOrganization; |
||
| 46 | /** |
||
| 47 | * @return Organization |
||
| 48 | */ |
||
| 49 | protected function getNoInitOrganization() |
||
| 57 | /** |
||
| 58 | * @return Member |
||
| 59 | */ |
||
| 60 | 26 | protected function getNoInitMember() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @return MemberQuery |
||
| 72 | */ |
||
| 73 | 26 | public function getOfMembers() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * |
||
| 80 | * @return MemberQuery |
||
| 81 | */ |
||
| 82 | 2 | public function getOfCreators() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | * @return MemberQuery |
||
| 90 | */ |
||
| 91 | 2 | public function getOfAdministrators() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * |
||
| 98 | * @return OrganizationQuery |
||
| 99 | */ |
||
| 100 | 11 | public function getAtOrganizations() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * |
||
| 107 | * @return OrganizationQuery |
||
| 108 | */ |
||
| 109 | 2 | public function getCreatorsAtOrganizations() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * |
||
| 116 | * @return OrganizationQuery |
||
| 117 | */ |
||
| 118 | 2 | public function getAdministratorsAtOrganizations() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Set up organization. |
||
| 125 | * @param string $name |
||
| 126 | * @param string $nickname |
||
| 127 | * @param integer $gravatar_type |
||
| 128 | * @param string $gravatar |
||
| 129 | * @param string $timezone |
||
| 130 | * @param string $description |
||
| 131 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 132 | */ |
||
| 133 | 33 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Set up organization. |
||
| 155 | * @param string $name |
||
| 156 | * @param Organization $parent |
||
| 157 | * @param string $nickname |
||
| 158 | * @param integer $gravatar_type |
||
| 159 | * @param string $gravatar |
||
| 160 | * @param string $timezone |
||
| 161 | * @param string $description |
||
| 162 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 163 | */ |
||
| 164 | 6 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Set up base organization. |
||
| 189 | * @param Organization $models |
||
| 190 | * @return boolean |
||
| 191 | * @throws InvalidConfigException |
||
| 192 | * @throws \Exception |
||
| 193 | */ |
||
| 194 | 33 | protected function setUpBaseOrganization($models) |
|
| 195 | { |
||
| 196 | 33 | $model = null; |
|
| 197 | 33 | $associatedModels = []; |
|
| 198 | 33 | if (is_array($models)) { |
|
| 199 | 2 | if (!array_key_exists(0, $models)) { |
|
| 200 | 2 | throw new InvalidConfigException('Invalid Organization Model.'); |
|
| 201 | } |
||
| 202 | $model = $models[0]; |
||
| 203 | $associatedModels = array_key_exists('associatedModels', $models) ? $models['associatedModels'] : []; |
||
| 204 | 32 | } elseif ($models instanceof $this->organizationClass) { |
|
| 205 | 32 | $model = $models; |
|
| 206 | } |
||
| 207 | 32 | $result = $model->register($associatedModels); |
|
| 208 | 32 | if ($result instanceof \Exception) { |
|
| 209 | throw $result; |
||
| 210 | } |
||
| 211 | 32 | if ($result !== true) { |
|
| 212 | throw new \Exception('Failed to set up.'); |
||
| 213 | } |
||
| 214 | 32 | return true; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Create organization. |
||
| 219 | * @param string $name |
||
| 220 | * @param Organization $parent |
||
| 221 | * @param string $nickname |
||
| 222 | * @param string $gravatar_type |
||
| 223 | * @param string $gravatar |
||
| 224 | * @param string $timezone |
||
| 225 | * @param string $description |
||
| 226 | * @return Organization |
||
| 227 | */ |
||
| 228 | 32 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Create department. |
||
| 235 | * @param string $name |
||
| 236 | * @param Organization $parent |
||
| 237 | * @param string $nickname |
||
| 238 | * @param string $gravatar_type |
||
| 239 | * @param string $gravatar |
||
| 240 | * @param string $timezone |
||
| 241 | * @param string $description |
||
| 242 | * @return Organization |
||
| 243 | */ |
||
| 244 | 4 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Create Base Organization. |
||
| 251 | * @param string $name |
||
| 252 | * @param Organization $parent |
||
| 253 | * @param string $nickname |
||
| 254 | * @param integer $gravatar_type |
||
| 255 | * @param string $gravatar |
||
| 256 | * @param string $timezone |
||
| 257 | * @param string $description |
||
| 258 | * @param integer $type |
||
| 259 | * @return Organization |
||
| 260 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 261 | * - The parent is itself. |
||
| 262 | * - The parent has already been its ancestor. |
||
| 263 | * - The current organization has reached the limit of ancestors. |
||
| 264 | */ |
||
| 265 | 32 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Revoke organization or department. |
||
| 287 | * @param static|string|integer $organization |
||
| 288 | * @param boolean $revokeIfHasChildren |
||
| 289 | * @throws InvalidParamException throw if current user is not the creator of organization. |
||
| 290 | */ |
||
| 291 | 9 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
| 328 | 8 | ||
| 329 | /** |
||
| 330 | * |
||
| 331 | * @param Organization $organization |
||
| 332 | */ |
||
| 333 | public function isOrganizationCreator($organization) |
||
| 341 | 13 | ||
| 342 | /** |
||
| 343 | 13 | * |
|
| 344 | 13 | * @param Organization $organization |
|
| 345 | 2 | */ |
|
| 346 | public function isOrganizationAdministrator($organization) |
||
| 354 | } |
||
| 355 |
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.