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 |
||
45 | trait UserOrganizationTrait |
||
46 | { |
||
47 | /** |
||
48 | * @var string The organization class. |
||
49 | * Note: Please assign it with your own Organization class. |
||
50 | */ |
||
51 | public $organizationClass = Organization::class; |
||
52 | |||
53 | /** |
||
54 | * @var string The organization limit class. |
||
55 | * Note: Please assign it with your own OrganizationLimit class. |
||
56 | */ |
||
57 | public $organizationLimitClass = OrganizationLimit::class; |
||
58 | |||
59 | /** |
||
60 | * @var string The member class. |
||
61 | * Note: Please assign it with your own Member class. |
||
62 | */ |
||
63 | public $memberClass = Member::class; |
||
64 | private $noInitOrganizationLimit; |
||
65 | private $noInitOrganization; |
||
66 | private $noInitMember; |
||
67 | public $lastSetUpOrganization; |
||
68 | |||
69 | /** |
||
70 | * @return OrganizationLimit |
||
71 | */ |
||
72 | public function getNoInitOrganizationLimit() |
||
80 | /** |
||
81 | * @return Organization |
||
82 | */ |
||
83 | public function getNoInitOrganization() |
||
91 | /** |
||
92 | * @return Member |
||
93 | */ |
||
94 | 37 | public function getNoInitMember() |
|
102 | |||
103 | /** |
||
104 | * Get member query. |
||
105 | * @return MemberQuery |
||
106 | */ |
||
107 | 37 | public function getOfMembers() |
|
111 | |||
112 | /** |
||
113 | * Get query of member whose role is creator. |
||
114 | * @return MemberQuery |
||
115 | */ |
||
116 | 37 | public function getOfCreators() |
|
120 | |||
121 | /** |
||
122 | * Get query of member whose role is administrator. |
||
123 | * @return MemberQuery |
||
124 | */ |
||
125 | 2 | public function getOfAdministrators() |
|
129 | |||
130 | /** |
||
131 | * Get query of organization of which this user has been a member. |
||
132 | * If you access this method as magic-property `atOrganizations`, you will |
||
133 | * get all organizations the current user has joined in. |
||
134 | * @return OrganizationQuery |
||
135 | */ |
||
136 | 12 | public function getAtOrganizations() |
|
140 | |||
141 | /** |
||
142 | * |
||
143 | * @return OrganizationQuery |
||
144 | */ |
||
145 | public function getAtOrganizationsOnly() |
||
149 | |||
150 | /** |
||
151 | * |
||
152 | * @return OrganizationQuery |
||
153 | */ |
||
154 | public function getAtDepartmentsOnly() |
||
158 | |||
159 | /** |
||
160 | * |
||
161 | * @return OrganizationQuery |
||
162 | */ |
||
163 | 37 | public function getCreatorsAtOrganizations() |
|
167 | |||
168 | /** |
||
169 | * |
||
170 | * @return OrganizationQuery |
||
171 | */ |
||
172 | 37 | public function getCreatorsAtOrganizationsOnly() |
|
176 | |||
177 | /** |
||
178 | * |
||
179 | * @return OrganizationQuery |
||
180 | */ |
||
181 | 2 | public function getAdministratorsAtOrganizations() |
|
185 | |||
186 | /** |
||
187 | * Get Organization Limit Query. |
||
188 | * @return BaseBlameableQuery |
||
189 | */ |
||
190 | public function getOrganizationLimit() |
||
197 | |||
198 | /** |
||
199 | * Set up organization. |
||
200 | * @param string $name |
||
201 | * @param string $nickname |
||
202 | * @param integer $gravatar_type |
||
203 | * @param string $gravatar |
||
204 | * @param string $timezone |
||
205 | * @param string $description |
||
206 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
207 | * @throws InvalidParamException |
||
208 | * @throws \Exception |
||
209 | */ |
||
210 | 37 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
229 | |||
230 | /** |
||
231 | * Set up organization. |
||
232 | * @param string $name Department name. |
||
233 | * @param Organization $parent Parent organization or department. |
||
234 | * @param string $nickname |
||
235 | * @param integer $gravatar_type |
||
236 | * @param string $gravatar |
||
237 | * @param string $timezone |
||
238 | * @param string $description |
||
239 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
240 | * @throws InvalidParamException |
||
241 | * @throws \Exception |
||
242 | */ |
||
243 | 8 | public function setUpDepartment($name, $parent, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
265 | |||
266 | /** |
||
267 | * Set up base organization. |
||
268 | * @param Organization $models |
||
269 | * @return boolean |
||
270 | * @throws InvalidConfigException |
||
271 | * @throws \Exception |
||
272 | */ |
||
273 | 37 | protected function setUpBaseOrganization($models) |
|
274 | { |
||
275 | 37 | $model = null; |
|
276 | 37 | $associatedModels = []; |
|
277 | 37 | if (is_array($models)) { |
|
278 | 2 | if (!array_key_exists(0, $models)) { |
|
279 | 2 | throw new InvalidConfigException('Invalid Organization Model.'); |
|
280 | } |
||
281 | $model = $models[0]; |
||
282 | $associatedModels = array_key_exists('associatedModels', $models) ? $models['associatedModels'] : []; |
||
283 | 36 | } elseif ($models instanceof $this->organizationClass) { |
|
284 | 36 | $model = $models; |
|
285 | } |
||
286 | 36 | $result = $model->register($associatedModels); |
|
287 | 36 | if ($result instanceof \Exception) { |
|
288 | throw $result; |
||
289 | } |
||
290 | 36 | if ($result !== true) { |
|
291 | throw new \Exception('Failed to set up.'); |
||
292 | } |
||
293 | 36 | return true; |
|
294 | } |
||
295 | |||
296 | /** |
||
297 | * Create organization. |
||
298 | * @param string $name |
||
299 | * @param Organization $parent |
||
300 | * @param string $nickname |
||
301 | * @param string $gravatar_type |
||
302 | * @param string $gravatar |
||
303 | * @param string $timezone |
||
304 | * @param string $description |
||
305 | * @return Organization |
||
306 | */ |
||
307 | 36 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
311 | |||
312 | /** |
||
313 | * Create department. |
||
314 | * @param string $name |
||
315 | * @param Organization $parent |
||
316 | * @param string $nickname |
||
317 | * @param string $gravatar_type |
||
318 | * @param string $gravatar |
||
319 | * @param string $timezone |
||
320 | * @param string $description |
||
321 | * @return Organization |
||
322 | */ |
||
323 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
327 | |||
328 | /** |
||
329 | * Create Base Organization. |
||
330 | * @param string $name |
||
331 | * @param Organization $parent |
||
332 | * @param string $nickname |
||
333 | * @param integer $gravatar_type |
||
334 | * @param string $gravatar |
||
335 | * @param string $timezone |
||
336 | * @param string $description |
||
337 | * @param integer $type |
||
338 | * @return Organization |
||
339 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
340 | * - The parent is itself. |
||
341 | * - The parent has already been its ancestor. |
||
342 | * - The current organization has reached the limit of ancestors. |
||
343 | */ |
||
344 | 36 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
363 | |||
364 | /** |
||
365 | * Revoke organization or department. |
||
366 | * @param Organization|string|integer $organization Organization or it's ID or GUID. |
||
367 | * @param boolean $revokeIfHasChildren True represents revoking organization if there are subordinates. |
||
368 | * @return boolean True if revocation is successful. |
||
369 | * @throws InvalidParamException throws if organization is invalid. |
||
370 | * @throws \Exception |
||
371 | * @throws RevokePreventedException throws if $revokeIfHasChildren is false, at the |
||
372 | * same time the current organization or department has subordinates. |
||
373 | * @throws @var:$organization@mtd:deregister |
||
374 | */ |
||
375 | 11 | public function revokeOrganization($organization, $revokeIfHasChildren = true) |
|
412 | |||
413 | /** |
||
414 | * Check whether current user is organization or department creator. |
||
415 | * @param Organization $organization |
||
416 | * @return boolean True if current is organization or department creator. |
||
417 | */ |
||
418 | 16 | public function isOrganizationCreator($organization) |
|
426 | |||
427 | /** |
||
428 | * Check whether current user is organization or department administrator. |
||
429 | * @param Organization $organization |
||
430 | * @return boolean True if current is organization or department administrator. |
||
431 | */ |
||
432 | 5 | public function isOrganizationAdministrator($organization) |
|
440 | |||
441 | /** |
||
442 | * Attach events associated with organization. |
||
443 | */ |
||
444 | 38 | public function initOrganizationEvents() |
|
448 | |||
449 | /** |
||
450 | * Revoke Organization Event. |
||
451 | * It should be triggered when deleting (not deregistering). |
||
452 | * @param Event $event |
||
453 | */ |
||
454 | 17 | public function onRevokeOrganizationsByCreator($event) |
|
464 | |||
465 | /** |
||
466 | * Check whether the current user has reached the upper limit of organizations. |
||
467 | * @return boolean the upper limit of organizations which current could be set up. |
||
468 | */ |
||
469 | 37 | public function hasReachedOrganizationLimit() |
|
482 | } |
||
483 |
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.