Complex classes like UserRelationTrait 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 UserRelationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | trait UserRelationTrait |
||
36 | { |
||
37 | use mb { |
||
38 | mb::addBlame as addGroup; |
||
39 | mb::createBlame as createGroup; |
||
40 | mb::addOrCreateBlame as addOrCreateGroup; |
||
41 | mb::removeBlame as removeGroup; |
||
42 | mb::removeAllBlames as removeAllGroups; |
||
43 | mb::getBlame as getGroup; |
||
44 | mb::getOrCreateBlame as getOrCreateGroup; |
||
45 | mb::getBlameds as getGroupMembers; |
||
46 | mb::getBlameGuids as getGroupGuids; |
||
47 | mb::setBlameGuids as setGroupGuids; |
||
48 | mb::getAllBlames as getAllGroups; |
||
49 | mb::getNonBlameds as getNonGroupMembers; |
||
50 | mb::getBlamesCount as getGroupsCount; |
||
51 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
52 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @var string the another party of the relation. |
||
57 | */ |
||
58 | public $otherGuidAttribute = 'other_guid'; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | public $remarkAttribute = 'remark'; |
||
64 | public static $relationSingle = 0; |
||
65 | public static $relationMutual = 1; |
||
66 | public $relationType = 1; |
||
67 | public $relationTypes = [ |
||
68 | 0 => 'Single', |
||
69 | 1 => 'Mutual', |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * @var string the attribute name of which determines the relation type. |
||
74 | */ |
||
75 | public $mutualTypeAttribute = 'type'; |
||
76 | public static $mutualTypeNormal = 0x00; |
||
77 | public static $mutualTypeSuspend = 0x01; |
||
78 | |||
79 | /** |
||
80 | * @var array Mutual types. |
||
81 | */ |
||
82 | public static $mutualTypes = [ |
||
83 | 0x00 => 'Normal', |
||
84 | 0x01 => 'Suspend', |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * @var string the attribute name of which determines the `favorite` field. |
||
89 | */ |
||
90 | public $favoriteAttribute = 'favorite'; |
||
91 | |||
92 | /** |
||
93 | * Get whether this relation is favorite or not. |
||
94 | * @return boolean |
||
95 | */ |
||
96 | 1 | public function getIsFavorite() |
|
101 | |||
102 | /** |
||
103 | * Set favorite. |
||
104 | * @param boolean $fav |
||
105 | */ |
||
106 | 1 | public function setIsFavorite($fav) |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 8 | public function rules() |
|
119 | |||
120 | /** |
||
121 | * Validation rules associated with user relation. |
||
122 | * @return array rules. |
||
123 | */ |
||
124 | 8 | public function getUserRelationRules() |
|
135 | |||
136 | /** |
||
137 | * Get remark. |
||
138 | * @return string remark. |
||
139 | */ |
||
140 | public function getRemark() |
||
145 | |||
146 | /** |
||
147 | * Set remark. |
||
148 | * @param string $remark |
||
149 | * @return string remark. |
||
150 | */ |
||
151 | public function setRemark($remark) |
||
156 | |||
157 | /** |
||
158 | * Validation rules associated with remark attribute. |
||
159 | * @return array rules. |
||
160 | */ |
||
161 | 8 | public function getRemarkRules() |
|
168 | |||
169 | /** |
||
170 | * Validation rules associated with favorites attribute. |
||
171 | * @return array rules. |
||
172 | */ |
||
173 | 8 | public function getFavoriteRules() |
|
180 | |||
181 | /** |
||
182 | * Validation rules associated with other guid attribute. |
||
183 | * @return array rules. |
||
184 | */ |
||
185 | 8 | public function getOtherGuidRules() |
|
194 | |||
195 | /** |
||
196 | * Attach events associated with user relation. |
||
197 | */ |
||
198 | 8 | public function initUserRelationEvents() |
|
208 | |||
209 | /** |
||
210 | * Get opposite relation to self. |
||
211 | * @return \vistart\Models\models\BaseUserRelationModel |
||
212 | */ |
||
213 | 7 | public function getOpposite() |
|
222 | |||
223 | /** |
||
224 | * Build a suspend mutual relation, not support single relation. |
||
225 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
226 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
227 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
228 | * given if exists, or return a new relation. |
||
229 | */ |
||
230 | 1 | public static function buildSuspendRelation($user, $other) |
|
237 | |||
238 | /** |
||
239 | * Build a normal relation. |
||
240 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
241 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
242 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
243 | * given if exists, or return a new relation. |
||
244 | */ |
||
245 | 8 | public static function buildNormalRelation($user, $other) |
|
254 | |||
255 | /** |
||
256 | * Build relation between initiator and recipient. |
||
257 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
258 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
259 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
260 | * given if exists, or return a new relation. |
||
261 | */ |
||
262 | 8 | protected static function buildRelation($user, $other) |
|
282 | |||
283 | /** |
||
284 | * Build opposite relation throughout the current relation. The opposite |
||
285 | * relation will be given if existed. |
||
286 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
287 | * @return \vistart\Models\models\BaseUserRelationModel |
||
288 | */ |
||
289 | 7 | protected static function buildOppositeRelation($relation) |
|
305 | |||
306 | /** |
||
307 | * Remove myself. |
||
308 | * @return integer|false The number of relations removed, or false if the remove |
||
309 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
310 | * removed is 0, even though the remove execution is successful. |
||
311 | */ |
||
312 | 2 | public function remove() |
|
316 | |||
317 | /** |
||
318 | * Remove first relation between initiator(s) and recipient(s). |
||
319 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
320 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
321 | * @return integer|false The number of relations removed. |
||
322 | */ |
||
323 | 1 | public static function removeOneRelation($user, $other) |
|
327 | |||
328 | /** |
||
329 | * Remove all relations between initiator(s) and recipient(s). |
||
330 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
331 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
332 | * @return integer The number of relations removed. |
||
333 | */ |
||
334 | 2 | public static function removeAllRelations($user, $other) |
|
341 | |||
342 | /** |
||
343 | * Get first relation between initiator(s) and recipient(s). |
||
344 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
345 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
346 | * @return \vistart\Models\models\BaseUserRelationModel |
||
347 | */ |
||
348 | 1 | public static function findOneRelation($user, $other) |
|
352 | |||
353 | /** |
||
354 | * Get first opposite relation between initiator(s) and recipient(s). |
||
355 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
356 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
357 | * @return \vistart\Models\models\BaseUserRelationModel |
||
358 | */ |
||
359 | 7 | public static function findOneOppositeRelation($user, $other) |
|
363 | |||
364 | /** |
||
365 | * Get user's or users' all relations, or by specified groups. |
||
366 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
367 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
368 | * @return array all eligible relations |
||
369 | */ |
||
370 | public static function findOnesAllRelations($user, $groups = null) |
||
374 | |||
375 | /** |
||
376 | * Initialize groups attribute. |
||
377 | * @param \yii\base\Event $event |
||
378 | */ |
||
379 | 8 | public function onInitGroups($event) |
|
384 | |||
385 | /** |
||
386 | * Initialize remark attribute. |
||
387 | * @param \yii\base\Event $event |
||
388 | */ |
||
389 | 8 | public function onInitRemark($event) |
|
395 | |||
396 | /** |
||
397 | * The event triggered after insert new relation. |
||
398 | * The opposite relation should be inserted without triggering events |
||
399 | * simultaneously after new relation inserted, |
||
400 | * @param \yii\base\Event $event |
||
401 | */ |
||
402 | 8 | public function onInsertRelation($event) |
|
414 | |||
415 | /** |
||
416 | * The event triggered after update relation. |
||
417 | * The opposite relation should be updated without triggering events |
||
418 | * simultaneously after existed relation removed. |
||
419 | * @param \yii\base\Event $event |
||
420 | */ |
||
421 | 3 | public function onUpdateRelation($event) |
|
433 | |||
434 | /** |
||
435 | * The event triggered after delete relation. |
||
436 | * The opposite relation should be deleted without triggering events |
||
437 | * simultaneously after existed relation removed. |
||
438 | * @param \yii\base\Event $event |
||
439 | */ |
||
440 | 2 | public function onDeleteRelation($event) |
|
451 | } |
||
452 |
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.