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 |
||
34 | trait UserRelationTrait |
||
35 | { |
||
36 | use mb { |
||
37 | mb::addBlame as addGroup; |
||
38 | mb::removeBlame as removeGroup; |
||
39 | mb::removeAllBlames as removeAllGroups; |
||
40 | mb::getBlame as getGroup; |
||
41 | mb::getBlameds as getGroupMembers; |
||
42 | mb::getBlameGuids as getGroupGuids; |
||
43 | mb::setBlameGuids as setGroupGuids; |
||
44 | mb::getAllBlames as getAllGroups; |
||
45 | mb::getNonBlameds as getNonGroupMembers; |
||
46 | mb::getBlamesCount as getGroupsCount; |
||
47 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
48 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @var string the another party of the relation. |
||
53 | */ |
||
54 | public $otherGuidAttribute = 'other_guid'; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | public $remarkAttribute = 'remark'; |
||
60 | public static $relationSingle = 0; |
||
61 | public static $relationMutual = 1; |
||
62 | public $relationType = 1; |
||
63 | public $relationTypes = [ |
||
64 | 0 => 'Single', |
||
65 | 1 => 'Mutual', |
||
66 | ]; |
||
67 | |||
68 | /** |
||
69 | * @var string the attribute name of which determines the relation type. |
||
70 | */ |
||
71 | public $mutualTypeAttribute = 'type'; |
||
72 | public static $mutualTypeNormal = 0x00; |
||
73 | public static $mutualTypeSuspend = 0x01; |
||
74 | |||
75 | /** |
||
76 | * @var array Mutual types. |
||
77 | */ |
||
78 | public static $mutualTypes = [ |
||
79 | 0x00 => 'Normal', |
||
80 | 0x01 => 'Suspend', |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * @var string the attribute name of which determines the `favorite` field. |
||
85 | */ |
||
86 | public $favoriteAttribute = 'favorite'; |
||
87 | |||
88 | /** |
||
89 | * Get whether this relation is favorite or not. |
||
90 | * @return boolean |
||
91 | */ |
||
92 | 1 | public function getIsFavorite() |
|
97 | |||
98 | /** |
||
99 | * Set favorite. |
||
100 | * @param boolean $fav |
||
101 | */ |
||
102 | 1 | public function setIsFavorite($fav) |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 7 | public function rules() |
|
115 | |||
116 | /** |
||
117 | * Validation rules associated with user relation. |
||
118 | * @return array rules. |
||
119 | */ |
||
120 | 7 | public function getUserRelationRules() |
|
131 | |||
132 | /** |
||
133 | * Get remark. |
||
134 | * @return string remark. |
||
135 | */ |
||
136 | public function getRemark() |
||
141 | |||
142 | /** |
||
143 | * Set remark. |
||
144 | * @param string $remark |
||
145 | * @return string remark. |
||
146 | */ |
||
147 | public function setRemark($remark) |
||
152 | |||
153 | /** |
||
154 | * Validation rules associated with remark attribute. |
||
155 | * @return array rules. |
||
156 | */ |
||
157 | 7 | public function getRemarkRules() |
|
164 | |||
165 | /** |
||
166 | * Validation rules associated with favorites attribute. |
||
167 | * @return array rules. |
||
168 | */ |
||
169 | 7 | public function getFavoriteRules() |
|
176 | |||
177 | /** |
||
178 | * Validation rules associated with other guid attribute. |
||
179 | * @return array rules. |
||
180 | */ |
||
181 | 7 | public function getOtherGuidRules() |
|
190 | |||
191 | /** |
||
192 | * Attach events associated with user relation. |
||
193 | */ |
||
194 | 7 | public function initUserRelationEvents() |
|
204 | |||
205 | /** |
||
206 | * Get opposite relation to self. |
||
207 | * @return \vistart\Models\models\BaseUserRelationModel |
||
208 | */ |
||
209 | 6 | public function getOpposite() |
|
210 | { |
||
211 | 6 | if ($this->isNewRecord) { |
|
212 | 1 | return null; |
|
213 | } |
||
214 | 6 | $createdByAttribute = $this->createdByAttribute; |
|
215 | 6 | $otherGuidAttribute = $this->otherGuidAttribute; |
|
216 | 6 | return static::find()->opposite($this->$createdByAttribute, $this->$otherGuidAttribute); |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Build a suspend mutual relation, not support single relation. |
||
221 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
222 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
223 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
224 | * given if exists, or return a new relation. |
||
225 | */ |
||
226 | 1 | public static function buildSuspendRelation($user, $other) |
|
233 | |||
234 | /** |
||
235 | * Build a normal relation. |
||
236 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
237 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
238 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
239 | * given if exists, or return a new relation. |
||
240 | */ |
||
241 | 7 | public static function buildNormalRelation($user, $other) |
|
250 | |||
251 | /** |
||
252 | * Build relation between initiator and recipient. |
||
253 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
254 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
255 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
256 | * given if exists, or return a new relation. |
||
257 | */ |
||
258 | 7 | protected static function buildRelation($user, $other) |
|
275 | |||
276 | /** |
||
277 | * Build opposite mutual relation throughout the current relation, not support |
||
278 | * single relation. The opposite relation will be given if existed. |
||
279 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
280 | * @return \vistart\Models\models\BaseUserRelationModel |
||
281 | */ |
||
282 | 6 | protected static function buildOppositeRelation($relation) |
|
294 | |||
295 | /** |
||
296 | * Remove myself. |
||
297 | * @return integer|false The number of relations removed, or false if the remove |
||
298 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
299 | * removed is 0, even though the remove execution is successful. |
||
300 | */ |
||
301 | 2 | public function remove() |
|
305 | |||
306 | /** |
||
307 | * Remove first relation between initiator(s) and recipient(s). |
||
308 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
309 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
310 | * @return integer|false The number of relations removed. |
||
311 | */ |
||
312 | 1 | public static function removeOneRelation($user, $other) |
|
316 | |||
317 | /** |
||
318 | * Remove all relations 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 The number of relations removed. |
||
322 | */ |
||
323 | 2 | public static function removeAllRelations($user, $other) |
|
330 | |||
331 | /** |
||
332 | * Get first relation between initiator(s) and recipient(s). |
||
333 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
334 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
335 | * @return \vistart\Models\models\BaseUserRelationModel |
||
336 | */ |
||
337 | 1 | public static function findOneRelation($user, $other) |
|
338 | { |
||
339 | 1 | return static::find()->initiators($user)->recipients($other)->one(); |
|
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get first opposite relation between initiator(s) and recipient(s). |
||
344 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
345 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
346 | * @return \vistart\Models\models\BaseUserRelationModel |
||
347 | */ |
||
348 | 6 | public static function findOneOppositeRelation($user, $other) |
|
352 | |||
353 | /** |
||
354 | * Get user's or users' all relations, or by specified groups. |
||
355 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
356 | * @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. |
||
357 | * @return array all eligible relations |
||
358 | */ |
||
359 | public static function findOnesAllRelations($user, $groups = null) |
||
363 | |||
364 | /** |
||
365 | * Initialize groups attribute. |
||
366 | * @param \yii\base\Event $event |
||
367 | */ |
||
368 | 7 | public function onInitGroups($event) |
|
373 | |||
374 | /** |
||
375 | * Initialize remark attribute. |
||
376 | * @param \yii\base\Event $event |
||
377 | */ |
||
378 | 7 | public function onInitRemark($event) |
|
384 | |||
385 | /** |
||
386 | * The event triggered after insert new relation. |
||
387 | * The opposite relation should be inserted without triggering events |
||
388 | * simultaneously after new relation inserted, |
||
389 | * @param \yii\base\Event $event |
||
390 | */ |
||
391 | 7 | public function onInsertRelation($event) |
|
403 | |||
404 | /** |
||
405 | * The event triggered after update relation. |
||
406 | * The opposite relation should be updated without triggering events |
||
407 | * simultaneously after existed relation removed. |
||
408 | * @param \yii\base\Event $event |
||
409 | */ |
||
410 | 3 | public function onUpdateRelation($event) |
|
422 | |||
423 | /** |
||
424 | * The event triggered after delete relation. |
||
425 | * The opposite relation should be deleted without triggering events |
||
426 | * simultaneously after existed relation removed. |
||
427 | * @param \yii\base\Event $event |
||
428 | */ |
||
429 | 2 | public function onDeleteRelation($event) |
|
440 | } |
||
441 |
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.