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 | * Permit to build self relation. |
||
| 94 | * @var boolean |
||
| 95 | */ |
||
| 96 | public $relationSelf = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get whether this relation is favorite or not. |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | 1 | public function getIsFavorite() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Set favorite. |
||
| 110 | * @param boolean $fav |
||
| 111 | */ |
||
| 112 | 1 | public function setIsFavorite($fav) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritdoc |
||
| 120 | */ |
||
| 121 | 8 | public function rules() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Validation rules associated with user relation. |
||
| 128 | * @return array rules. |
||
| 129 | */ |
||
| 130 | 8 | public function getUserRelationRules() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Get remark. |
||
| 144 | * @return string remark. |
||
| 145 | */ |
||
| 146 | public function getRemark() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set remark. |
||
| 154 | * @param string $remark |
||
| 155 | * @return string remark. |
||
| 156 | */ |
||
| 157 | public function setRemark($remark) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Validation rules associated with remark attribute. |
||
| 165 | * @return array rules. |
||
| 166 | */ |
||
| 167 | 8 | public function getRemarkRules() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Validation rules associated with favorites attribute. |
||
| 177 | * @return array rules. |
||
| 178 | */ |
||
| 179 | 8 | public function getFavoriteRules() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Validation rules associated with other guid attribute. |
||
| 189 | * @return array rules. |
||
| 190 | */ |
||
| 191 | 8 | public function getOtherGuidRules() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Attach events associated with user relation. |
||
| 203 | */ |
||
| 204 | 8 | public function initUserRelationEvents() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get opposite relation to self. |
||
| 217 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 218 | */ |
||
| 219 | 7 | public function getOpposite() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Build a suspend mutual relation, not support single relation. |
||
| 231 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 232 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 233 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 234 | * given if exists, or return a new relation. |
||
| 235 | */ |
||
| 236 | 1 | public static function buildSuspendRelation($user, $other) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Build a normal relation. |
||
| 246 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 247 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 248 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 249 | * given if exists, or return a new relation. |
||
| 250 | */ |
||
| 251 | 8 | public static function buildNormalRelation($user, $other) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Build relation between initiator and recipient. |
||
| 263 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 264 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 265 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 266 | * given if exists, or return a new relation. Or return null if not allowed |
||
| 267 | * to build self relation, |
||
| 268 | */ |
||
| 269 | 8 | protected static function buildRelation($user, $other) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Build opposite relation throughout the current relation. The opposite |
||
| 295 | * relation will be given if existed. |
||
| 296 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
| 297 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 298 | */ |
||
| 299 | 7 | protected static function buildOppositeRelation($relation) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Remove myself. |
||
| 318 | * @return integer|false The number of relations removed, or false if the remove |
||
| 319 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
| 320 | * removed is 0, even though the remove execution is successful. |
||
| 321 | */ |
||
| 322 | 2 | public function remove() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Remove first relation between initiator(s) and recipient(s). |
||
| 329 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 330 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 331 | * @return integer|false The number of relations removed. |
||
| 332 | */ |
||
| 333 | 1 | public static function removeOneRelation($user, $other) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Remove all relations between initiator(s) and recipient(s). |
||
| 340 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 341 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 342 | * @return integer The number of relations removed. |
||
| 343 | */ |
||
| 344 | 2 | public static function removeAllRelations($user, $other) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get first relation between initiator(s) and recipient(s). |
||
| 354 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 355 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 356 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 357 | */ |
||
| 358 | 1 | public static function findOneRelation($user, $other) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get first opposite relation between initiator(s) and recipient(s). |
||
| 365 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
| 366 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
| 367 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 368 | */ |
||
| 369 | 7 | public static function findOneOppositeRelation($user, $other) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Get user's or users' all relations, or by specified groups. |
||
| 376 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
| 377 | * @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. |
||
| 378 | * @return array all eligible relations |
||
| 379 | */ |
||
| 380 | public static function findOnesAllRelations($user, $groups = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Initialize groups attribute. |
||
| 387 | * @param \yii\base\Event $event |
||
| 388 | */ |
||
| 389 | 8 | public function onInitGroups($event) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Initialize remark attribute. |
||
| 397 | * @param \yii\base\Event $event |
||
| 398 | */ |
||
| 399 | 8 | public function onInitRemark($event) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * The event triggered after insert new relation. |
||
| 408 | * The opposite relation should be inserted without triggering events |
||
| 409 | * simultaneously after new relation inserted, |
||
| 410 | * @param \yii\base\Event $event |
||
| 411 | */ |
||
| 412 | 8 | public function onInsertRelation($event) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * The event triggered after update relation. |
||
| 427 | * The opposite relation should be updated without triggering events |
||
| 428 | * simultaneously after existed relation removed. |
||
| 429 | * @param \yii\base\Event $event |
||
| 430 | */ |
||
| 431 | 3 | public function onUpdateRelation($event) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * The event triggered after delete relation. |
||
| 446 | * The opposite relation should be deleted without triggering events |
||
| 447 | * simultaneously after existed relation removed. |
||
| 448 | * @param \yii\base\Event $event |
||
| 449 | */ |
||
| 450 | 2 | public function onDeleteRelation($event) |
|
| 461 | } |
||
| 462 |
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
Idableprovides a methodequalsIdthat 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.