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 | 25 | protected function getNoInitMember() |
|
68 | |||
69 | /** |
||
70 | * |
||
71 | * @return MemberQuery |
||
72 | */ |
||
73 | 25 | 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 | 10 | 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 | 32 | 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 | 5 | 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 | 32 | protected function setUpBaseOrganization($models) |
|
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 | 31 | 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 | 3 | 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 | 31 | 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 | 8 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
336 | |||
337 | /** |
||
338 | * |
||
339 | * @param Organization $organization |
||
340 | */ |
||
341 | 8 | public function isOrganizationCreator($organization) |
|
349 | |||
350 | /** |
||
351 | * |
||
352 | * @param Organization $organization |
||
353 | */ |
||
354 | 2 | public function isOrganizationAdministrator($organization) |
|
362 | } |
||
363 |
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.