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 | 199 | public function rules() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 207 | 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 | 7 | public function getContent() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set content. |
||
| 217 | * @param mixed $content |
||
| 218 | */ |
||
| 219 | 65 | 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 | 1 | public function getContentCanBeEdited() |
|
| 241 | { |
||
| 242 | 1 | if ($this->contentAttribute === false) { |
|
| 243 | return false; |
||
| 244 | } |
||
| 245 | 1 | throw new NotSupportedException("This method is not implemented."); |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get blameable rules cache key. |
||
| 250 | * @return string cache key. |
||
| 251 | */ |
||
| 252 | 199 | public function getBlameableRulesCacheKey() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get blameable rules cache tag. |
||
| 259 | * @return string cache tag |
||
| 260 | */ |
||
| 261 | 199 | public function getBlameableRulesCacheTag() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get the rules associated with content to be blamed. |
||
| 268 | * @return array rules. |
||
| 269 | */ |
||
| 270 | 199 | 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 | 199 | public function getBlameableAttributeRules() |
|
| 334 | |||
| 335 | 199 | public function getIdRules() |
|
| 336 | { |
||
| 337 | 199 | if (is_string($this->idAttribute) && !empty($this->idAttribute) && $this->idCreatorCombinatedUnique && $this->idAttributeType !== static::$idTypeAutoIncrement) { |
|
| 338 | return [ |
||
| 339 | 139 | [[$this->idAttribute], 'required'], |
|
| 340 | ]; |
||
| 341 | } |
||
| 342 | 106 | return parent::getIdRules(); |
|
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get the rules associated with `description` attribute. |
||
| 347 | * @return array rules. |
||
| 348 | */ |
||
| 349 | 199 | public function getDescriptionRules() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 368 | * @return array rules. |
||
| 369 | */ |
||
| 370 | 199 | public function getContentRules() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Set blameable rules. |
||
| 404 | * @param array $rules |
||
| 405 | */ |
||
| 406 | 199 | protected function setBlameableRules($rules = []) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get blameable behaviors cache key. |
||
| 418 | * @return string cache key. |
||
| 419 | */ |
||
| 420 | 207 | public function getBlameableBehaviorsCacheKey() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Get blameable behaviors cache tag. |
||
| 427 | * @return string cache tag. |
||
| 428 | */ |
||
| 429 | 207 | public function getBlameableBehaviorsCacheTag() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
| 436 | * array will be given. |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | 207 | public function getBlameableBehaviors() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Set blameable behaviors. |
||
| 464 | * @param array $behaviors |
||
| 465 | */ |
||
| 466 | 207 | protected function setBlameableBehaviors($behaviors = []) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set description. |
||
| 479 | * @return string description. |
||
| 480 | */ |
||
| 481 | 1 | public function getDescription() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Get description. |
||
| 489 | * @param string $desc description. |
||
| 490 | * @return string|null description if enabled, or null if disabled. |
||
| 491 | */ |
||
| 492 | 1 | public function setDescription($desc) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Get blame who owned this blameable model. |
||
| 500 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
| 501 | * specify it in `init()` method. |
||
| 502 | * @return BaseUserQuery user. |
||
| 503 | */ |
||
| 504 | 18 | public function getUser() |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Declares a `has-one` relation. |
||
| 511 | * The declaration is returned in terms of a relational [[\yii\db\ActiveQuery]] instance |
||
| 512 | * through which the related record can be queried and retrieved back. |
||
| 513 | * |
||
| 514 | * A `has-one` relation means that there is at most one related record matching |
||
| 515 | * the criteria set by this relation, e.g., a customer has one country. |
||
| 516 | * |
||
| 517 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
| 518 | * the following code in the `Customer` class: |
||
| 519 | * |
||
| 520 | * ```php |
||
| 521 | * public function getCountry() |
||
| 522 | * { |
||
| 523 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
| 524 | * } |
||
| 525 | * ``` |
||
| 526 | * |
||
| 527 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
| 528 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
| 529 | * in the current AR class. |
||
| 530 | * |
||
| 531 | * Call methods declared in [[\yii\db\ActiveQuery]] to further customize the relation. |
||
| 532 | * |
||
| 533 | * This method is provided by [[\yii\db\BaseActiveRecord]]. |
||
| 534 | * @param string $class the class name of the related record |
||
| 535 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
| 536 | * the attributes of the record associated with the `$class` model, while the values of the |
||
| 537 | * array refer to the corresponding attributes in **this** AR class. |
||
| 538 | * @return \yii\dbActiveQueryInterface the relational query object. |
||
| 539 | */ |
||
| 540 | public abstract function hasOne($class, $link); |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get host of this model. |
||
| 544 | * @return BaseUserQuery |
||
| 545 | */ |
||
| 546 | 35 | public function getHost() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Set host of this model. |
||
| 555 | * @param string $host |
||
| 556 | * @return type |
||
| 557 | */ |
||
| 558 | 148 | public function setHost($host) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * |
||
| 574 | * @param BaseUserModel|string $user |
||
| 575 | * @return boolean |
||
| 576 | */ |
||
| 577 | 4 | public function setUser($user) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Get updater who updated this blameable model recently. |
||
| 584 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
| 585 | * specify it in `init()` method. |
||
| 586 | * @return BaseUserQuery user. |
||
| 587 | */ |
||
| 588 | 6 | public function getUpdater() |
|
| 598 | |||
| 599 | /** |
||
| 600 | * |
||
| 601 | * @param BaseUserModel|string $user |
||
| 602 | * @return boolean |
||
| 603 | */ |
||
| 604 | 5 | public function setUpdater($updater) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * This event is triggered before the model update. |
||
| 623 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 624 | * override or modify it directly, unless you know the consequences. |
||
| 625 | * @param ModelEvent $event |
||
| 626 | */ |
||
| 627 | 58 | public function onContentChanged($event) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 636 | * yet, or return the owner's GUID if current model has been specified. |
||
| 637 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 638 | * override or modify it directly, unless you know the consequences. |
||
| 639 | * @param ModelEvent $event |
||
| 640 | * @return string the GUID of current user or the owner. |
||
| 641 | */ |
||
| 642 | 141 | public function onGetCurrentUserGuid($event) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Initialize type of content. the first of element[index is 0] of |
||
| 658 | * $contentTypes will be used. |
||
| 659 | * @param ModelEvent $event |
||
| 660 | */ |
||
| 661 | 24 | public function onInitContentType($event) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Initialize description property with $initDescription. |
||
| 678 | * @param ModelEvent $event |
||
| 679 | */ |
||
| 680 | 73 | public function onInitDescription($event) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Attaches an event handler to an event. |
||
| 695 | * |
||
| 696 | * The event handler must be a valid PHP callback. The following are |
||
| 697 | * some examples: |
||
| 698 | * |
||
| 699 | * ``` |
||
| 700 | * function ($event) { ... } // anonymous function |
||
| 701 | * [$object, 'handleClick'] // $object->handleClick() |
||
| 702 | * ['Page', 'handleClick'] // Page::handleClick() |
||
| 703 | * 'handleClick' // global function handleClick() |
||
| 704 | * ``` |
||
| 705 | * |
||
| 706 | * The event handler must be defined with the following signature, |
||
| 707 | * |
||
| 708 | * ``` |
||
| 709 | * function ($event) |
||
| 710 | * ``` |
||
| 711 | * |
||
| 712 | * where `$event` is an [[Event]] object which includes parameters associated with the event. |
||
| 713 | * |
||
| 714 | * This method is provided by [[\yii\base\Component]]. |
||
| 715 | * @param string $name the event name |
||
| 716 | * @param callable $handler the event handler |
||
| 717 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
| 718 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
| 719 | * @param boolean $append whether to append new event handler to the end of the existing |
||
| 720 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
| 721 | * handler list. |
||
| 722 | * @see off() |
||
| 723 | */ |
||
| 724 | public abstract function on($name, $handler, $data = null, $append = true); |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Detaches an existing event handler from this component. |
||
| 728 | * This method is the opposite of [[on()]]. |
||
| 729 | * This method is provided by [[\yii\base\Component]] |
||
| 730 | * @param string $name event name |
||
| 731 | * @param callable $handler the event handler to be removed. |
||
| 732 | * If it is null, all handlers attached to the named event will be removed. |
||
| 733 | * @return boolean if a handler is found and detached |
||
| 734 | * @see on() |
||
| 735 | */ |
||
| 736 | public abstract function off($name, $handler = null); |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Attach events associated with blameable model. |
||
| 740 | */ |
||
| 741 | 207 | public function initBlameableEvents() |
|
| 756 | |||
| 757 | /** |
||
| 758 | * @inheritdoc |
||
| 759 | */ |
||
| 760 | 86 | public function enabledFields() |
|
| 787 | |||
| 788 | /** |
||
| 789 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 790 | * identity will be taken. |
||
| 791 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 792 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 793 | * regarded as sum of models in one page. |
||
| 794 | * @param integer $currentPage The current page number, begun with 0. |
||
| 795 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
| 796 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 797 | */ |
||
| 798 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 808 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 809 | * be given. |
||
| 810 | * @param integer $id user id. |
||
| 811 | * @param boolean $throwException |
||
| 812 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
| 813 | * @return static |
||
| 814 | * @throws InvalidParamException |
||
| 815 | */ |
||
| 816 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Get total of follows of specified identity. |
||
| 831 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
| 832 | * @return integer total. |
||
| 833 | */ |
||
| 834 | 3 | public static function countByIdentity($identity = null) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Get pagination, used for building contents page by page. |
||
| 841 | * @param integer $limit |
||
| 842 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
| 843 | * @return Pagination |
||
| 844 | */ |
||
| 845 | 2 | public static function getPagination($limit = 10, $identity = null) |
|
| 854 | } |
||
| 855 |
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: