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 |
||
39 | trait UserRelationTrait |
||
40 | { |
||
41 | use mb { |
||
42 | mb::addBlame as addGroup; |
||
43 | mb::createBlame as createGroup; |
||
44 | mb::addOrCreateBlame as addOrCreateGroup; |
||
45 | mb::removeBlame as removeGroup; |
||
46 | mb::removeAllBlames as removeAllGroups; |
||
47 | mb::getBlame as getGroup; |
||
48 | mb::getOrCreateBlame as getOrCreateGroup; |
||
49 | mb::getBlameds as getGroupMembers; |
||
50 | mb::getBlameGuids as getGroupGuids; |
||
51 | mb::setBlameGuids as setGroupGuids; |
||
52 | mb::getAllBlames as getAllGroups; |
||
53 | mb::getNonBlameds as getNonGroupMembers; |
||
54 | mb::getBlamesCount as getGroupsCount; |
||
55 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
56 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @var string the another party of the relation. |
||
61 | */ |
||
62 | public $otherGuidAttribute = 'other_guid'; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | public $remarkAttribute = 'remark'; |
||
68 | public static $relationSingle = 0; |
||
69 | public static $relationMutual = 1; |
||
70 | public $relationType = 1; |
||
71 | public $relationTypes = [ |
||
72 | 0 => 'Single', |
||
73 | 1 => 'Mutual', |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var string the attribute name of which determines the relation type. |
||
78 | */ |
||
79 | public $mutualTypeAttribute = 'type'; |
||
80 | public static $mutualTypeNormal = 0x00; |
||
81 | public static $mutualTypeSuspend = 0x01; |
||
82 | |||
83 | /** |
||
84 | * @var array Mutual types. |
||
85 | */ |
||
86 | public static $mutualTypes = [ |
||
87 | 0x00 => 'Normal', |
||
88 | 0x01 => 'Suspend', |
||
89 | ]; |
||
90 | |||
91 | /** |
||
92 | * @var string the attribute name of which determines the `favorite` field. |
||
93 | */ |
||
94 | public $favoriteAttribute = 'favorite'; |
||
95 | |||
96 | /** |
||
97 | * Permit to build self relation. |
||
98 | * @var boolean |
||
99 | */ |
||
100 | public $relationSelf = false; |
||
101 | |||
102 | /** |
||
103 | * Get whether this relation is favorite or not. |
||
104 | * @return boolean |
||
105 | */ |
||
106 | 1 | public function getIsFavorite() |
|
111 | |||
112 | /** |
||
113 | * Set favorite. |
||
114 | * @param boolean $fav |
||
115 | */ |
||
116 | 1 | public function setIsFavorite($fav) |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 8 | public function rules() |
|
129 | |||
130 | /** |
||
131 | * Get initiator. |
||
132 | * @return \vistart\Models\queries\BaseUserQuery |
||
133 | */ |
||
134 | public function getInitiator() |
||
138 | |||
139 | /** |
||
140 | * Get recipient. |
||
141 | * @return \vistart\Models\queries\BaseUserQuery |
||
142 | */ |
||
143 | public function getRecipient() |
||
152 | |||
153 | /** |
||
154 | * Validation rules associated with user relation. |
||
155 | * @return array rules. |
||
156 | */ |
||
157 | 8 | public function getUserRelationRules() |
|
168 | |||
169 | /** |
||
170 | * Get remark. |
||
171 | * @return string remark. |
||
172 | */ |
||
173 | public function getRemark() |
||
178 | |||
179 | /** |
||
180 | * Set remark. |
||
181 | * @param string $remark |
||
182 | * @return string remark. |
||
183 | */ |
||
184 | public function setRemark($remark) |
||
189 | |||
190 | /** |
||
191 | * Validation rules associated with remark attribute. |
||
192 | * @return array rules. |
||
193 | */ |
||
194 | 8 | public function getRemarkRules() |
|
201 | |||
202 | /** |
||
203 | * Validation rules associated with favorites attribute. |
||
204 | * @return array rules. |
||
205 | */ |
||
206 | 8 | public function getFavoriteRules() |
|
213 | |||
214 | /** |
||
215 | * Validation rules associated with other guid attribute. |
||
216 | * @return array rules. |
||
217 | */ |
||
218 | 8 | public function getOtherGuidRules() |
|
227 | |||
228 | /** |
||
229 | * Attach events associated with user relation. |
||
230 | */ |
||
231 | 8 | public function initUserRelationEvents() |
|
241 | |||
242 | /** |
||
243 | * Get opposite relation to self. |
||
244 | * @return \vistart\Models\models\BaseUserRelationModel |
||
245 | */ |
||
246 | 7 | public function getOpposite() |
|
255 | |||
256 | /** |
||
257 | * Build a suspend mutual relation, not support single relation. |
||
258 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
259 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
260 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
261 | * given if exists, or return a new relation. |
||
262 | */ |
||
263 | 1 | public static function buildSuspendRelation($user, $other) |
|
270 | |||
271 | /** |
||
272 | * Build a normal relation. |
||
273 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
274 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
275 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
276 | * given if exists, or return a new relation. |
||
277 | */ |
||
278 | 8 | public static function buildNormalRelation($user, $other) |
|
287 | |||
288 | /** |
||
289 | * Build relation between initiator and recipient. |
||
290 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
291 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
292 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
293 | * given if exists, or return a new relation. Or return null if not allowed |
||
294 | * to build self relation, |
||
295 | */ |
||
296 | 8 | protected static function buildRelation($user, $other) |
|
319 | |||
320 | /** |
||
321 | * Build opposite relation throughout the current relation. The opposite |
||
322 | * relation will be given if existed. |
||
323 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
324 | * @return \vistart\Models\models\BaseUserRelationModel |
||
325 | */ |
||
326 | 7 | protected static function buildOppositeRelation($relation) |
|
342 | |||
343 | /** |
||
344 | * Remove myself. |
||
345 | * @return integer|false The number of relations removed, or false if the remove |
||
346 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
347 | * removed is 0, even though the remove execution is successful. |
||
348 | */ |
||
349 | 2 | public function remove() |
|
353 | |||
354 | /** |
||
355 | * Remove first relation between initiator(s) and recipient(s). |
||
356 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
357 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
358 | * @return integer|false The number of relations removed. |
||
359 | */ |
||
360 | 1 | public static function removeOneRelation($user, $other) |
|
364 | |||
365 | /** |
||
366 | * Remove all relations between initiator(s) and recipient(s). |
||
367 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
368 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
369 | * @return integer The number of relations removed. |
||
370 | */ |
||
371 | 2 | public static function removeAllRelations($user, $other) |
|
378 | |||
379 | /** |
||
380 | * Get first relation between initiator(s) and recipient(s). |
||
381 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
382 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
383 | * @return \vistart\Models\models\BaseUserRelationModel |
||
384 | */ |
||
385 | 1 | public static function findOneRelation($user, $other) |
|
389 | |||
390 | /** |
||
391 | * Get first opposite relation between initiator(s) and recipient(s). |
||
392 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
393 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
394 | * @return \vistart\Models\models\BaseUserRelationModel |
||
395 | */ |
||
396 | 7 | public static function findOneOppositeRelation($user, $other) |
|
400 | |||
401 | /** |
||
402 | * Get user's or users' all relations, or by specified groups. |
||
403 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
404 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
405 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
406 | * @return array all eligible relations |
||
407 | */ |
||
408 | public static function findOnesAllRelations($user, $groups = null) |
||
412 | |||
413 | /** |
||
414 | * Initialize groups attribute. |
||
415 | * @param \yii\base\Event $event |
||
416 | */ |
||
417 | 8 | public function onInitGroups($event) |
|
422 | |||
423 | /** |
||
424 | * Initialize remark attribute. |
||
425 | * @param \yii\base\Event $event |
||
426 | */ |
||
427 | 8 | public function onInitRemark($event) |
|
433 | |||
434 | /** |
||
435 | * The event triggered after insert new relation. |
||
436 | * The opposite relation should be inserted without triggering events |
||
437 | * simultaneously after new relation inserted, |
||
438 | * @param \yii\base\Event $event |
||
439 | */ |
||
440 | 8 | public function onInsertRelation($event) |
|
452 | |||
453 | /** |
||
454 | * The event triggered after update relation. |
||
455 | * The opposite relation should be updated without triggering events |
||
456 | * simultaneously after existed relation removed. |
||
457 | * @param \yii\base\Event $event |
||
458 | */ |
||
459 | 3 | public function onUpdateRelation($event) |
|
471 | |||
472 | /** |
||
473 | * The event triggered after delete relation. |
||
474 | * The opposite relation should be deleted without triggering events |
||
475 | * simultaneously after existed relation removed. |
||
476 | * @param \yii\base\Event $event |
||
477 | */ |
||
478 | 2 | public function onDeleteRelation($event) |
|
489 | } |
||
490 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.