Complex classes like BlameableTrait 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 BlameableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | trait BlameableTrait |
||
| 50 | { |
||
| 51 | use ConfirmationTrait, |
||
| 52 | SelfBlameableTrait; |
||
| 53 | |||
| 54 | private $blameableLocalRules = []; |
||
| 55 | private $blameableLocalBehaviors = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
| 59 | * there is only one content attribute, you can assign its name. Or there |
||
| 60 | * is multiple attributes associated with contents, you can assign their |
||
| 61 | * names in array. If you don't want to use this feature, please assign |
||
| 62 | * false. |
||
| 63 | * For example: |
||
| 64 | * ```php |
||
| 65 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
| 66 | * ``` |
||
| 67 | * or |
||
| 68 | * ```php |
||
| 69 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
| 70 | * ``` |
||
| 71 | * or |
||
| 72 | * ```php |
||
| 73 | * public $contentAttribute = false; // no need of this feature. |
||
| 74 | * ``` |
||
| 75 | * If you don't need this feature, you should add rules corresponding with |
||
| 76 | * `content` in `rules()` method of your user model by yourself. |
||
| 77 | */ |
||
| 78 | public $contentAttribute = 'content'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array built-in validator name or validatation method name and |
||
| 82 | * additional parameters. |
||
| 83 | */ |
||
| 84 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var boolean|string Specify the field which stores the type of content. |
||
| 88 | */ |
||
| 89 | public $contentTypeAttribute = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var boolean|array Specify the logic type of content, not data type. If |
||
| 93 | * your content doesn't need this feature. please specify false. If the |
||
| 94 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
| 95 | * ```php |
||
| 96 | * public $contentTypes = [ |
||
| 97 | * 'public', |
||
| 98 | * 'private', |
||
| 99 | * 'friend', |
||
| 100 | * ]; |
||
| 101 | * ``` |
||
| 102 | */ |
||
| 103 | public $contentTypes = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var boolean|string This attribute speicfy the name of description |
||
| 107 | * attribute. If this attribute is assigned to false, this feature will be |
||
| 108 | * skipped. |
||
| 109 | */ |
||
| 110 | public $descriptionAttribute = false; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | public $initDescription = ''; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string the attribute that will receive current user ID value. This |
||
| 119 | * attribute must be assigned. |
||
| 120 | */ |
||
| 121 | public $createdByAttribute = "user_guid"; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string the attribute that will receive current user ID value. |
||
| 125 | * Set this property to false if you do not want to record the updater ID. |
||
| 126 | */ |
||
| 127 | public $updatedByAttribute = "user_guid"; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var boolean Add combinated unique rule if assigned to true. |
||
| 131 | */ |
||
| 132 | public $idCreatorCombinatedUnique = true; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var boolean|string The name of user class which own the current entity. |
||
| 136 | * If this attribute is assigned to false, this feature will be skipped, and |
||
| 137 | * when you use create() method of UserTrait, it will be assigned with |
||
| 138 | * current user class. |
||
| 139 | */ |
||
| 140 | //public $userClass; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var boolean|string Host class. |
||
| 144 | */ |
||
| 145 | public $hostClass; |
||
| 146 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
| 147 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
| 148 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
| 149 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritdoc |
||
| 153 | * ------------ |
||
| 154 | * The classical rules is like following: |
||
| 155 | * [ |
||
| 156 | * ['guid', 'required'], |
||
| 157 | * ['guid', 'unique'], |
||
| 158 | * ['guid', 'string', 'max' => 36], |
||
| 159 | * |
||
| 160 | * ['id', 'required'], |
||
| 161 | * ['id', 'unique'], |
||
| 162 | * ['id', 'string', 'max' => 4], |
||
| 163 | * |
||
| 164 | * ['created_at', 'safe'], |
||
| 165 | * ['updated_at', 'safe'], |
||
| 166 | * |
||
| 167 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
| 168 | * ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 169 | * ] |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 177 | public function rules() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 184 | public function behaviors() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get total of contents which owned by their owner. |
||
| 187 | * @return integer |
||
| 188 | */ |
||
| 189 | 1 | public function countOfOwner() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get content. |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | 6 | public function getContent() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set content. |
||
| 217 | * @param mixed $content |
||
| 218 | */ |
||
| 219 | 60 | public function setContent($content) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Determines whether content could be edited. Your should implement this |
||
| 236 | * method by yourself. |
||
| 237 | * @return boolean |
||
| 238 | * @throws NotSupportedException |
||
| 239 | */ |
||
| 240 | public function getContentCanBeEdited() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get blameable rules cache key. |
||
| 250 | * @return string cache key. |
||
| 251 | */ |
||
| 252 | 177 | public function getBlameableRulesCacheKey() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get blameable rules cache tag. |
||
| 259 | * @return string cache tag |
||
| 260 | */ |
||
| 261 | 168 | public function getBlameableRulesCacheTag() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get the rules associated with content to be blamed. |
||
| 268 | * @return array rules. |
||
| 269 | */ |
||
| 270 | 177 | public function getBlameableRules() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
| 299 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
| 300 | * @return array rules. |
||
| 301 | */ |
||
| 302 | 168 | public function getBlameableAttributeRules() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get the rules associated with `description` attribute. |
||
| 335 | * @return array rules. |
||
| 336 | */ |
||
| 337 | 168 | public function getDescriptionRules() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 356 | * @return array rules. |
||
| 357 | */ |
||
| 358 | 168 | public function getContentRules() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Set blameable rules. |
||
| 392 | * @param array $rules |
||
| 393 | */ |
||
| 394 | 168 | protected function setBlameableRules($rules = []) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get blameable behaviors cache key. |
||
| 406 | * @return string cache key. |
||
| 407 | */ |
||
| 408 | 184 | public function getBlameableBehaviorsCacheKey() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get blameable behaviors cache tag. |
||
| 415 | * @return string cache tag. |
||
| 416 | */ |
||
| 417 | 173 | public function getBlameableBehaviorsCacheTag() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
| 424 | * array will be given. |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | 184 | public function getBlameableBehaviors() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Set blameable behaviors. |
||
| 452 | * @param array $behaviors |
||
| 453 | */ |
||
| 454 | 173 | protected function setBlameableBehaviors($behaviors = []) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Set description. |
||
| 467 | * @return string description. |
||
| 468 | */ |
||
| 469 | 1 | public function getDescription() |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get description. |
||
| 477 | * @param string $desc description. |
||
| 478 | * @return string|null description if enabled, or null if disabled. |
||
| 479 | */ |
||
| 480 | 1 | public function setDescription($desc) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get blame who owned this blameable model. |
||
| 488 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
| 489 | * specify it in `init()` method. |
||
| 490 | * @return BaseUserQuery user. |
||
| 491 | */ |
||
| 492 | 17 | public function getUser() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Declares a `has-one` relation. |
||
| 499 | * The declaration is returned in terms of a relational [[\yii\db\ActiveQuery]] instance |
||
| 500 | * through which the related record can be queried and retrieved back. |
||
| 501 | * |
||
| 502 | * A `has-one` relation means that there is at most one related record matching |
||
| 503 | * the criteria set by this relation, e.g., a customer has one country. |
||
| 504 | * |
||
| 505 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
| 506 | * the following code in the `Customer` class: |
||
| 507 | * |
||
| 508 | * ```php |
||
| 509 | * public function getCountry() |
||
| 510 | * { |
||
| 511 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
| 512 | * } |
||
| 513 | * ``` |
||
| 514 | * |
||
| 515 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
| 516 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
| 517 | * in the current AR class. |
||
| 518 | * |
||
| 519 | * Call methods declared in [[\yii\db\ActiveQuery]] to further customize the relation. |
||
| 520 | * |
||
| 521 | * This method is provided by [[\yii\db\BaseActiveRecord]]. |
||
| 522 | * @param string $class the class name of the related record |
||
| 523 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
| 524 | * the attributes of the record associated with the `$class` model, while the values of the |
||
| 525 | * array refer to the corresponding attributes in **this** AR class. |
||
| 526 | * @return \yii\dbActiveQueryInterface the relational query object. |
||
| 527 | */ |
||
| 528 | public abstract function hasOne($class, $link); |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get host of this model. |
||
| 532 | * @return BaseUserQuery |
||
| 533 | */ |
||
| 534 | 32 | public function getHost() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Set host of this model. |
||
| 543 | * @param string $host |
||
| 544 | * @return type |
||
| 545 | */ |
||
| 546 | 139 | public function setHost($host) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * |
||
| 562 | * @param BaseUserModel|string $user |
||
| 563 | * @return boolean |
||
| 564 | */ |
||
| 565 | 4 | public function setUser($user) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Get updater who updated this blameable model recently. |
||
| 572 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
| 573 | * specify it in `init()` method. |
||
| 574 | * @return BaseUserQuery user. |
||
| 575 | */ |
||
| 576 | 6 | public function getUpdater() |
|
| 586 | |||
| 587 | /** |
||
| 588 | * |
||
| 589 | * @param BaseUserModel|string $user |
||
| 590 | * @return boolean |
||
| 591 | */ |
||
| 592 | 5 | public function setUpdater($updater) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * This event is triggered before the model update. |
||
| 611 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 612 | * override or modify it directly, unless you know the consequences. |
||
| 613 | * @param ModelEvent $event |
||
| 614 | */ |
||
| 615 | 54 | public function onContentChanged($event) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 624 | * yet, or return the owner's GUID if current model has been specified. |
||
| 625 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 626 | * override or modify it directly, unless you know the consequences. |
||
| 627 | * @param ModelEvent $event |
||
| 628 | * @return string the GUID of current user or the owner. |
||
| 629 | */ |
||
| 630 | 133 | public function onGetCurrentUserGuid($event) |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Initialize type of content. the first of element[index is 0] of |
||
| 646 | * $contentTypes will be used. |
||
| 647 | * @param ModelEvent $event |
||
| 648 | */ |
||
| 649 | 20 | public function onInitContentType($event) |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Initialize description property with $initDescription. |
||
| 666 | * @param ModelEvent $event |
||
| 667 | */ |
||
| 668 | 66 | public function onInitDescription($event) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Attaches an event handler to an event. |
||
| 683 | * |
||
| 684 | * The event handler must be a valid PHP callback. The following are |
||
| 685 | * some examples: |
||
| 686 | * |
||
| 687 | * ``` |
||
| 688 | * function ($event) { ... } // anonymous function |
||
| 689 | * [$object, 'handleClick'] // $object->handleClick() |
||
| 690 | * ['Page', 'handleClick'] // Page::handleClick() |
||
| 691 | * 'handleClick' // global function handleClick() |
||
| 692 | * ``` |
||
| 693 | * |
||
| 694 | * The event handler must be defined with the following signature, |
||
| 695 | * |
||
| 696 | * ``` |
||
| 697 | * function ($event) |
||
| 698 | * ``` |
||
| 699 | * |
||
| 700 | * where `$event` is an [[Event]] object which includes parameters associated with the event. |
||
| 701 | * |
||
| 702 | * This method is provided by [[\yii\base\Component]]. |
||
| 703 | * @param string $name the event name |
||
| 704 | * @param callable $handler the event handler |
||
| 705 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
| 706 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
| 707 | * @param boolean $append whether to append new event handler to the end of the existing |
||
| 708 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
| 709 | * handler list. |
||
| 710 | * @see off() |
||
| 711 | */ |
||
| 712 | public abstract function on($name, $handler, $data = null, $append = true); |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Detaches an existing event handler from this component. |
||
| 716 | * This method is the opposite of [[on()]]. |
||
| 717 | * This method is provided by [[\yii\base\Component]] |
||
| 718 | * @param string $name event name |
||
| 719 | * @param callable $handler the event handler to be removed. |
||
| 720 | * If it is null, all handlers attached to the named event will be removed. |
||
| 721 | * @return boolean if a handler is found and detached |
||
| 722 | * @see on() |
||
| 723 | */ |
||
| 724 | public abstract function off($name, $handler = null); |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Attach events associated with blameable model. |
||
| 728 | */ |
||
| 729 | 184 | public function initBlameableEvents() |
|
| 744 | |||
| 745 | /** |
||
| 746 | * @inheritdoc |
||
| 747 | */ |
||
| 748 | 71 | public function enabledFields() |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 778 | * identity will be taken. |
||
| 779 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 780 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 781 | * regarded as sum of models in one page. |
||
| 782 | * @param integer $currentPage The current page number, begun with 0. |
||
| 783 | * @param {$this->hostClass} $identity |
||
| 784 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 785 | */ |
||
| 786 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 796 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 797 | * be given. |
||
| 798 | * @param integer $id user id. |
||
| 799 | * @param boolean $throwException |
||
| 800 | * @param {$this->hostClass} $identity |
||
| 801 | * @return static |
||
| 802 | * @throws InvalidParamException |
||
| 803 | */ |
||
| 804 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
| 816 | |||
| 817 | /** |
||
| 818 | * Get total of follows of specified identity. |
||
| 819 | * @param {$this->hostClass} $identity |
||
| 820 | * @return integer total. |
||
| 821 | */ |
||
| 822 | 2 | public static function countByIdentity($identity = null) |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Get pagination, used for building contents page by page. |
||
| 829 | * @param integer $limit |
||
| 830 | * @param {$this->hostClass} $identity |
||
| 831 | * @return Pagination |
||
| 832 | */ |
||
| 833 | 1 | public static function getPagination($limit = 10, $identity = null) |
|
| 842 | } |
||
| 843 |
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: