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 |
||
| 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 | 122 | public function rules() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 129 | 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 | 2 | public function getContent() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Set content. |
||
| 217 | * @param mixed $content |
||
| 218 | */ |
||
| 219 | 50 | 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 | 122 | public function getBlameableRulesCacheKey() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get blameable rules cache tag. |
||
| 259 | * @return string cache tag |
||
| 260 | */ |
||
| 261 | 122 | public function getBlameableRulesCacheTag() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get the rules associated with content to be blamed. |
||
| 268 | * @return array rules. |
||
| 269 | */ |
||
| 270 | 122 | 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 | 122 | public function getBlameableAttributeRules() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get the rules associated with `description` attribute. |
||
| 335 | * @return array rules. |
||
| 336 | */ |
||
| 337 | 122 | public function getDescriptionRules() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 356 | * @return array rules. |
||
| 357 | */ |
||
| 358 | 122 | public function getContentRules() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Set blameable rules. |
||
| 392 | * @param array $rules |
||
| 393 | */ |
||
| 394 | 122 | protected function setBlameableRules($rules = []) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get blameable behaviors cache key. |
||
| 406 | * @return string cache key. |
||
| 407 | */ |
||
| 408 | 129 | public function getBlameableBehaviorsCacheKey() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get blameable behaviors cache tag. |
||
| 415 | * @return string cache tag. |
||
| 416 | */ |
||
| 417 | 129 | 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 | 129 | public function getBlameableBehaviors() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Set blameable behaviors. |
||
| 452 | * @param array $behaviors |
||
| 453 | */ |
||
| 454 | 129 | 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 `$userClass` exists. You should |
||
| 489 | * specify it in `init()` method. |
||
| 490 | * @return BaseUserQuery user. |
||
| 491 | */ |
||
| 492 | 9 | public function getUser() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * |
||
| 499 | * @return BaseUserQuery |
||
| 500 | */ |
||
| 501 | 26 | public function getHost() |
|
| 507 | |||
| 508 | /** |
||
| 509 | * |
||
| 510 | * @param string $host |
||
| 511 | * @return type |
||
| 512 | */ |
||
| 513 | 117 | public function setHost($host) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * |
||
| 529 | * @param BaseUserModel|string $user |
||
| 530 | * @return boolean |
||
| 531 | */ |
||
| 532 | 4 | public function setUser($user) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Get updater who updated this blameable model recently. |
||
| 539 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 540 | * specify it in `init()` method. |
||
| 541 | * @return BaseUserQuery user. |
||
| 542 | */ |
||
| 543 | 1 | public function getUpdater() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * |
||
| 556 | * @param BaseUserModel|string $user |
||
| 557 | * @return boolean |
||
| 558 | */ |
||
| 559 | public function setUpdater($updater) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * This event is triggered before the model update. |
||
| 578 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 579 | * override or modify it directly, unless you know the consequences. |
||
| 580 | * @param ModelEvent $event |
||
| 581 | */ |
||
| 582 | 21 | public function onContentChanged($event) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 591 | * yet, or return the owner's GUID if current model has been specified. |
||
| 592 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 593 | * override or modify it directly, unless you know the consequences. |
||
| 594 | * @param ModelEvent $event |
||
| 595 | * @return string the GUID of current user or the owner. |
||
| 596 | */ |
||
| 597 | 110 | public function onGetCurrentUserGuid($event) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Initialize type of content. the first of element[index is 0] of |
||
| 613 | * $contentTypes will be used. |
||
| 614 | * @param ModelEvent $event |
||
| 615 | */ |
||
| 616 | 18 | public function onInitContentType($event) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Initialize description property with $initDescription. |
||
| 633 | * @param ModelEvent $event |
||
| 634 | */ |
||
| 635 | 61 | public function onInitDescription($event) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Attach events associated with blameable model. |
||
| 650 | */ |
||
| 651 | 129 | public function initBlameableEvents() |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @inheritdoc |
||
| 669 | */ |
||
| 670 | 32 | public function enabledFields() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 700 | * identity will be taken. |
||
| 701 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 702 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 703 | * regarded as sum of models in one page. |
||
| 704 | * @param integer $currentPage The current page number, begun with 0. |
||
| 705 | * @param {$this->userClass} $identity |
||
| 706 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 707 | */ |
||
| 708 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
| 715 | |||
| 716 | /** |
||
| 717 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 718 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 719 | * be given. |
||
| 720 | * @param integer $id user id. |
||
| 721 | * @param boolean $throwException |
||
| 722 | * @param {$this->userClass} $identity |
||
| 723 | * @return static |
||
| 724 | * @throws InvalidParamException |
||
| 725 | */ |
||
| 726 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Get total of follows of specified identity. |
||
| 741 | * @param {$this->userClass} $identity |
||
| 742 | * @return integer total. |
||
| 743 | */ |
||
| 744 | 1 | public static function countByIdentity($identity = null) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Get pagination, used for building contents page by page. |
||
| 751 | * @param integer $limit |
||
| 752 | * @param {$this->userClass} $identity |
||
| 753 | * @return Pagination |
||
| 754 | */ |
||
| 755 | public static function getPagination($limit = 10, $identity = null) |
||
| 764 | } |
||
| 765 |
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: