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 |
||
| 48 | trait BlameableTrait |
||
| 49 | { |
||
| 50 | use ConfirmationTrait, |
||
| 51 | SelfBlameableTrait; |
||
| 52 | |||
| 53 | private $blameableLocalRules = []; |
||
| 54 | private $blameableLocalBehaviors = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
| 58 | * there is only one content attribute, you can assign its name. Or there |
||
| 59 | * is multiple attributes associated with contents, you can assign their |
||
| 60 | * names in array. If you don't want to use this feature, please assign |
||
| 61 | * false. |
||
| 62 | * For example: |
||
| 63 | * ```php |
||
| 64 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
| 65 | * ``` |
||
| 66 | * or |
||
| 67 | * ```php |
||
| 68 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
| 69 | * ``` |
||
| 70 | * or |
||
| 71 | * ```php |
||
| 72 | * public $contentAttribute = false; // no need of this feature. |
||
| 73 | * ``` |
||
| 74 | * If you don't need this feature, you should add rules corresponding with |
||
| 75 | * `content` in `rules()` method of your user model by yourself. |
||
| 76 | */ |
||
| 77 | public $contentAttribute = 'content'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array built-in validator name or validatation method name and |
||
| 81 | * additional parameters. |
||
| 82 | */ |
||
| 83 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var boolean|string Specify the field which stores the type of content. |
||
| 87 | */ |
||
| 88 | public $contentTypeAttribute = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var boolean|array Specify the logic type of content, not data type. If |
||
| 92 | * your content doesn't need this feature. please specify false. If the |
||
| 93 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
| 94 | * ```php |
||
| 95 | * public $contentTypes = [ |
||
| 96 | * 'public', |
||
| 97 | * 'private', |
||
| 98 | * 'friend', |
||
| 99 | * ]; |
||
| 100 | * ``` |
||
| 101 | */ |
||
| 102 | public $contentTypes = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var boolean|string This attribute speicfy the name of description |
||
| 106 | * attribute. If this attribute is assigned to false, this feature will be |
||
| 107 | * skipped. |
||
| 108 | */ |
||
| 109 | public $descriptionAttribute = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | public $initDescription = ''; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string the attribute that will receive current user ID value. This |
||
| 118 | * attribute must be assigned. |
||
| 119 | */ |
||
| 120 | public $createdByAttribute = "user_guid"; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string the attribute that will receive current user ID value. |
||
| 124 | * Set this property to false if you do not want to record the updater ID. |
||
| 125 | */ |
||
| 126 | public $updatedByAttribute = "user_guid"; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var boolean Add combinated unique rule if assigned to true. |
||
| 130 | */ |
||
| 131 | public $idCreatorCombinatedUnique = true; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var boolean|string The name of user class which own the current entity. |
||
| 135 | * If this attribute is assigned to false, this feature will be skipped, and |
||
| 136 | * when you use create() method of UserTrait, it will be assigned with |
||
| 137 | * current user class. |
||
| 138 | */ |
||
| 139 | public $userClass; |
||
| 140 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
| 141 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
| 142 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
| 143 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @inheritdoc |
||
| 147 | * ------------ |
||
| 148 | * The classical rules is like following: |
||
| 149 | * [ |
||
| 150 | * ['guid', 'required'], |
||
| 151 | * ['guid', 'unique'], |
||
| 152 | * ['guid', 'string', 'max' => 36], |
||
| 153 | * |
||
| 154 | * ['id', 'required'], |
||
| 155 | * ['id', 'unique'], |
||
| 156 | * ['id', 'string', 'max' => 4], |
||
| 157 | * |
||
| 158 | * ['created_at', 'safe'], |
||
| 159 | * ['updated_at', 'safe'], |
||
| 160 | * |
||
| 161 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
| 162 | * ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 163 | * ] |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | 69 | public function rules() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritdoc |
||
| 173 | */ |
||
| 174 | 70 | public function behaviors() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get total of contents which owned by their owner. |
||
| 181 | * @return integer |
||
| 182 | */ |
||
| 183 | 1 | public function countOfOwner() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get content. |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | 4 | public function getContent() |
|
| 194 | { |
||
| 195 | 4 | $contentAttribute = $this->contentAttribute; |
|
| 196 | 4 | if ($contentAttribute === false) { |
|
| 197 | return null; |
||
| 198 | } |
||
| 199 | 4 | if (is_array($contentAttribute)) { |
|
| 200 | $content = []; |
||
| 201 | foreach ($contentAttribute as $key => $value) { |
||
| 202 | $content[$key] = $this->$value; |
||
| 203 | } |
||
| 204 | return $content; |
||
| 205 | } |
||
| 206 | 4 | return $this->$contentAttribute; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set content. |
||
| 211 | * @param mixed $content |
||
| 212 | */ |
||
| 213 | 24 | public function setContent($content) |
|
| 214 | { |
||
| 215 | 24 | $contentAttribute = $this->contentAttribute; |
|
| 216 | 24 | if ($contentAttribute === false) { |
|
| 217 | return; |
||
| 218 | } |
||
| 219 | 24 | if (is_array($contentAttribute)) { |
|
| 220 | foreach ($contentAttribute as $key => $value) { |
||
| 221 | $this->$value = $content[$key]; |
||
| 222 | } |
||
| 223 | return; |
||
| 224 | } |
||
| 225 | 24 | $this->$contentAttribute = $content; |
|
| 226 | 24 | } |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Determines whether content could be edited. Your should implement this |
||
| 230 | * method by yourself. |
||
| 231 | * @return boolean |
||
| 232 | * @throws NotSupportedException |
||
| 233 | */ |
||
| 234 | public function getContentCanBeEdited() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get blameable rules cache key. |
||
| 244 | * @return string cache key. |
||
| 245 | */ |
||
| 246 | 69 | public function getBlameableRulesCacheKey() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get blameable rules cache tag. |
||
| 253 | * @return string cache tag |
||
| 254 | */ |
||
| 255 | 69 | public function getBlameableRulesCacheTag() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get the rules associated with content to be blamed. |
||
| 262 | * @return array rules. |
||
| 263 | */ |
||
| 264 | 69 | public function getBlameableRules() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
| 293 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
| 294 | * @return array rules. |
||
| 295 | */ |
||
| 296 | 69 | public function getBlameableAttributeRules() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get the rules associated with `description` attribute. |
||
| 329 | * @return array rules. |
||
| 330 | */ |
||
| 331 | 69 | public function getDescriptionRules() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 350 | * @return array rules. |
||
| 351 | */ |
||
| 352 | 69 | public function getContentRules() |
|
| 353 | { |
||
| 354 | 69 | if (!$this->contentAttribute) { |
|
| 355 | 15 | return []; |
|
| 356 | } |
||
| 357 | 54 | $rules = []; |
|
| 358 | 54 | $rules[] = [$this->contentAttribute, 'required']; |
|
| 359 | 54 | if ($this->contentAttributeRule) { |
|
| 360 | 54 | if (is_string($this->contentAttributeRule)) { |
|
| 361 | $this->contentAttributeRule = [$this->contentAttributeRule]; |
||
| 362 | } |
||
| 363 | 54 | if (is_array($this->contentAttributeRule)) { |
|
| 364 | 54 | $rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule); |
|
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | 54 | if (!$this->contentTypeAttribute) { |
|
| 369 | 42 | return $rules; |
|
| 370 | } |
||
| 371 | |||
| 372 | 12 | if (is_array($this->contentTypes) && !empty($this->contentTypes)) { |
|
| 373 | 12 | $rules[] = [[ |
|
| 374 | 12 | $this->contentTypeAttribute], |
|
| 375 | 12 | 'required']; |
|
| 376 | 12 | $rules[] = [[ |
|
| 377 | 12 | $this->contentTypeAttribute], |
|
| 378 | 12 | 'in', |
|
| 379 | 12 | 'range' => array_keys($this->contentTypes)]; |
|
| 380 | } |
||
| 381 | 12 | return $rules; |
|
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set blameable rules. |
||
| 386 | * @param array $rules |
||
| 387 | */ |
||
| 388 | 69 | protected function setBlameableRules($rules = []) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get blameable behaviors cache key. |
||
| 400 | * @return string cache key. |
||
| 401 | */ |
||
| 402 | 70 | public function getBlameableBehaviorsCacheKey() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Get blameable behaviors cache tag. |
||
| 409 | * @return string cache tag. |
||
| 410 | */ |
||
| 411 | 70 | public function getBlameableBehaviorsCacheTag() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
| 418 | * array will be given. |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | 70 | public function getBlameableBehaviors() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Set blameable behaviors. |
||
| 446 | * @param array $behaviors |
||
| 447 | */ |
||
| 448 | 70 | protected function setBlameableBehaviors($behaviors = []) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Set description. |
||
| 461 | * @return string description. |
||
| 462 | */ |
||
| 463 | 1 | public function getDescription() |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Get description. |
||
| 471 | * @param string $desc description. |
||
| 472 | * @return string|null description if enabled, or null if disabled. |
||
| 473 | */ |
||
| 474 | 1 | public function setDescription($desc) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Get blame who owned this blameable model. |
||
| 482 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 483 | * specify it in `init()` method. |
||
| 484 | * @return BaseUserQuery user. |
||
| 485 | */ |
||
| 486 | 11 | public function getUser() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * |
||
| 496 | * @param BaseUserModel|string $user |
||
| 497 | * @return boolean |
||
| 498 | */ |
||
| 499 | 42 | public function setUser($user) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Get updater who updated this blameable model recently. |
||
| 515 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 516 | * specify it in `init()` method. |
||
| 517 | * @return BaseUserQuery user. |
||
| 518 | */ |
||
| 519 | 1 | public function getUpdater() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * |
||
| 532 | * @param BaseUserModel|string $user |
||
| 533 | * @return boolean |
||
| 534 | */ |
||
| 535 | public function setUpdater($user) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * This event is triggered before the model update. |
||
| 554 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 555 | * override or modify it directly, unless you know the consequences. |
||
| 556 | * @param ModelEvent $event |
||
| 557 | */ |
||
| 558 | 8 | public function onContentChanged($event) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 567 | * yet, or return the owner's GUID if current model has been specified. |
||
| 568 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 569 | * override or modify it directly, unless you know the consequences. |
||
| 570 | * @param ModelEvent $event |
||
| 571 | * @return string the GUID of current user or the owner. |
||
| 572 | */ |
||
| 573 | 57 | public function onGetCurrentUserGuid($event) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Initialize type of content. the first of element[index is 0] of |
||
| 589 | * $contentTypes will be used. |
||
| 590 | * @param ModelEvent $event |
||
| 591 | */ |
||
| 592 | 12 | public function onInitContentType($event) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Initialize description property with $initDescription. |
||
| 609 | * @param ModelEvent $event |
||
| 610 | */ |
||
| 611 | 28 | public function onInitDescription($event) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Attach events associated with blameable model. |
||
| 626 | */ |
||
| 627 | 70 | public function initBlameableEvents() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * @inheritdoc |
||
| 645 | */ |
||
| 646 | 32 | public function enabledFields() |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 676 | * identity will be taken. |
||
| 677 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 678 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 679 | * regarded as sum of models in one page. |
||
| 680 | * @param integer $currentPage The current page number, begun with 0. |
||
| 681 | * @param {$this->userClass} $identity |
||
| 682 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 683 | */ |
||
| 684 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 694 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 695 | * be given. |
||
| 696 | * @param integer $id user id. |
||
| 697 | * @param boolean $throwException |
||
| 698 | * @param {$this->userClass} $identity |
||
| 699 | * @return static |
||
| 700 | * @throws InvalidParamException |
||
| 701 | */ |
||
| 702 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
| 714 | |||
| 715 | /** |
||
| 716 | * Get total of follows of specified identity. |
||
| 717 | * @param {$this->userClass} $identity |
||
| 718 | * @return integer total. |
||
| 719 | */ |
||
| 720 | 1 | public static function countByIdentity($identity = null) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Get pagination, used for building contents page by page. |
||
| 727 | * @param integer $limit |
||
| 728 | * @param {$this->userClass} $identity |
||
| 729 | * @return Pagination |
||
| 730 | */ |
||
| 731 | public static function getPagination($limit = 10, $identity = null) |
||
| 740 | } |
||
| 741 |
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: