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 |
||
40 | trait UserOrganizationTrait |
||
41 | { |
||
42 | public $organizationClass = Organization::class; |
||
43 | public $memberClass = Member::class; |
||
44 | private $noInitOrganization; |
||
45 | private $noInitMember; |
||
46 | public $lastSetUpOrganization; |
||
47 | /** |
||
48 | * @return Organization |
||
49 | */ |
||
50 | protected function getNoInitOrganization() |
||
58 | /** |
||
59 | * @return Member |
||
60 | */ |
||
61 | 28 | protected function getNoInitMember() |
|
69 | |||
70 | /** |
||
71 | * |
||
72 | * @return MemberQuery |
||
73 | */ |
||
74 | 28 | public function getOfMembers() |
|
78 | |||
79 | /** |
||
80 | * |
||
81 | * @return MemberQuery |
||
82 | */ |
||
83 | 16 | public function getOfCreators() |
|
87 | |||
88 | /** |
||
89 | * |
||
90 | * @return MemberQuery |
||
91 | */ |
||
92 | 2 | public function getOfAdministrators() |
|
96 | |||
97 | /** |
||
98 | * Get query of organization of which this user has been a member. |
||
99 | * If you access this method as magic-property `atOrganizations`, you will |
||
100 | * get all organizations the current user has joined in. |
||
101 | * @return OrganizationQuery |
||
102 | */ |
||
103 | 12 | public function getAtOrganizations() |
|
107 | |||
108 | /** |
||
109 | * |
||
110 | * @return OrganizationQuery |
||
111 | */ |
||
112 | 16 | public function getCreatorsAtOrganizations() |
|
116 | |||
117 | /** |
||
118 | * |
||
119 | * @return OrganizationQuery |
||
120 | */ |
||
121 | 2 | public function getAdministratorsAtOrganizations() |
|
125 | |||
126 | /** |
||
127 | * Set up organization. |
||
128 | * @param string $name |
||
129 | * @param string $nickname |
||
130 | * @param integer $gravatar_type |
||
131 | * @param string $gravatar |
||
132 | * @param string $timezone |
||
133 | * @param string $description |
||
134 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
135 | * @throws InvalidParamException |
||
136 | * @throws \Exception |
||
137 | */ |
||
138 | 35 | public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
157 | |||
158 | /** |
||
159 | * Set up organization. |
||
160 | * @param string $name |
||
161 | * @param Organization $parent |
||
162 | * @param string $nickname |
||
163 | * @param integer $gravatar_type |
||
164 | * @param string $gravatar |
||
165 | * @param string $timezone |
||
166 | * @param string $description |
||
167 | * @return boolean Whether indicate the setting-up succeeded or not. |
||
168 | * @throws InvalidParamException |
||
169 | * @throws \Exception |
||
170 | */ |
||
171 | 8 | public function setUpDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
193 | |||
194 | /** |
||
195 | * Set up base organization. |
||
196 | * @param Organization $models |
||
197 | * @return boolean |
||
198 | * @throws InvalidConfigException |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | 35 | protected function setUpBaseOrganization($models) |
|
202 | { |
||
203 | 35 | $model = null; |
|
204 | 35 | $associatedModels = []; |
|
205 | 35 | if (is_array($models)) { |
|
206 | 2 | if (!array_key_exists(0, $models)) { |
|
207 | 2 | throw new InvalidConfigException('Invalid Organization Model.'); |
|
208 | } |
||
209 | $model = $models[0]; |
||
210 | $associatedModels = array_key_exists('associatedModels', $models) ? $models['associatedModels'] : []; |
||
211 | 34 | } elseif ($models instanceof $this->organizationClass) { |
|
212 | 34 | $model = $models; |
|
213 | } |
||
214 | 34 | $result = $model->register($associatedModels); |
|
215 | 34 | if ($result instanceof \Exception) { |
|
216 | throw $result; |
||
217 | } |
||
218 | 34 | if ($result !== true) { |
|
219 | throw new \Exception('Failed to set up.'); |
||
220 | } |
||
221 | 34 | return true; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * Create organization. |
||
226 | * @param string $name |
||
227 | * @param Organization $parent |
||
228 | * @param string $nickname |
||
229 | * @param string $gravatar_type |
||
230 | * @param string $gravatar |
||
231 | * @param string $timezone |
||
232 | * @param string $description |
||
233 | * @return Organization |
||
234 | */ |
||
235 | 34 | public function createOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
239 | |||
240 | /** |
||
241 | * Create department. |
||
242 | * @param string $name |
||
243 | * @param Organization $parent |
||
244 | * @param string $nickname |
||
245 | * @param string $gravatar_type |
||
246 | * @param string $gravatar |
||
247 | * @param string $timezone |
||
248 | * @param string $description |
||
249 | * @return Organization |
||
250 | */ |
||
251 | 6 | public function createDepartment($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
255 | |||
256 | /** |
||
257 | * Create Base Organization. |
||
258 | * @param string $name |
||
259 | * @param Organization $parent |
||
260 | * @param string $nickname |
||
261 | * @param integer $gravatar_type |
||
262 | * @param string $gravatar |
||
263 | * @param string $timezone |
||
264 | * @param string $description |
||
265 | * @param integer $type |
||
266 | * @return Organization |
||
267 | * @throws InvalidParamException throw if setting parent failed. Possible reasons include: |
||
268 | * - The parent is itself. |
||
269 | * - The parent has already been its ancestor. |
||
270 | * - The current organization has reached the limit of ancestors. |
||
271 | */ |
||
272 | 34 | protected function createBaseOrganization($name, $parent = null, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '', $type = Organization::TYPE_ORGANIZATION) |
|
273 | { |
||
274 | 34 | $class = $this->organizationClass; |
|
275 | $profileConfig = [ |
||
276 | 34 | 'name' => $name, |
|
277 | 34 | 'nickname' => $nickname, |
|
278 | 34 | 'gravatar_type' => $gravatar_type, |
|
279 | 34 | 'gravatar' => $gravatar, |
|
280 | 34 | 'timezone' => $timezone, |
|
281 | 34 | 'description' => $description, |
|
282 | ]; |
||
283 | 34 | $organization = new $class(['type' => $type, 'creatorModel' => $this, 'profileConfig' => $profileConfig]); |
|
284 | 34 | if (empty($parent)) { |
|
285 | 34 | $organization->setNullParent(); |
|
286 | 6 | } elseif ($organization->setParent($parent) === false) { |
|
287 | throw new InvalidParamException("Failed to set parent."); |
||
288 | } |
||
289 | 34 | return $organization; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * Revoke organization or department. |
||
294 | * @param static|string|integer $organization |
||
295 | * @param boolean $revokeIfHasChildren |
||
296 | * @throws InvalidParamException throw if current user is not the creator of organization. |
||
297 | */ |
||
298 | 9 | public function revokeOrganization($organization, $revokeIfHasChildren = false) |
|
299 | { |
||
300 | 9 | if (!($organization instanceof $this->organizationClass)) |
|
301 | { |
||
302 | 2 | $class = $this->organizationClass; |
|
303 | 2 | if (is_numeric($organization)) { |
|
304 | 1 | $organization = $class::find()->id($organization)->one(); |
|
305 | 1 | } elseif (is_string($organization)) { |
|
306 | 1 | $organization = $class::find()->guid($organization)->one(); |
|
307 | } |
||
308 | } |
||
309 | 9 | if (!($organization instanceof $this->organizationClass)) { |
|
310 | throw new InvalidParamException('Invalid Organization.'); |
||
311 | } |
||
312 | 9 | if (!Yii::$app->authManager->checkAccess( |
|
313 | $this, |
||
314 | 9 | $organization->type == Organization::TYPE_ORGANIZATION ? (new RevokeOrganization)->name : (new RevokeDepartment)->name, |
|
315 | 9 | ['organization' => $organization])) { |
|
316 | 1 | throw new InvalidParamException("You do not have permission to revoke it."); |
|
317 | } |
||
318 | 8 | $transaction = Yii::$app->db->beginTransaction(); |
|
319 | try { |
||
320 | 8 | $result = $organization->deregister(); |
|
321 | 8 | if ($result instanceof \Exception){ |
|
322 | throw $result; |
||
323 | } |
||
324 | 8 | if ($result !== true) { |
|
325 | throw new InvalidParamException("Failed to revoke."); |
||
326 | } |
||
327 | 8 | $transaction->commit(); |
|
328 | } catch (\Exception $ex) { |
||
329 | $transaction->rollBack(); |
||
330 | Yii::error($ex->getMessage(), __METHOD__); |
||
331 | throw $ex; |
||
332 | } |
||
333 | 8 | return true; |
|
334 | } |
||
335 | |||
336 | /** |
||
337 | * |
||
338 | * @param Organization $organization |
||
339 | */ |
||
340 | 14 | public function isOrganizationCreator($organization) |
|
348 | |||
349 | /** |
||
350 | * |
||
351 | * @param Organization $organization |
||
352 | */ |
||
353 | 5 | public function isOrganizationAdministrator($organization) |
|
361 | |||
362 | /** |
||
363 | * Attach events associated with organization. |
||
364 | */ |
||
365 | 36 | public function initOrganizationEvents() |
|
369 | |||
370 | /** |
||
371 | * Revoke Organization Event. |
||
372 | * It should be triggered when deleting (not deregistering). |
||
373 | * @param Event $event |
||
374 | */ |
||
375 | 15 | public function onRevokeOrganizationsByCreator($event) |
|
385 | } |
||
386 |
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.