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 |
||
| 47 | trait BlameableTrait |
||
| 48 | { |
||
| 49 | use ConfirmationTrait, |
||
| 50 | SelfBlameableTrait; |
||
| 51 | |||
| 52 | private $blameableLocalRules = []; |
||
| 53 | private $blameableLocalBehaviors = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
| 57 | * there is only one content attribute, you can assign its name. Or there |
||
| 58 | * is multiple attributes associated with contents, you can assign their |
||
| 59 | * names in array. If you don't want to use this feature, please assign |
||
| 60 | * false. |
||
| 61 | * For example: |
||
| 62 | * ```php |
||
| 63 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
| 64 | * ``` |
||
| 65 | * or |
||
| 66 | * ```php |
||
| 67 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
| 68 | * ``` |
||
| 69 | * or |
||
| 70 | * ```php |
||
| 71 | * public $contentAttribute = false; // no need of this feature. |
||
| 72 | * ``` |
||
| 73 | * If you don't need this feature, you should add rules corresponding with |
||
| 74 | * `content` in `rules()` method of your user model by yourself. |
||
| 75 | */ |
||
| 76 | public $contentAttribute = 'content'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array built-in validator name or validatation method name and |
||
| 80 | * additional parameters. |
||
| 81 | */ |
||
| 82 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean|string Specify the field which stores the type of content. |
||
| 86 | */ |
||
| 87 | public $contentTypeAttribute = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var boolean|array Specify the logic type of content, not data type. If |
||
| 91 | * your content doesn't need this feature. please specify false. If the |
||
| 92 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
| 93 | * ```php |
||
| 94 | * public $contentTypes = [ |
||
| 95 | * 'public', |
||
| 96 | * 'private', |
||
| 97 | * 'friend', |
||
| 98 | * ]; |
||
| 99 | * ``` |
||
| 100 | */ |
||
| 101 | public $contentTypes = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var boolean|string This attribute speicfy the name of description |
||
| 105 | * attribute. If this attribute is assigned to false, this feature will be |
||
| 106 | * skipped. |
||
| 107 | */ |
||
| 108 | public $descriptionAttribute = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | public $initDescription = ''; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var string the attribute that will receive current user ID value. This |
||
| 117 | * attribute must be assigned. |
||
| 118 | */ |
||
| 119 | public $createdByAttribute = "user_guid"; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var string the attribute that will receive current user ID value. |
||
| 123 | * Set this property to false if you do not want to record the updater ID. |
||
| 124 | */ |
||
| 125 | public $updatedByAttribute = "user_guid"; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var boolean Add combinated unique rule if assigned to true. |
||
| 129 | */ |
||
| 130 | public $idCreatorCombinatedUnique = true; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var boolean|string The name of user class which own the current entity. |
||
| 134 | * If this attribute is assigned to false, this feature will be skipped, and |
||
| 135 | * when you use create() method of UserTrait, it will be assigned with |
||
| 136 | * current user class. |
||
| 137 | */ |
||
| 138 | public $userClass; |
||
| 139 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
| 140 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
| 141 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
| 142 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | * ------------ |
||
| 147 | * The classical rules is like following: |
||
| 148 | * [ |
||
| 149 | * ['guid', 'required'], |
||
| 150 | * ['guid', 'unique'], |
||
| 151 | * ['guid', 'string', 'max' => 36], |
||
| 152 | * |
||
| 153 | * ['id', 'required'], |
||
| 154 | * ['id', 'unique'], |
||
| 155 | * ['id', 'string', 'max' => 4], |
||
| 156 | * |
||
| 157 | * ['create_time', 'safe'], |
||
| 158 | * ['update_time', 'safe'], |
||
| 159 | * |
||
| 160 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
| 161 | * ['ip_1', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 162 | * ['ip_2', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 163 | * ['ip_3', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 164 | * ['ip_4', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 165 | * |
||
| 166 | * |
||
| 167 | * ] |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 45 | public function rules() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | 46 | public function behaviors() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Get total of contents which owned by their owner. |
||
| 185 | * @return integer |
||
| 186 | */ |
||
| 187 | 2 | public function countOfOwner() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get content. |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | public function getContent() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Set content. |
||
| 215 | * @param mixed $content |
||
| 216 | */ |
||
| 217 | public function setContent($content) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Determines whether content could be edited. Your should implement this |
||
| 234 | * method by yourself. |
||
| 235 | * @return boolean |
||
| 236 | * @throws NotSupportedException |
||
| 237 | */ |
||
| 238 | public function getContentCanBeEdited() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Check it has been ever edited. |
||
| 248 | * @return boolean Whether this content has ever been edited. |
||
| 249 | */ |
||
| 250 | public function hasEverEdited() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get blameable rules cache key. |
||
| 262 | * @return string cache key. |
||
| 263 | */ |
||
| 264 | 45 | public function getBlameableRulesCacheKey() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Get blameable rules cache tag. |
||
| 271 | * @return string cache tag |
||
| 272 | */ |
||
| 273 | 15 | public function getBlameableRulesCacheTag() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get the rules associated with content to be blamed. |
||
| 280 | * @return array rules. |
||
| 281 | */ |
||
| 282 | 45 | public function getBlameableRules() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
| 311 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
| 312 | * @return array rules. |
||
| 313 | */ |
||
| 314 | 15 | public function getBlameableAttributeRules() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Get the rules associated with `description` attribute. |
||
| 347 | * @return array rules. |
||
| 348 | */ |
||
| 349 | 15 | public function getDescriptionRules() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 368 | * @return array rules. |
||
| 369 | */ |
||
| 370 | 15 | public function getContentRules() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Set blameable rules. |
||
| 404 | * @param array $rules |
||
| 405 | */ |
||
| 406 | 15 | protected function setBlameableRules($rules = []) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get blameable behaviors cache key. |
||
| 418 | * @return string cache key. |
||
| 419 | */ |
||
| 420 | 46 | public function getBlameableBehaviorsCacheKey() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Get blameable behaviors cache tag. |
||
| 427 | * @return string cache tag. |
||
| 428 | */ |
||
| 429 | 15 | 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 | 46 | public function getBlameableBehaviors() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Set blameable behaviors. |
||
| 464 | * @param array $behaviors |
||
| 465 | */ |
||
| 466 | 15 | protected function setBlameableBehaviors($behaviors = []) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set description. |
||
| 479 | * @return string description. |
||
| 480 | */ |
||
| 481 | 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 | public function setDescription($desc) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get blame who owned this blameable model. |
||
| 500 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 501 | * specify it in `init()` method. |
||
| 502 | * @return BaseUserQuery user. |
||
| 503 | */ |
||
| 504 | 1 | public function getUser() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get updater who updated this blameable model recently. |
||
| 513 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 514 | * specify it in `init()` method. |
||
| 515 | * @return BaseUserQuery user. |
||
| 516 | */ |
||
| 517 | 1 | public function getUpdater() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * This event is triggered before the model update. |
||
| 529 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 530 | * override or modify it directly, unless you know the consequences. |
||
| 531 | * @param ModelEvent $event |
||
| 532 | */ |
||
| 533 | 15 | public function onContentChanged($event) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 541 | * yet, or return the owner's GUID if current model has been specified. |
||
| 542 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 543 | * override or modify it directly, unless you know the consequences. |
||
| 544 | * @param ModelEvent $event |
||
| 545 | * @return string the GUID of current user or the owner. |
||
| 546 | */ |
||
| 547 | 45 | public function onGetCurrentUserGuid($event) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Initialize type of content. the first of element[index is 0] of |
||
| 562 | * $contentTypes will be used. |
||
| 563 | * @param ModelEvent $event |
||
| 564 | */ |
||
| 565 | 45 | public function onInitContentType($event) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Initialize description property with $initDescription. |
||
| 581 | * @param ModelEvent $event |
||
| 582 | */ |
||
| 583 | 46 | public function onInitDescription($event) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Attach events associated with blameable model. |
||
| 597 | */ |
||
| 598 | 46 | public function initBlameableEvents() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * @inheritdoc |
||
| 616 | */ |
||
| 617 | 14 | public function enabledFields() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 646 | * identity will be taken. |
||
| 647 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 648 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 649 | * regarded as sum of models in one page. |
||
| 650 | * @param integer $currentPage The current page number, begun with 0. |
||
| 651 | * @param {$this->userClass} $identity |
||
| 652 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 653 | */ |
||
| 654 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 664 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 665 | * be given. |
||
| 666 | * @param integer $id user id. |
||
| 667 | * @param boolean $throwException |
||
| 668 | * @param {$this->userClass} $identity |
||
| 669 | * @return static |
||
| 670 | * @throws InvalidParamException |
||
| 671 | */ |
||
| 672 | public static function findOneById($id, $throwException = true, $identity = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get total of follows of specified identity. |
||
| 687 | * @param {$this->userClass} $identity |
||
| 688 | * @return integer total. |
||
| 689 | */ |
||
| 690 | public static function countByIdentity($identity = null) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get pagination, used for building contents page by page. |
||
| 697 | * @param integer $limit |
||
| 698 | * @param {$this->userClass} $identity |
||
| 699 | * @return Pagination |
||
| 700 | */ |
||
| 701 | public static function getPagination($limit = 10, $identity = null) |
||
| 710 | } |
||
| 711 |
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: