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 |
||
49 | trait UserOrganizationTrait |
||
50 | { |
||
51 | /** |
||
52 | * @var string The organization class. |
||
53 | * Note: Please assign it with your own Organization class. |
||
54 | */ |
||
55 | public $organizationClass = Organization::class; |
||
56 | |||
57 | /** |
||
58 | * @var string The organization limit class. |
||
59 | * Note: Please assign it with your own OrganizationLimit class. |
||
60 | */ |
||
61 | public $organizationLimitClass = OrganizationLimit::class; |
||
62 | private $noInitOrganizationLimit; |
||
63 | private $noInitOrganization; |
||
64 | public $lastSetUpOrganization; |
||
65 | |||
66 | /** |
||
67 | * @return OrganizationLimit |
||
68 | */ |
||
69 | 51 | public function getNoInitOrganizationLimit() |
|
70 | { |
||
71 | 51 | if (!$this->noInitOrganizationLimit) { |
|
72 | 51 | $class = $this->organizationLimitClass; |
|
73 | 51 | $this->noInitOrganizationLimit = $class::buildNoInitModel(); |
|
74 | } |
||
75 | 51 | return $this->noInitOrganizationLimit; |
|
76 | } |
||
77 | /** |
||
78 | * @return Organization |
||
79 | */ |
||
80 | 51 | public function getNoInitOrganization() |
|
88 | /** |
||
89 | * @return Member |
||
90 | */ |
||
91 | 51 | public function getNoInitMember() |
|
95 | |||
96 | /** |
||
97 | * Get member query. |
||
98 | * @return MemberQuery |
||
99 | */ |
||
100 | 51 | public function getOfMembers() |
|
104 | |||
105 | /** |
||
106 | * Get query of member whose role is creator. |
||
107 | * @return MemberQuery |
||
108 | */ |
||
109 | 51 | public function getOfCreators() |
|
113 | |||
114 | /** |
||
115 | * Get query of member whose role is administrator. |
||
116 | * @return MemberQuery |
||
117 | */ |
||
118 | 4 | public function getOfAdministrators() |
|
122 | |||
123 | /** |
||
124 | * Get query of organization of which this user has been a member. |
||
125 | * If you access this method as magic-property `atOrganizations`, you will |
||
126 | * get all organizations the current user has joined in. |
||
127 | * @return OrganizationQuery |
||
128 | */ |
||
129 | 50 | public function getAtOrganizations() |
|
133 | |||
134 | /** |
||
135 | * |
||
136 | * @return OrganizationQuery |
||
137 | */ |
||
138 | 2 | public function getAtOrganizationsOnly() |
|
142 | |||
143 | /** |
||
144 | * |
||
145 | * @return OrganizationQuery |
||
146 | */ |
||
147 | 1 | public function getAtDepartmentsOnly() |
|
151 | |||
152 | /** |
||
153 | * |
||
154 | * @return OrganizationQuery |
||
155 | */ |
||
156 | 51 | public function getCreatorsAtOrganizations() |
|
160 | |||
161 | /** |
||
162 | * |
||
163 | * @return OrganizationQuery |
||
164 | */ |
||
165 | 51 | public function getCreatorsAtOrganizationsOnly() |
|
169 | |||
170 | /** |
||
171 | * |
||
172 | * @return OrganizationQuery |
||
173 | */ |
||
174 | 4 | public function getAdministratorsAtOrganizations() |
|
178 | |||
179 | /** |
||
180 | * @return OrganizationQuery |
||
181 | */ |
||
182 | 2 | public function getAdministratorsAtOrganizationsOnly() |
|
186 | |||
187 | /** |
||
188 | * Get Organization Limit Query. |
||
189 | * @return BaseBlameableQuery |
||
190 | */ |
||
191 | 51 | public function getOrganizationLimit() |
|
192 | { |
||
193 | 51 | if (empty($this->organizationLimitClass)) { |
|
194 | return null; |
||
195 | } |
||
196 | 51 | return $this->hasOne($this->organizationLimitClass, [$this->getNoInitOrganizationLimit()->createdByAttribute => $this->guidAttribute]); |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Set up organization. |
||
201 | * @param string $name |
||
202 | * @param string $nickname |
||
203 | * @param integer $gravatar_type |
||
204 | * @param string $gravatar |
||
205 | * @param string $timezone |
||
206 | * @param string $description |
||
207 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
208 | * @throws InvalidParamException |
||
209 | * @throws \Exception |
||
210 | */ |
||
211 | 51 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
230 | |||
231 | /** |
||
232 | * Set up organization. |
||
233 | * @param string $name Department name. |
||
234 | * @param Organization $parent Parent organization or department. |
||
235 | * @param string $nickname |
||
236 | * @param integer $gravatar_type |
||
237 | * @param string $gravatar |
||
238 | * @param string $timezone |
||
239 | * @param string $description |
||
240 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
241 | * @throws InvalidParamException |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | 20 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
266 | |||
267 | /** |
||
268 | * Set up base organization. |
||
269 | * @param Organization $models |
||
270 | * @return boolean |
||
271 | * @throws InvalidConfigException |
||
272 | * @throws \Exception |
||
273 | */ |
||
274 | 51 | protected function setUpBaseOrganization($models) |
|
296 | |||
297 | /** |
||
298 | * Create organization. |
||
299 | * @param string $name |
||
300 | * @param Organization $parent |
||
301 | * @param string $nickname |
||
302 | * @param string $gravatar_type |
||
303 | * @param string $gravatar |
||
304 | * @param string $timezone |
||
305 | * @param string $description |
||
306 | * @return Organization |
||
307 | */ |
||
308 | 50 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
312 | |||
313 | /** |
||
314 | * Create department. |
||
315 | * @param string $name |
||
316 | * @param Organization $parent |
||
317 | * @param string $nickname |
||
318 | * @param string $gravatar_type |
||
319 | * @param string $gravatar |
||
320 | * @param string $timezone |
||
321 | * @param string $description |
||
322 | * @return Organization |
||
323 | */ |
||
324 | 18 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
328 | |||
329 | /** |
||
330 | * Create Base Organization. |
||
331 | * @param string $name |
||
332 | * @param Organization $parent |
||
333 | * @param string $nickname |
||
334 | * @param integer $gravatar_type |
||
335 | * @param string $gravatar |
||
336 | * @param string $timezone |
||
337 | * @param string $description |
||
338 | * @param integer $type |
||
339 | * @return Organization |
||
340 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
341 | * - The parent is itself. |
||
342 | * - The parent has already been its ancestor. |
||
343 | * - The current organization has reached the limit of ancestors. |
||
344 | */ |
||
345 | 50 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
364 | |||
365 | /** |
||
366 | * Revoke organization or department. |
||
367 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
368 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
369 | * @return boolean True if revocation is successful. |
||
370 | * @throws InvalidParamException throws if organization is invalid. |
||
371 | * @throws \Exception |
||
372 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
373 | * same time the current organization or department has subordinates. |
||
374 | * @throws @var:$organization@mtd:deregister |
||
375 | */ |
||
376 | 14 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
413 | |||
414 | /** |
||
415 | * Check whether current user is organization or department creator. |
||
416 | * @param Organization $organization |
||
417 | * @return boolean True if current is organization or department creator. |
||
418 | */ |
||
419 | 29 | public function isOrganizationCreator($organization) |
|
427 | |||
428 | /** |
||
429 | * Check whether current user is organization or department administrator. |
||
430 | * @param Organization $organization |
||
431 | * @return boolean True if current is organization or department administrator. |
||
432 | */ |
||
433 | 14 | public function isOrganizationAdministrator($organization) |
|
441 | |||
442 | /** |
||
443 | * Attach events associated with organization. |
||
444 | */ |
||
445 | 52 | public function initOrganizationEvents() |
|
449 | |||
450 | /** |
||
451 | * Revoke Organization Event. |
||
452 | * It should be triggered when deleting (not deregistering). |
||
453 | * @param Event $event |
||
454 | */ |
||
455 | 22 | public function onRevokeOrganizationsByCreator($event) |
|
465 | |||
466 | /** |
||
467 | * Check whether the current user has reached the upper limit of organizations. |
||
468 | * @return boolean the upper limit of organizations which current could be set up. |
||
469 | */ |
||
470 | 51 | public function hasReachedOrganizationLimit() |
|
478 | |||
479 | /** |
||
480 | * Get the remaining places of organizations. |
||
481 | * @return bool|int False if no limit. |
||
482 | */ |
||
483 | 51 | public function getRemainingOrganizationPlaces() |
|
496 | } |
||
497 |
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.