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() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Set favorite. |
||
| 147 | * @param boolean $fav |
||
| 148 | */ |
||
| 149 | 1 | public function setIsFavorite($fav) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | 46 | public function rules() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Validation rules associated with user relation. |
||
| 166 | * @return array rules. |
||
| 167 | */ |
||
| 168 | 46 | public function getUserRelationRules() |
|
| 169 | { |
||
| 170 | 46 | $rules = []; |
|
| 171 | 46 | if ($this->relationType == static::$relationMutual) { |
|
| 172 | $rules = [ |
||
| 173 | 12 | [[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
|
| 174 | 12 | [[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
|
| 175 | ]; |
||
| 176 | } |
||
| 177 | 46 | return array_merge($rules, $this->getRemarkRules(), |
|
| 178 | 46 | $this->getFavoriteRules(), |
|
| 179 | 46 | $this->getGroupsRules(), |
|
| 180 | 46 | $this->getOtherGuidRules()); |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get remark. |
||
| 185 | * @return string remark. |
||
| 186 | */ |
||
| 187 | 1 | public function getRemark() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Set remark. |
||
| 195 | * @param string $remark |
||
| 196 | * @return string remark. |
||
| 197 | */ |
||
| 198 | 49 | public function setRemark($remark) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Validation rules associated with remark attribute. |
||
| 206 | * @return array rules. |
||
| 207 | */ |
||
| 208 | 46 | public function getRemarkRules() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Validation rules associated with favorites attribute. |
||
| 218 | * @return array rules. |
||
| 219 | */ |
||
| 220 | 46 | public function getFavoriteRules() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Validation rules associated with other guid attribute. |
||
| 230 | * @return array rules. |
||
| 231 | */ |
||
| 232 | 46 | public function getOtherGuidRules() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Attach events associated with user relation. |
||
| 245 | */ |
||
| 246 | 49 | public function initUserRelationEvents() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get opposite relation against self. |
||
| 259 | * @return static |
||
| 260 | */ |
||
| 261 | 1 | public function getOpposite() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Check whether the initiator is followed by recipient. |
||
| 271 | * @param BaseUserModel $initiator |
||
| 272 | * @param BaseUserModel $recipient |
||
| 273 | * @return boolean |
||
| 274 | */ |
||
| 275 | 6 | public static function isFollowed($initiator, $recipient) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Check whether the initiator is following recipient. |
||
| 282 | * @param BaseUserModel $initiator |
||
| 283 | * @param BaseUserModel $recipient |
||
| 284 | * @return boolean |
||
| 285 | */ |
||
| 286 | 6 | public static function isFollowing($initiator, $recipient) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 293 | * Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
||
| 294 | * @param BaseUserModel $initiator |
||
| 295 | * @param BaseUserModel $recipient |
||
| 296 | * @return boolean |
||
| 297 | */ |
||
| 298 | 3 | public static function isMutual($initiator, $recipient) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
| 305 | * Or check whether the initiator and recipient are friend if the mutual type is normal. |
||
| 306 | * @param BaseUserModel $initiator |
||
| 307 | * @param BaseUserModel $recipient |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | 6 | public static function isFriend($initiator, $recipient) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Build new or return existed suspend mutual relation, or return null if |
||
| 330 | * current type is not mutual. |
||
| 331 | * @see buildRelation() |
||
| 332 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 333 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 334 | * @return static The relation will be |
||
| 335 | * given if exists, or return a new relation. |
||
| 336 | */ |
||
| 337 | 15 | public static function buildSuspendRelation($user, $other) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Build new or return existed normal relation. |
||
| 349 | * The status of mutual relation will be changed to normal if it is not. |
||
| 350 | * @see buildRelation() |
||
| 351 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 352 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 353 | * @return static The relation will be |
||
| 354 | * given if exists, or return a new relation. |
||
| 355 | */ |
||
| 356 | 49 | public static function buildNormalRelation($user, $other) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Transform relation from suspend to normal. |
||
| 370 | * Note: You should ensure the relation model is not new one. |
||
| 371 | * @param static $relation |
||
| 372 | * @return boolean |
||
| 373 | */ |
||
| 374 | 2 | public static function transformSuspendToNormal($relation) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Revert relation from normal to suspend. |
||
| 386 | * Note: You should ensure the relation model is not new one. |
||
| 387 | * @param static $relation |
||
| 388 | * @return boolean |
||
| 389 | */ |
||
| 390 | 2 | public static function revertNormalToSuspend($relation) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Build new or return existed relation between initiator and recipient. |
||
| 402 | * If relation between initiator and recipient is not found, new relation will |
||
| 403 | * be built. If initiator and recipient are the same one and it is not allowed |
||
| 404 | * to build self relation, null will be given. |
||
| 405 | * If you want to know whether the relation exists, you can check the return |
||
| 406 | * value of `getIsNewRecord()` method. |
||
| 407 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
| 408 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
| 409 | * @return static The relation will be |
||
| 410 | * given if exists, or return a new relation. Or return null if not allowed |
||
| 411 | * to build self relation, |
||
| 412 | */ |
||
| 413 | 49 | protected static function buildRelation($user, $other) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Build opposite relation throughout the current relation. The opposite |
||
| 437 | * relation will be given if existed. |
||
| 438 | * @param static $relation |
||
| 439 | * @return static |
||
| 440 | */ |
||
| 441 | 12 | protected static function buildOppositeRelation($relation) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get mutual type. |
||
| 457 | * @return integer |
||
| 458 | */ |
||
| 459 | 12 | public function getMutualType() |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Set mutual type. |
||
| 470 | * @param integer $type |
||
| 471 | * @return integer |
||
| 472 | */ |
||
| 473 | 14 | protected function setMutualType($type) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Insert relation, the process is placed in a transaction. |
||
| 487 | * Note: This feature only support relational databases and skip all errors. |
||
| 488 | * If you don't want to use transaction or database doesn't support it, |
||
| 489 | * please use `save()` directly. |
||
| 490 | * @param static $relation |
||
| 491 | * @param Connection $connection |
||
| 492 | * @return boolean |
||
| 493 | * @throws InvalidValueException |
||
| 494 | * @throws InvalidConfigException |
||
| 495 | * @throws IntegrityException |
||
| 496 | */ |
||
| 497 | 1 | public static function insertRelation($relation, Connection $connection = null) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Remove relation, the process is placed in transaction. |
||
| 527 | * Note: This feature only support relational databases and skip all errors. |
||
| 528 | * If you don't want to use transaction or database doesn't support it, |
||
| 529 | * please use `remove()` directly. |
||
| 530 | * @param static $relation |
||
| 531 | * @param Connection $connection |
||
| 532 | * @return boolean|integer |
||
| 533 | * @throws InvalidConfigException |
||
| 534 | */ |
||
| 535 | 1 | public static function removeRelation($relation, Connection $connection = null) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Remove myself. |
||
| 561 | * @return integer|false The number of relations removed, or false if the remove |
||
| 562 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
| 563 | * removed is 0, even though the remove execution is successful. |
||
| 564 | */ |
||
| 565 | 49 | public function remove() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Remove first relation between initiator(s) and recipient(s). |
||
| 572 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 573 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 574 | * @return integer|false The number of relations removed. |
||
| 575 | */ |
||
| 576 | 1 | public static function removeOneRelation($user, $other) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Remove all relations between initiator(s) and recipient(s). |
||
| 587 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 588 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 589 | * @return integer The number of relations removed. |
||
| 590 | */ |
||
| 591 | 15 | public static function removeAllRelations($user, $other) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Get first relation between initiator(s) and recipient(s). |
||
| 600 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
| 601 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
| 602 | * @return static |
||
| 603 | */ |
||
| 604 | 4 | public static function findOneRelation($user, $other) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Get first opposite relation between initiator(s) and recipient(s). |
||
| 611 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
| 612 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
| 613 | * @return static |
||
| 614 | */ |
||
| 615 | 1 | public static function findOneOppositeRelation($user, $other) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Get user's or users' all relations, or by specified groups. |
||
| 622 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
| 623 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
| 624 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
| 625 | * @return array all eligible relations |
||
| 626 | */ |
||
| 627 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Initialize groups attribute. |
||
| 634 | * @param ModelEvent $event |
||
| 635 | */ |
||
| 636 | 49 | public function onInitGroups($event) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Initialize remark attribute. |
||
| 645 | * @param ModelEvent $event |
||
| 646 | */ |
||
| 647 | 49 | public function onInitRemark($event) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * The event triggered after insert new relation. |
||
| 656 | * The opposite relation should be inserted without triggering events |
||
| 657 | * simultaneously after new relation inserted, |
||
| 658 | * @param ModelEvent $event |
||
| 659 | * @throws IntegrityException throw if insert failed. |
||
| 660 | */ |
||
| 661 | 46 | public function onInsertRelation($event) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * The event triggered after update relation. |
||
| 678 | * The opposite relation should be updated without triggering events |
||
| 679 | * simultaneously after existed relation removed. |
||
| 680 | * @param ModelEvent $event |
||
| 681 | * @throw IntegrityException throw if update failed. |
||
| 682 | */ |
||
| 683 | 11 | public function onUpdateRelation($event) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * The event triggered after delete relation. |
||
| 700 | * The opposite relation should be deleted without triggering events |
||
| 701 | * simultaneously after existed relation removed. |
||
| 702 | * @param ModelEvent $event |
||
| 703 | */ |
||
| 704 | 49 | public function onDeleteRelation($event) |
|
| 714 | } |
||
| 715 |
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: