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 |
||
| 31 | trait UserOrganizationTrait |
||
| 32 | { |
||
| 33 | public $organizationClass = Organization::class; |
||
| 34 | public $memberClass = Member::class; |
||
| 35 | private $noInitOrganization; |
||
| 36 | private $noInitMember; |
||
| 37 | public $lastSetUpOrganization; |
||
| 38 | /** |
||
| 39 | * @return Organization |
||
| 40 | */ |
||
| 41 | protected function getNoInitOrganization() |
||
| 49 | /** |
||
| 50 | * @return Member |
||
| 51 | */ |
||
| 52 | 17 | protected function getNoInitMember() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * |
||
| 63 | * @return MemberQuery |
||
| 64 | */ |
||
| 65 | 17 | public function getOfMembers() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * |
||
| 72 | * @return MemberQuery |
||
| 73 | */ |
||
| 74 | public function getOfCreators() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @return MemberQuery |
||
| 82 | */ |
||
| 83 | public function getOfAdministrators() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @return OrganizationQuery |
||
| 91 | */ |
||
| 92 | 8 | public function getAtOrganizations() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * |
||
| 99 | * @return OrganizationQuery |
||
| 100 | */ |
||
| 101 | public function getCreatorsAtOrganizations() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * |
||
| 108 | * @return OrganizationQuery |
||
| 109 | */ |
||
| 110 | public function getAdministratorsAtOrganizations() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set up organization. |
||
| 117 | * @param string $name |
||
| 118 | * @param Organization $parent |
||
| 119 | * @param string $nickname |
||
| 120 | * @param integer $gravatar_type |
||
| 121 | * @param string $gravatar |
||
| 122 | * @param string $timezone |
||
| 123 | * @param string $description |
||
| 124 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 125 | */ |
||
| 126 | 24 | public function setUpOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Set up organization. |
||
| 144 | * @param string $name |
||
| 145 | * @param Organization $parent |
||
| 146 | * @param string $nickname |
||
| 147 | * @param integer $gravatar_type |
||
| 148 | * @param string $gravatar |
||
| 149 | * @param string $timezone |
||
| 150 | * @param string $description |
||
| 151 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
| 152 | */ |
||
| 153 | 3 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set up base organization. |
||
| 174 | * @param Organization $models |
||
| 175 | * @return boolean |
||
| 176 | * @throws InvalidConfigException |
||
| 177 | * @throws \Exception |
||
| 178 | */ |
||
| 179 | 24 | protected function setUpBaseOrganization($models) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Create organization. |
||
| 204 | * @param string $name |
||
| 205 | * @param Organization $parent |
||
| 206 | * @param string $nickname |
||
| 207 | * @param string $gravatar_type |
||
| 208 | * @param string $gravatar |
||
| 209 | * @param string $timezone |
||
| 210 | * @param string $description |
||
| 211 | * @return Organization |
||
| 212 | */ |
||
| 213 | 23 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Create department. |
||
| 220 | * @param string $name |
||
| 221 | * @param Organization $parent |
||
| 222 | * @param string $nickname |
||
| 223 | * @param string $gravatar_type |
||
| 224 | * @param string $gravatar |
||
| 225 | * @param string $timezone |
||
| 226 | * @param string $description |
||
| 227 | * @return Organization |
||
| 228 | */ |
||
| 229 | 1 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Create Base Organization. |
||
| 236 | * @param string $name |
||
| 237 | * @param Organization $parent |
||
| 238 | * @param string $nickname |
||
| 239 | * @param integer $gravatar_type |
||
| 240 | * @param string $gravatar |
||
| 241 | * @param string $timezone |
||
| 242 | * @param string $description |
||
| 243 | * @param integer $type |
||
| 244 | * @return Organization |
||
| 245 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
| 246 | * - The parent is itself. |
||
| 247 | * - The parent has already been its ancestor. |
||
| 248 | * - The current organization has reached the limit of ancestors. |
||
| 249 | */ |
||
| 250 | 23 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Revoke organization. |
||
| 272 | * @param static|string|integer $organization |
||
| 273 | * @param boolean $revokeIfHasChildren |
||
| 274 | * @throws InvalidParamException throw if current user is not the creator of organization. |
||
| 275 | */ |
||
| 276 | 1 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * |
||
| 310 | * @param Organization $organization |
||
| 311 | */ |
||
| 312 | 1 | public function isOrganizationCreator($organization) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | * @param Organization $organization |
||
| 324 | */ |
||
| 325 | 2 | public function isOrganizationAdministrator($organization) |
|
| 333 | } |
||
| 334 |
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.