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::createBlame as createGroup; |
||
| 39 | mb::addOrCreateBlame as addOrCreateGroup; |
||
| 40 | mb::removeBlame as removeGroup; |
||
| 41 | mb::removeAllBlames as removeAllGroups; |
||
| 42 | mb::getBlame as getGroup; |
||
| 43 | mb::getOrCreateBlame as getOrCreateGroup; |
||
| 44 | mb::getBlameds as getGroupMembers; |
||
| 45 | mb::getBlameGuids as getGroupGuids; |
||
| 46 | mb::setBlameGuids as setGroupGuids; |
||
| 47 | mb::getAllBlames as getAllGroups; |
||
| 48 | mb::getNonBlameds as getNonGroupMembers; |
||
| 49 | mb::getBlamesCount as getGroupsCount; |
||
| 50 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
| 51 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string the another party of the relation. |
||
| 56 | */ |
||
| 57 | public $otherGuidAttribute = 'other_guid'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $remarkAttribute = 'remark'; |
||
| 63 | public static $relationSingle = 0; |
||
| 64 | public static $relationMutual = 1; |
||
| 65 | public $relationType = 1; |
||
| 66 | public $relationTypes = [ |
||
| 67 | 0 => 'Single', |
||
| 68 | 1 => 'Mutual', |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string the attribute name of which determines the relation type. |
||
| 73 | */ |
||
| 74 | public $mutualTypeAttribute = 'type'; |
||
| 75 | public static $mutualTypeNormal = 0x00; |
||
| 76 | public static $mutualTypeSuspend = 0x01; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array Mutual types. |
||
| 80 | */ |
||
| 81 | public static $mutualTypes = [ |
||
| 82 | 0x00 => 'Normal', |
||
| 83 | 0x01 => 'Suspend', |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string the attribute name of which determines the `favorite` field. |
||
| 88 | */ |
||
| 89 | public $favoriteAttribute = 'favorite'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get whether this relation is favorite or not. |
||
| 93 | * @return boolean |
||
| 94 | */ |
||
| 95 | 1 | public function getIsFavorite() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Set favorite. |
||
| 103 | * @param boolean $fav |
||
| 104 | */ |
||
| 105 | 1 | public function setIsFavorite($fav) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 8 | public function rules() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Validation rules associated with user relation. |
||
| 121 | * @return array rules. |
||
| 122 | */ |
||
| 123 | 8 | public function getUserRelationRules() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get remark. |
||
| 137 | * @return string remark. |
||
| 138 | */ |
||
| 139 | public function getRemark() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set remark. |
||
| 147 | * @param string $remark |
||
| 148 | * @return string remark. |
||
| 149 | */ |
||
| 150 | public function setRemark($remark) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Validation rules associated with remark attribute. |
||
| 158 | * @return array rules. |
||
| 159 | */ |
||
| 160 | 8 | public function getRemarkRules() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Validation rules associated with favorites attribute. |
||
| 170 | * @return array rules. |
||
| 171 | */ |
||
| 172 | 8 | public function getFavoriteRules() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Validation rules associated with other guid attribute. |
||
| 182 | * @return array rules. |
||
| 183 | */ |
||
| 184 | 8 | public function getOtherGuidRules() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Attach events associated with user relation. |
||
| 196 | */ |
||
| 197 | 8 | public function initUserRelationEvents() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get opposite relation to self. |
||
| 210 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 211 | */ |
||
| 212 | 7 | public function getOpposite() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Build a suspend mutual relation, not support single relation. |
||
| 224 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 225 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 226 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 227 | * given if exists, or return a new relation. |
||
| 228 | */ |
||
| 229 | 1 | public static function buildSuspendRelation($user, $other) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Build a normal relation. |
||
| 239 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 240 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 241 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 242 | * given if exists, or return a new relation. |
||
| 243 | */ |
||
| 244 | 8 | public static function buildNormalRelation($user, $other) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Build relation between initiator and recipient. |
||
| 256 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 257 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 258 | * @return \vistart\Models\models\BaseUserRelationModel The relation will be |
||
| 259 | * given if exists, or return a new relation. |
||
| 260 | */ |
||
| 261 | 8 | protected static function buildRelation($user, $other) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Build opposite mutual relation throughout the current relation, not support |
||
| 283 | * single relation. The opposite relation will be given if existed. |
||
| 284 | * @param \vistart\Models\models\BaseUserRelationModel $relation |
||
| 285 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 286 | */ |
||
| 287 | 7 | protected static function buildOppositeRelation($relation) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Remove myself. |
||
| 302 | * @return integer|false The number of relations removed, or false if the remove |
||
| 303 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
| 304 | * removed is 0, even though the remove execution is successful. |
||
| 305 | */ |
||
| 306 | 2 | public function remove() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Remove first relation between initiator(s) and recipient(s). |
||
| 313 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 314 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 315 | * @return integer|false The number of relations removed. |
||
| 316 | */ |
||
| 317 | 1 | public static function removeOneRelation($user, $other) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Remove all relations between initiator(s) and recipient(s). |
||
| 324 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 325 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 326 | * @return integer The number of relations removed. |
||
| 327 | */ |
||
| 328 | 2 | public static function removeAllRelations($user, $other) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get first relation between initiator(s) and recipient(s). |
||
| 338 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 339 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 340 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 341 | */ |
||
| 342 | 1 | public static function findOneRelation($user, $other) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get first opposite relation between initiator(s) and recipient(s). |
||
| 349 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
| 350 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
| 351 | * @return \vistart\Models\models\BaseUserRelationModel |
||
| 352 | */ |
||
| 353 | 7 | public static function findOneOppositeRelation($user, $other) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get user's or users' all relations, or by specified groups. |
||
| 360 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
| 361 | * @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. |
||
| 362 | * @return array all eligible relations |
||
| 363 | */ |
||
| 364 | public static function findOnesAllRelations($user, $groups = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Initialize groups attribute. |
||
| 371 | * @param \yii\base\Event $event |
||
| 372 | */ |
||
| 373 | 8 | public function onInitGroups($event) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Initialize remark attribute. |
||
| 381 | * @param \yii\base\Event $event |
||
| 382 | */ |
||
| 383 | 8 | public function onInitRemark($event) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * The event triggered after insert new relation. |
||
| 392 | * The opposite relation should be inserted without triggering events |
||
| 393 | * simultaneously after new relation inserted, |
||
| 394 | * @param \yii\base\Event $event |
||
| 395 | */ |
||
| 396 | 8 | public function onInsertRelation($event) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * The event triggered after update relation. |
||
| 411 | * The opposite relation should be updated without triggering events |
||
| 412 | * simultaneously after existed relation removed. |
||
| 413 | * @param \yii\base\Event $event |
||
| 414 | */ |
||
| 415 | 3 | public function onUpdateRelation($event) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * The event triggered after delete relation. |
||
| 430 | * The opposite relation should be deleted without triggering events |
||
| 431 | * simultaneously after existed relation removed. |
||
| 432 | * @param \yii\base\Event $event |
||
| 433 | */ |
||
| 434 | 2 | public function onDeleteRelation($event) |
|
| 445 | } |
||
| 446 |
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.