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 |
||
41 | trait UserRelationTrait |
||
42 | { |
||
43 | use mb { |
||
44 | mb::addBlame as addGroup; |
||
45 | mb::createBlame as createGroup; |
||
46 | mb::addOrCreateBlame as addOrCreateGroup; |
||
47 | mb::removeBlame as removeGroup; |
||
48 | mb::removeAllBlames as removeAllGroups; |
||
49 | mb::getBlame as getGroup; |
||
50 | mb::getOrCreateBlame as getOrCreateGroup; |
||
51 | mb::getBlameds as getGroupMembers; |
||
52 | mb::getBlameGuids as getGroupGuids; |
||
53 | mb::setBlameGuids as setGroupGuids; |
||
54 | mb::getAllBlames as getAllGroups; |
||
55 | mb::getNonBlameds as getNonGroupMembers; |
||
56 | mb::getBlamesCount as getGroupsCount; |
||
57 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
58 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @var string the another party of the relation. |
||
63 | */ |
||
64 | public $otherGuidAttribute = 'other_guid'; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | public $remarkAttribute = 'remark'; |
||
70 | public static $relationSingle = 0; |
||
71 | public static $relationMutual = 1; |
||
72 | public $relationType = 1; |
||
73 | public $relationTypes = [ |
||
74 | 0 => 'Single', |
||
75 | 1 => 'Mutual', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @var string the attribute name of which determines the relation type. |
||
80 | */ |
||
81 | public $mutualTypeAttribute = 'type'; |
||
82 | public static $mutualTypeNormal = 0x00; |
||
83 | public static $mutualTypeSuspend = 0x01; |
||
84 | |||
85 | /** |
||
86 | * @var array Mutual types. |
||
87 | */ |
||
88 | public static $mutualTypes = [ |
||
89 | 0x00 => 'Normal', |
||
90 | 0x01 => 'Suspend', |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * @var string the attribute name of which determines the `favorite` field. |
||
95 | */ |
||
96 | public $favoriteAttribute = 'favorite'; |
||
97 | |||
98 | /** |
||
99 | * Permit to build self relation. |
||
100 | * @var boolean |
||
101 | */ |
||
102 | public $relationSelf = false; |
||
103 | |||
104 | /** |
||
105 | * Get whether this relation is favorite or not. |
||
106 | * @return boolean |
||
107 | */ |
||
108 | 1 | public function getIsFavorite() |
|
113 | |||
114 | /** |
||
115 | * Set favorite. |
||
116 | * @param boolean $fav |
||
117 | */ |
||
118 | 1 | public function setIsFavorite($fav) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | 8 | public function rules() |
|
131 | |||
132 | /** |
||
133 | * Get initiator. |
||
134 | * @return \vistart\Models\queries\BaseUserQuery |
||
135 | */ |
||
136 | public function getInitiator() |
||
140 | |||
141 | /** |
||
142 | * Get recipient. |
||
143 | * @return \vistart\Models\queries\BaseUserQuery |
||
144 | */ |
||
145 | public function getRecipient() |
||
154 | |||
155 | /** |
||
156 | * Validation rules associated with user relation. |
||
157 | * @return array rules. |
||
158 | */ |
||
159 | 8 | public function getUserRelationRules() |
|
170 | |||
171 | /** |
||
172 | * Get remark. |
||
173 | * @return string remark. |
||
174 | */ |
||
175 | public function getRemark() |
||
180 | |||
181 | /** |
||
182 | * Set remark. |
||
183 | * @param string $remark |
||
184 | * @return string remark. |
||
185 | */ |
||
186 | public function setRemark($remark) |
||
191 | |||
192 | /** |
||
193 | * Validation rules associated with remark attribute. |
||
194 | * @return array rules. |
||
195 | */ |
||
196 | 8 | public function getRemarkRules() |
|
203 | |||
204 | /** |
||
205 | * Validation rules associated with favorites attribute. |
||
206 | * @return array rules. |
||
207 | */ |
||
208 | 8 | public function getFavoriteRules() |
|
215 | |||
216 | /** |
||
217 | * Validation rules associated with other guid attribute. |
||
218 | * @return array rules. |
||
219 | */ |
||
220 | 8 | public function getOtherGuidRules() |
|
229 | |||
230 | /** |
||
231 | * Attach events associated with user relation. |
||
232 | */ |
||
233 | 8 | public function initUserRelationEvents() |
|
243 | |||
244 | /** |
||
245 | * Get opposite relation against self. |
||
246 | * @return \vistart\Models\models\BaseUserRelationModel |
||
247 | */ |
||
248 | 7 | public function getOpposite() |
|
257 | |||
258 | /** |
||
259 | * Build new or return existed suspend mutual relation, of return null if |
||
260 | * current type is not mutual. |
||
261 | * @see buildRelation() |
||
262 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
263 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
264 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
265 | * given if exists, or return a new relation. |
||
266 | */ |
||
267 | 1 | public static function buildSuspendRelation($user, $other) |
|
277 | |||
278 | /** |
||
279 | * Build new or return existed normal relation. |
||
280 | * The status of mutual relation will be changed to normal if it is not. |
||
281 | * @see buildRelation() |
||
282 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
283 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
284 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
285 | * given if exists, or return a new relation. |
||
286 | */ |
||
287 | 8 | public static function buildNormalRelation($user, $other) |
|
296 | |||
297 | /** |
||
298 | * Build new or return existed relation between initiator and recipient. |
||
299 | * If relation between initiator and recipient is not found, new relation will |
||
300 | * be built. If initiator and recipient are the same one and it is not allowed |
||
301 | * to build self relation, null will be given. |
||
302 | * If you want to know whether the relation exists, you can check the return |
||
303 | * value of `getIsNewRecord()` method. |
||
304 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
305 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
306 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
307 | * given if exists, or return a new relation. Or return null if not allowed |
||
308 | * to build self relation, |
||
309 | */ |
||
310 | 8 | protected static function buildRelation($user, $other) |
|
333 | |||
334 | /** |
||
335 | * Build opposite relation throughout the current relation. The opposite |
||
336 | * relation will be given if existed. |
||
337 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
338 | * @return \vistart\Models\models\BaseUserRelationModel |
||
339 | */ |
||
340 | 7 | protected static function buildOppositeRelation($relation) |
|
356 | |||
357 | /** |
||
358 | * Remove myself. |
||
359 | * @return integer|false The number of relations removed, or false if the remove |
||
360 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
361 | * removed is 0, even though the remove execution is successful. |
||
362 | */ |
||
363 | 2 | public function remove() |
|
367 | |||
368 | /** |
||
369 | * Remove first relation between initiator(s) and recipient(s). |
||
370 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
371 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
372 | * @return integer|false The number of relations removed. |
||
373 | */ |
||
374 | 1 | public static function removeOneRelation($user, $other) |
|
378 | |||
379 | /** |
||
380 | * Remove all relations 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 integer The number of relations removed. |
||
384 | */ |
||
385 | 2 | public static function removeAllRelations($user, $other) |
|
392 | |||
393 | /** |
||
394 | * Get first relation between initiator(s) and recipient(s). |
||
395 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
396 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
397 | * @return \vistart\Models\models\BaseUserRelationModel |
||
398 | */ |
||
399 | 1 | public static function findOneRelation($user, $other) |
|
403 | |||
404 | /** |
||
405 | * Get first opposite relation between initiator(s) and recipient(s). |
||
406 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
407 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
408 | * @return \vistart\Models\models\BaseUserRelationModel |
||
409 | */ |
||
410 | 7 | public static function findOneOppositeRelation($user, $other) |
|
414 | |||
415 | /** |
||
416 | * Get user's or users' all relations, or by specified groups. |
||
417 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
418 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
419 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
420 | * @return array all eligible relations |
||
421 | */ |
||
422 | public static function findOnesAllRelations($user, $groups = null) |
||
426 | |||
427 | /** |
||
428 | * Initialize groups attribute. |
||
429 | * @param \yii\base\Event $event |
||
430 | */ |
||
431 | 8 | public function onInitGroups($event) |
|
436 | |||
437 | /** |
||
438 | * Initialize remark attribute. |
||
439 | * @param \yii\base\Event $event |
||
440 | */ |
||
441 | 8 | public function onInitRemark($event) |
|
447 | |||
448 | /** |
||
449 | * The event triggered after insert new relation. |
||
450 | * The opposite relation should be inserted without triggering events |
||
451 | * simultaneously after new relation inserted, |
||
452 | * @param \yii\base\Event $event |
||
453 | */ |
||
454 | 8 | public function onInsertRelation($event) |
|
466 | |||
467 | /** |
||
468 | * The event triggered after update relation. |
||
469 | * The opposite relation should be updated without triggering events |
||
470 | * simultaneously after existed relation removed. |
||
471 | * @param \yii\base\Event $event |
||
472 | */ |
||
473 | 3 | public function onUpdateRelation($event) |
|
485 | |||
486 | /** |
||
487 | * The event triggered after delete relation. |
||
488 | * The opposite relation should be deleted without triggering events |
||
489 | * simultaneously after existed relation removed. |
||
490 | * @param \yii\base\Event $event |
||
491 | */ |
||
492 | 2 | public function onDeleteRelation($event) |
|
503 | } |
||
504 |
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.