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 |
||
41 | trait UserOrganizationTrait |
||
42 | { |
||
43 | public $organizationClass = Organization::class; |
||
44 | public $memberClass = Member::class; |
||
45 | private $noInitOrganization; |
||
46 | private $noInitMember; |
||
47 | public $lastSetUpOrganization; |
||
48 | /** |
||
49 | * @return Organization |
||
50 | */ |
||
51 | protected function getNoInitOrganization() |
||
59 | /** |
||
60 | * @return Member |
||
61 | */ |
||
62 | 28 | protected function getNoInitMember() |
|
70 | |||
71 | /** |
||
72 | * Get member query. |
||
73 | * @return MemberQuery |
||
74 | */ |
||
75 | 28 | public function getOfMembers() |
|
79 | |||
80 | /** |
||
81 | * Get query of member whose role is creator. |
||
82 | * @return MemberQuery |
||
83 | */ |
||
84 | 16 | public function getOfCreators() |
|
88 | |||
89 | /** |
||
90 | * Get query of member whose role is administrator. |
||
91 | * @return MemberQuery |
||
92 | */ |
||
93 | 2 | public function getOfAdministrators() |
|
97 | |||
98 | /** |
||
99 | * Get query of organization of which this user has been a member. |
||
100 | * If you access this method as magic-property `atOrganizations`, you will |
||
101 | * get all organizations the current user has joined in. |
||
102 | * @return OrganizationQuery |
||
103 | */ |
||
104 | 12 | public function getAtOrganizations() |
|
108 | |||
109 | /** |
||
110 | * |
||
111 | * @return OrganizationQuery |
||
112 | */ |
||
113 | 16 | public function getCreatorsAtOrganizations() |
|
117 | |||
118 | /** |
||
119 | * |
||
120 | * @return OrganizationQuery |
||
121 | */ |
||
122 | 2 | public function getAdministratorsAtOrganizations() |
|
126 | |||
127 | /** |
||
128 | * Set up organization. |
||
129 | * @param string $name |
||
130 | * @param string $nickname |
||
131 | * @param integer $gravatar_type |
||
132 | * @param string $gravatar |
||
133 | * @param string $timezone |
||
134 | * @param string $description |
||
135 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
136 | * @throws InvalidParamException |
||
137 | * @throws \Exception |
||
138 | */ |
||
139 | 35 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
158 | |||
159 | /** |
||
160 | * Set up organization. |
||
161 | * @param string $name Department name. |
||
162 | * @param Organization $parent Parent organization or department. |
||
163 | * @param string $nickname |
||
164 | * @param integer $gravatar_type |
||
165 | * @param string $gravatar |
||
166 | * @param string $timezone |
||
167 | * @param string $description |
||
168 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
169 | * @throws InvalidParamException |
||
170 | * @throws \Exception |
||
171 | */ |
||
172 | 8 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
194 | |||
195 | /** |
||
196 | * Set up base organization. |
||
197 | * @param Organization $models |
||
198 | * @return boolean |
||
199 | * @throws InvalidConfigException |
||
200 | * @throws \Exception |
||
201 | */ |
||
202 | 35 | protected function setUpBaseOrganization($models) |
|
224 | |||
225 | /** |
||
226 | * Create organization. |
||
227 | * @param string $name |
||
228 | * @param Organization $parent |
||
229 | * @param string $nickname |
||
230 | * @param string $gravatar_type |
||
231 | * @param string $gravatar |
||
232 | * @param string $timezone |
||
233 | * @param string $description |
||
234 | * @return Organization |
||
235 | */ |
||
236 | 34 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
240 | |||
241 | /** |
||
242 | * Create department. |
||
243 | * @param string $name |
||
244 | * @param Organization $parent |
||
245 | * @param string $nickname |
||
246 | * @param string $gravatar_type |
||
247 | * @param string $gravatar |
||
248 | * @param string $timezone |
||
249 | * @param string $description |
||
250 | * @return Organization |
||
251 | */ |
||
252 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
256 | |||
257 | /** |
||
258 | * Create Base Organization. |
||
259 | * @param string $name |
||
260 | * @param Organization $parent |
||
261 | * @param string $nickname |
||
262 | * @param integer $gravatar_type |
||
263 | * @param string $gravatar |
||
264 | * @param string $timezone |
||
265 | * @param string $description |
||
266 | * @param integer $type |
||
267 | * @return Organization |
||
268 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
269 | * - The parent is itself. |
||
270 | * - The parent has already been its ancestor. |
||
271 | * - The current organization has reached the limit of ancestors. |
||
272 | */ |
||
273 | 34 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
292 | |||
293 | /** |
||
294 | * Revoke organization or department. |
||
295 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
296 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
297 | * @return boolean True if revocation is successful. |
||
298 | * @throws InvalidParamException throws if organization is invalid. |
||
299 | * @throws \Exception |
||
300 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
301 | * same time the current organization or department has subordinates. |
||
302 | * @throws @var:$organization@mtd:deregister |
||
303 | */ |
||
304 | 9 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
345 | |||
346 | /** |
||
347 | * Check whether current user is organization or department creator. |
||
348 | * @param Organization $organization |
||
349 | * @return boolean True if current is organization or department creator. |
||
350 | */ |
||
351 | 14 | public function isOrganizationCreator($organization) |
|
359 | |||
360 | /** |
||
361 | * Check whether current user is organization or department administrator. |
||
362 | * @param Organization $organization |
||
363 | * @return boolean True if current is organization or department administrator. |
||
364 | */ |
||
365 | 5 | public function isOrganizationAdministrator($organization) |
|
373 | |||
374 | /** |
||
375 | * Attach events associated with organization. |
||
376 | */ |
||
377 | 36 | public function initOrganizationEvents() |
|
381 | |||
382 | /** |
||
383 | * Revoke Organization Event. |
||
384 | * It should be triggered when deleting (not deregistering). |
||
385 | * @param Event $event |
||
386 | */ |
||
387 | 15 | public function onRevokeOrganizationsByCreator($event) |
|
397 | } |
||
398 |
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
Idable
provides a methodequalsId
that 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.