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 |
||
| 72 | trait UserRelationTrait |
||
| 73 | { |
||
| 74 | use mb, |
||
| 75 | MutualTrait { |
||
| 76 | mb::addBlame as addGroup; |
||
| 77 | mb::createBlame as createGroup; |
||
| 78 | mb::addOrCreateBlame as addOrCreateGroup; |
||
| 79 | mb::removeBlame as removeGroup; |
||
| 80 | mb::removeAllBlames as removeAllGroups; |
||
| 81 | mb::getBlame as getGroup; |
||
| 82 | mb::getOrCreateBlame as getOrCreateGroup; |
||
| 83 | mb::getBlameds as getGroupMembers; |
||
| 84 | mb::getBlameGuids as getGroupGuids; |
||
| 85 | mb::setBlameGuids as setGroupGuids; |
||
| 86 | mb::getOwnBlames as getOwnGroups; |
||
| 87 | mb::setOwnBlames as setOwnGroups; |
||
| 88 | mb::isBlameOwned as isGroupContained; |
||
| 89 | mb::getAllBlames as getAllGroups; |
||
| 90 | mb::getNonBlameds as getNonGroupMembers; |
||
| 91 | mb::getBlamesCount as getGroupsCount; |
||
| 92 | mb::getEmptyBlames as getEmptyGroups; |
||
| 93 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | public $remarkAttribute = 'remark'; |
||
| 100 | public static $relationSingle = 0; |
||
| 101 | public static $relationMutual = 1; |
||
| 102 | public $relationType = 1; |
||
| 103 | public static $relationTypes = [ |
||
| 104 | 0 => 'Single', |
||
| 105 | 1 => 'Mutual', |
||
| 106 | ]; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string the attribute name of which determines the relation type. |
||
| 110 | */ |
||
| 111 | public $mutualTypeAttribute = 'type'; |
||
| 112 | public static $mutualTypeNormal = 0x00; |
||
| 113 | public static $mutualTypeSuspend = 0x01; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var array Mutual types. |
||
| 117 | */ |
||
| 118 | public static $mutualTypes = [ |
||
| 119 | 0x00 => 'Normal', |
||
| 120 | 0x01 => 'Suspend', |
||
| 121 | ]; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string the attribute name of which determines the `favorite` field. |
||
| 125 | */ |
||
| 126 | public $favoriteAttribute = 'favorite'; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Permit to build self relation. |
||
| 130 | * @var boolean |
||
| 131 | */ |
||
| 132 | public $relationSelf = false; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get whether this relation is favorite or not. |
||
| 136 | * @return boolean |
||
| 137 | */ |
||
| 138 | 1 | public function getIsFavorite() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Set favorite. |
||
| 146 | * @param boolean $fav |
||
| 147 | */ |
||
| 148 | 1 | public function setIsFavorite($fav) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @inheritdoc |
||
| 156 | */ |
||
| 157 | 40 | public function rules() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Validation rules associated with user relation. |
||
| 164 | * @return array rules. |
||
| 165 | */ |
||
| 166 | 40 | public function getUserRelationRules() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Get remark. |
||
| 180 | * @return string remark. |
||
| 181 | */ |
||
| 182 | 1 | public function getRemark() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Set remark. |
||
| 190 | * @param string $remark |
||
| 191 | * @return string remark. |
||
| 192 | */ |
||
| 193 | 1 | public function setRemark($remark) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Validation rules associated with remark attribute. |
||
| 201 | * @return array rules. |
||
| 202 | */ |
||
| 203 | 40 | public function getRemarkRules() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Validation rules associated with favorites attribute. |
||
| 213 | * @return array rules. |
||
| 214 | */ |
||
| 215 | 40 | public function getFavoriteRules() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Validation rules associated with other guid attribute. |
||
| 225 | * @return array rules. |
||
| 226 | */ |
||
| 227 | 40 | public function getOtherGuidRules() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Attach events associated with user relation. |
||
| 237 | */ |
||
| 238 | 43 | public function initUserRelationEvents() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get opposite relation against self. |
||
| 251 | * @return static |
||
| 252 | */ |
||
| 253 | 1 | public function getOpposite() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Check whether the initiator is followed by recipient. |
||
| 263 | * @param BaseUserModel $initiator |
||
| 264 | * @param BaseUserModel $recipient |
||
| 265 | * @return boolean |
||
| 266 | */ |
||
| 267 | 6 | public static function isFollowed($initiator, $recipient) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Check whether the initiator is following recipient. |
||
| 274 | * @param BaseUserModel $initiator |
||
| 275 | * @param BaseUserModel $recipient |
||
| 276 | * @return boolean |
||
| 277 | */ |
||
| 278 | 6 | public static function isFollowing($initiator, $recipient) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 285 | * Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
||
| 286 | * @param BaseUserModel $initiator |
||
| 287 | * @param BaseUserModel $recipient |
||
| 288 | * @return boolean |
||
| 289 | */ |
||
| 290 | 3 | public static function isMutual($initiator, $recipient) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 297 | * Or check whether the initiator and recipient are friend if the mutual type is normal. |
||
| 298 | * @param BaseUserModel $initiator |
||
| 299 | * @param BaseUserModel $recipient |
||
| 300 | * @return boolean |
||
| 301 | */ |
||
| 302 | 6 | public static function isFriend($initiator, $recipient) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Build new or return existed suspend mutual relation, or return null if |
||
| 320 | * current type is not mutual. |
||
| 321 | * @see buildRelation() |
||
| 322 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 323 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 324 | * @return static The relation will be |
||
| 325 | * given if exists, or return a new relation. |
||
| 326 | */ |
||
| 327 | 15 | public static function buildSuspendRelation($user, $other) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Build new or return existed normal relation. |
||
| 339 | * The status of mutual relation will be changed to normal if it is not. |
||
| 340 | * @see buildRelation() |
||
| 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. |
||
| 345 | */ |
||
| 346 | 43 | public static function buildNormalRelation($user, $other) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Transform relation from suspend to normal. |
||
| 360 | * Note: You should ensure the relation model is not new one. |
||
| 361 | * @param static $relation |
||
| 362 | * @return boolean |
||
| 363 | */ |
||
| 364 | 2 | public static function transformSuspendToNormal($relation) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Revert relation from normal to suspend. |
||
| 375 | * Note: You should ensure the relation model is not new one. |
||
| 376 | * @param static $relation |
||
| 377 | * @return boolean |
||
| 378 | */ |
||
| 379 | 2 | public static function revertNormalToSuspend($relation) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Build new or return existed relation between initiator and recipient. |
||
| 390 | * If relation between initiator and recipient is not found, new relation will |
||
| 391 | * be built. If initiator and recipient are the same one and it is not allowed |
||
| 392 | * to build self relation, null will be given. |
||
| 393 | * If you want to know whether the relation exists, you can check the return |
||
| 394 | * value of `getIsNewRecord()` method. |
||
| 395 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 396 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 397 | * @return static The relation will be |
||
| 398 | * given if exists, or return a new relation. Or return null if not allowed |
||
| 399 | * to build self relation, |
||
| 400 | */ |
||
| 401 | 43 | protected static function buildRelation($user, $other) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Build opposite relation throughout the current relation. The opposite |
||
| 425 | * relation will be given if existed. |
||
| 426 | * @param static $relation |
||
| 427 | * @return static |
||
| 428 | */ |
||
| 429 | 12 | protected static function buildOppositeRelation($relation) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Get mutual type. |
||
| 445 | * @return integer |
||
| 446 | */ |
||
| 447 | 12 | public function getMutualType() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Set mutual type. |
||
| 458 | * @param integer $type |
||
| 459 | * @return integer |
||
| 460 | */ |
||
| 461 | 14 | protected function setMutualType($type) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Insert relation, the process is placed in a transaction. |
||
| 475 | * Note: This feature only support relational databases and skip all errors. |
||
| 476 | * If you don't want to use transaction or database doesn't support it, |
||
| 477 | * please use `save()` directly. |
||
| 478 | * @param static $relation |
||
| 479 | * @param Connection $db |
||
| 480 | * @return boolean |
||
| 481 | * @throws InvalidValueException |
||
| 482 | * @throws InvalidConfigException |
||
| 483 | * @throws IntegrityException |
||
| 484 | */ |
||
| 485 | 1 | public static function insertRelation($relation, Connection $db = null) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Remove relation, the process is placed in transaction. |
||
| 515 | * Note: This feature only support relational databases and skip all errors. |
||
| 516 | * If you don't want to use transaction or database doesn't support it, |
||
| 517 | * please use `remove()` directly. |
||
| 518 | * @param static $relation |
||
| 519 | * @param Connection $db |
||
| 520 | * @return boolean|integer |
||
| 521 | * @throws InvalidConfigException |
||
| 522 | */ |
||
| 523 | 1 | public static function removeRelation($relation, Connection $db = null) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Remove myself. |
||
| 549 | * @return integer|false The number of relations removed, or false if the remove |
||
| 550 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
| 551 | * removed is 0, even though the remove execution is successful. |
||
| 552 | */ |
||
| 553 | 43 | public function remove() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Remove first relation between initiator(s) and recipient(s). |
||
| 560 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 561 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 562 | * @return integer|false The number of relations removed. |
||
| 563 | */ |
||
| 564 | 1 | public static function removeOneRelation($user, $other) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Remove all relations between initiator(s) and recipient(s). |
||
| 575 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 576 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 577 | * @return integer The number of relations removed. |
||
| 578 | */ |
||
| 579 | 15 | public static function removeAllRelations($user, $other) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Get first relation between initiator(s) and recipient(s). |
||
| 589 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 590 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 591 | * @return static |
||
| 592 | */ |
||
| 593 | 4 | public static function findOneRelation($user, $other) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Get first opposite relation between initiator(s) and recipient(s). |
||
| 600 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
| 601 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
| 602 | * @return static |
||
| 603 | */ |
||
| 604 | 1 | public static function findOneOppositeRelation($user, $other) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Get user's or users' all relations, or by specified groups. |
||
| 611 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
| 612 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
| 613 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
| 614 | * @return array all eligible relations |
||
| 615 | */ |
||
| 616 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Initialize groups attribute. |
||
| 623 | * @param ModelEvent $event |
||
| 624 | */ |
||
| 625 | 43 | public function onInitGroups($event) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Initialize remark attribute. |
||
| 633 | * @param ModelEvent $event |
||
| 634 | */ |
||
| 635 | 43 | public function onInitRemark($event) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * The event triggered after insert new relation. |
||
| 644 | * The opposite relation should be inserted without triggering events |
||
| 645 | * simultaneously after new relation inserted, |
||
| 646 | * @param ModelEvent $event |
||
| 647 | * @throws IntegrityException throw if insert failed. |
||
| 648 | */ |
||
| 649 | 40 | public function onInsertRelation($event) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * The event triggered after update relation. |
||
| 665 | * The opposite relation should be updated without triggering events |
||
| 666 | * simultaneously after existed relation removed. |
||
| 667 | * @param ModelEvent $event |
||
| 668 | * @throw IntegrityException throw if update failed. |
||
| 669 | */ |
||
| 670 | 9 | public function onUpdateRelation($event) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * The event triggered after delete relation. |
||
| 686 | * The opposite relation should be deleted without triggering events |
||
| 687 | * simultaneously after existed relation removed. |
||
| 688 | * @param ModelEvent $event |
||
| 689 | */ |
||
| 690 | 43 | public function onDeleteRelation($event) |
|
| 701 | } |
||
| 702 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: