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 |
||
| 48 | trait UserRelationTrait |
||
| 49 | { |
||
| 50 | use mb, |
||
| 51 | MutualTrait { |
||
| 52 | mb::addBlame as addGroup; |
||
| 53 | mb::createBlame as createGroup; |
||
| 54 | mb::addOrCreateBlame as addOrCreateGroup; |
||
| 55 | mb::removeBlame as removeGroup; |
||
| 56 | mb::removeAllBlames as removeAllGroups; |
||
| 57 | mb::getBlame as getGroup; |
||
| 58 | mb::getOrCreateBlame as getOrCreateGroup; |
||
| 59 | mb::getBlameds as getGroupMembers; |
||
| 60 | mb::getBlameGuids as getGroupGuids; |
||
| 61 | mb::setBlameGuids as setGroupGuids; |
||
| 62 | mb::getAllBlames as getAllGroups; |
||
| 63 | mb::getNonBlameds as getNonGroupMembers; |
||
| 64 | mb::getBlamesCount as getGroupsCount; |
||
| 65 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $remarkAttribute = 'remark'; |
||
| 72 | public static $relationSingle = 0; |
||
| 73 | public static $relationMutual = 1; |
||
| 74 | public $relationType = 1; |
||
| 75 | public $relationTypes = [ |
||
| 76 | 0 => 'Single', |
||
| 77 | 1 => 'Mutual', |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string the attribute name of which determines the relation type. |
||
| 82 | */ |
||
| 83 | public $mutualTypeAttribute = 'type'; |
||
| 84 | public static $mutualTypeNormal = 0x00; |
||
| 85 | public static $mutualTypeSuspend = 0x01; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array Mutual types. |
||
| 89 | */ |
||
| 90 | public static $mutualTypes = [ |
||
| 91 | 0x00 => 'Normal', |
||
| 92 | 0x01 => 'Suspend', |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string the attribute name of which determines the `favorite` field. |
||
| 97 | */ |
||
| 98 | public $favoriteAttribute = 'favorite'; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Permit to build self relation. |
||
| 102 | * @var boolean |
||
| 103 | */ |
||
| 104 | public $relationSelf = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get whether this relation is favorite or not. |
||
| 108 | * @return boolean |
||
| 109 | */ |
||
| 110 | 1 | public function getIsFavorite() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Set favorite. |
||
| 118 | * @param boolean $fav |
||
| 119 | */ |
||
| 120 | 1 | public function setIsFavorite($fav) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @inheritdoc |
||
| 128 | */ |
||
| 129 | 15 | public function rules() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Validation rules associated with user relation. |
||
| 136 | * @return array rules. |
||
| 137 | */ |
||
| 138 | 15 | public function getUserRelationRules() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Get remark. |
||
| 152 | * @return string remark. |
||
| 153 | */ |
||
| 154 | 1 | public function getRemark() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Set remark. |
||
| 162 | * @param string $remark |
||
| 163 | * @return string remark. |
||
| 164 | */ |
||
| 165 | 1 | public function setRemark($remark) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Validation rules associated with remark attribute. |
||
| 173 | * @return array rules. |
||
| 174 | */ |
||
| 175 | 15 | public function getRemarkRules() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Validation rules associated with favorites attribute. |
||
| 185 | * @return array rules. |
||
| 186 | */ |
||
| 187 | 15 | public function getFavoriteRules() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Validation rules associated with other guid attribute. |
||
| 197 | * @return array rules. |
||
| 198 | */ |
||
| 199 | 15 | public function getOtherGuidRules() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Attach events associated with user relation. |
||
| 209 | */ |
||
| 210 | 16 | public function initUserRelationEvents() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get opposite relation against self. |
||
| 223 | * @return static |
||
| 224 | */ |
||
| 225 | 1 | public function getOpposite() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Check whether the initiator is followed by recipient. |
||
| 237 | * @param BaseUserModel $initiator |
||
| 238 | * @param BaseUserModel $recipient |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | 3 | public static function isFollowed($initiator, $recipient) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Check whether the initiator is following recipient. |
||
| 248 | * @param BaseUserModel $initiator |
||
| 249 | * @param BaseUserModel $recipient |
||
| 250 | * @return boolean |
||
| 251 | */ |
||
| 252 | 3 | public static function isFollowing($initiator, $recipient) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 259 | * Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
||
| 260 | * @param BaseUserModel $initiator |
||
| 261 | * @param BaseUserModel $recipient |
||
| 262 | * @return boolean |
||
| 263 | */ |
||
| 264 | 2 | public static function isMutual($initiator, $recipient) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 271 | * Or check whether the initiator and recipient are friend if the mutual type is normal. |
||
| 272 | * @param BaseUserModel $initiator |
||
| 273 | * @param BaseUserModel $recipient |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | 1 | public static function isFriend($initiator, $recipient) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Build new or return existed suspend mutual relation, or return null if |
||
| 294 | * current type is not mutual. |
||
| 295 | * @see buildRelation() |
||
| 296 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 297 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 298 | * @return static The relation will be |
||
| 299 | * given if exists, or return a new relation. |
||
| 300 | */ |
||
| 301 | 1 | public static function buildSuspendRelation($user, $other) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Build new or return existed normal relation. |
||
| 314 | * The status of mutual relation will be changed to normal if it is not. |
||
| 315 | * @see buildRelation() |
||
| 316 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 317 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 318 | * @return static The relation will be |
||
| 319 | * given if exists, or return a new relation. |
||
| 320 | */ |
||
| 321 | 16 | public static function buildNormalRelation($user, $other) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Build new or return existed relation between initiator and recipient. |
||
| 336 | * If relation between initiator and recipient is not found, new relation will |
||
| 337 | * be built. If initiator and recipient are the same one and it is not allowed |
||
| 338 | * to build self relation, null will be given. |
||
| 339 | * If you want to know whether the relation exists, you can check the return |
||
| 340 | * value of `getIsNewRecord()` method. |
||
| 341 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 342 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 343 | * @return static The relation will be |
||
| 344 | * given if exists, or return a new relation. Or return null if not allowed |
||
| 345 | * to build self relation, |
||
| 346 | */ |
||
| 347 | 16 | protected static function buildRelation($user, $other) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Build opposite relation throughout the current relation. The opposite |
||
| 373 | * relation will be given if existed. |
||
| 374 | * @param static $relation |
||
| 375 | * @return static |
||
| 376 | */ |
||
| 377 | protected static function buildOppositeRelation($relation) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Remove myself. |
||
| 396 | * @return integer|false The number of relations removed, or false if the remove |
||
| 397 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
| 398 | * removed is 0, even though the remove execution is successful. |
||
| 399 | */ |
||
| 400 | 16 | public function remove() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Remove first relation between initiator(s) and recipient(s). |
||
| 407 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 408 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 409 | * @return integer|false The number of relations removed. |
||
| 410 | */ |
||
| 411 | 1 | public static function removeOneRelation($user, $other) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Remove all relations between initiator(s) and recipient(s). |
||
| 418 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 419 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 420 | * @return integer The number of relations removed. |
||
| 421 | */ |
||
| 422 | 1 | public static function removeAllRelations($user, $other) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Get first relation between initiator(s) and recipient(s). |
||
| 432 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 433 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 434 | * @return static |
||
| 435 | */ |
||
| 436 | 4 | public static function findOneRelation($user, $other) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Get first opposite relation between initiator(s) and recipient(s). |
||
| 443 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
| 444 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
| 445 | * @return static |
||
| 446 | */ |
||
| 447 | 1 | public static function findOneOppositeRelation($user, $other) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get user's or users' all relations, or by specified groups. |
||
| 454 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
| 455 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
| 456 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
| 457 | * @return array all eligible relations |
||
| 458 | */ |
||
| 459 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Initialize groups attribute. |
||
| 466 | * @param ModelEvent $event |
||
| 467 | */ |
||
| 468 | 16 | public function onInitGroups($event) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Initialize remark attribute. |
||
| 476 | * @param ModelEvent $event |
||
| 477 | */ |
||
| 478 | 16 | public function onInitRemark($event) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * The event triggered after insert new relation. |
||
| 487 | * The opposite relation should be inserted without triggering events |
||
| 488 | * simultaneously after new relation inserted, |
||
| 489 | * @param ModelEvent $event |
||
| 490 | */ |
||
| 491 | 15 | public function onInsertRelation($event) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * The event triggered after update relation. |
||
| 506 | * The opposite relation should be updated without triggering events |
||
| 507 | * simultaneously after existed relation removed. |
||
| 508 | * @param ModelEvent $event |
||
| 509 | */ |
||
| 510 | 2 | public function onUpdateRelation($event) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * The event triggered after delete relation. |
||
| 525 | * The opposite relation should be deleted without triggering events |
||
| 526 | * simultaneously after existed relation removed. |
||
| 527 | * @param ModelEvent $event |
||
| 528 | */ |
||
| 529 | 16 | public function onDeleteRelation($event) |
|
| 540 | } |
||
| 541 |
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.