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 |
||
| 44 | trait BlameableTrait |
||
| 45 | { |
||
| 46 | use ConfirmationTrait, |
||
| 47 | SelfBlameableTrait; |
||
| 48 | |||
| 49 | private $blameableLocalRules = []; |
||
| 50 | private $blameableLocalBehaviors = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
| 54 | * there is only one content attribute, you can assign its name. Or there |
||
| 55 | * is multiple attributes associated with contents, you can assign their |
||
| 56 | * names in array. If you don't want to use this feature, please assign |
||
| 57 | * false. |
||
| 58 | * For example: |
||
| 59 | * ```php |
||
| 60 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
| 61 | * ``` |
||
| 62 | * or |
||
| 63 | * ```php |
||
| 64 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
| 65 | * ``` |
||
| 66 | * or |
||
| 67 | * ```php |
||
| 68 | * public $contentAttribute = false; // no need of this feature. |
||
| 69 | * ``` |
||
| 70 | * If you don't need this feature, you should add rules corresponding with |
||
| 71 | * `content` in `rules()` method of your user model by yourself. |
||
| 72 | */ |
||
| 73 | public $contentAttribute = 'content'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array built-in validator name or validatation method name and |
||
| 77 | * additional parameters. |
||
| 78 | */ |
||
| 79 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var boolean|string Specify the field which stores the type of content. |
||
| 83 | */ |
||
| 84 | public $contentTypeAttribute = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var boolean|array Specify the logic type of content, not data type. If |
||
| 88 | * your content doesn't need this feature. please specify false. If the |
||
| 89 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
| 90 | * ```php |
||
| 91 | * public $contentTypes = [ |
||
| 92 | * 'public', |
||
| 93 | * 'private', |
||
| 94 | * 'friend', |
||
| 95 | * ]; |
||
| 96 | * ``` |
||
| 97 | */ |
||
| 98 | public $contentTypes = false; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean|string This attribute speicfy the name of description |
||
| 102 | * attribute. If this attribute is assigned to false, this feature will be |
||
| 103 | * skipped. |
||
| 104 | */ |
||
| 105 | public $descriptionAttribute = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | public $initDescription = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string the attribute that will receive current user ID value. This |
||
| 114 | * attribute must be assigned. |
||
| 115 | */ |
||
| 116 | public $createdByAttribute = "user_guid"; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var string the attribute that will receive current user ID value. |
||
| 120 | * Set this property to false if you do not want to record the updater ID. |
||
| 121 | */ |
||
| 122 | public $updatedByAttribute = "user_guid"; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var boolean Add combinated unique rule if assigned to true. |
||
| 126 | */ |
||
| 127 | public $idCreatorCombinatedUnique = true; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var boolean|string The name of user class which own the current entity. |
||
| 131 | * If this attribute is assigned to false, this feature will be skipped, and |
||
| 132 | * when you use create() method of UserTrait, it will be assigned with |
||
| 133 | * current user class. |
||
| 134 | */ |
||
| 135 | public $userClass; |
||
| 136 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
| 137 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
| 138 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
| 139 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 37 | public function rules() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 37 | public function behaviors() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Get total of contents which owned by their owner. |
||
| 159 | * @return integer |
||
| 160 | */ |
||
| 161 | 2 | public function countOfOwner() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get content. |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function getContent() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set content. |
||
| 189 | * @param mixed $content |
||
| 190 | */ |
||
| 191 | public function setContent($content) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Determines whether content could be edited. Your should implement this |
||
| 208 | * method by yourself. |
||
| 209 | * @return boolean |
||
| 210 | * @throws \yii\base\NotSupportedException |
||
| 211 | */ |
||
| 212 | public function getContentCanBeEdited() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Check it has been ever edited. |
||
| 222 | * @return boolean Whether this content has ever been edited. |
||
| 223 | */ |
||
| 224 | public function hasEverEdited() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get blameable rules cache key. |
||
| 236 | * @return string cache key. |
||
| 237 | */ |
||
| 238 | 37 | public function getBlameableRulesCacheKey() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Get blameable rules cache tag. |
||
| 245 | * @return string cache tag |
||
| 246 | */ |
||
| 247 | 13 | public function getBlameableRulesCacheTag() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get the rules associated with content to be blamed. |
||
| 254 | * @return array rules. |
||
| 255 | */ |
||
| 256 | 37 | public function getBlameableRules() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
| 280 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
| 281 | * @return array rules. |
||
| 282 | */ |
||
| 283 | 13 | public function getBlameableAttributeRules() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get the rules associated with `description` attribute. |
||
| 316 | * @return array rules. |
||
| 317 | */ |
||
| 318 | 13 | public function getDescriptionRules() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 337 | * @return array rules. |
||
| 338 | */ |
||
| 339 | 13 | public function getContentRules() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Set blameable rules. |
||
| 375 | * @param array $rules |
||
| 376 | */ |
||
| 377 | 13 | protected function setBlameableRules($rules = []) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get blameable behaviors cache key. |
||
| 389 | * @return string cache key. |
||
| 390 | */ |
||
| 391 | 37 | public function getBlameableBehaviorsCacheKey() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get blameable behaviors cache tag. |
||
| 398 | * @return string cache tag. |
||
| 399 | */ |
||
| 400 | 13 | public function getBlameableBehaviorsCacheTag() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
| 407 | * array will be given. |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | 37 | public function getBlameableBehaviors() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Set blameable behaviors. |
||
| 435 | * @param array $behaviors |
||
| 436 | */ |
||
| 437 | 13 | protected function setBlameableBehaviors($behaviors = []) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Set description. |
||
| 450 | * @return string description. |
||
| 451 | */ |
||
| 452 | public function getDescription() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Get description. |
||
| 460 | * @param string $desc description. |
||
| 461 | * @return string|null description if enabled, or null if disabled. |
||
| 462 | */ |
||
| 463 | public function setDescription($desc) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get blame who owned this blameable model. |
||
| 471 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 472 | * specify it in `init()` method. |
||
| 473 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
| 474 | */ |
||
| 475 | 1 | public function getUser() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get updater who updated this blameable model recently. |
||
| 484 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
| 485 | * specify it in `init()` method. |
||
| 486 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
| 487 | */ |
||
| 488 | 1 | public function getUpdater() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * This event is triggered before the model update. |
||
| 500 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 501 | * override or modify it directly, unless you know the consequences. |
||
| 502 | * @param \yii\base\Event $event |
||
| 503 | */ |
||
| 504 | 11 | public function onContentChanged($event) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 512 | * yet, or return the owner's GUID if current model has been specified. |
||
| 513 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 514 | * override or modify it directly, unless you know the consequences. |
||
| 515 | * @param \yii\base\Event $event |
||
| 516 | * @return string the GUID of current user or the owner. |
||
| 517 | */ |
||
| 518 | 37 | public function onGetCurrentUserGuid($event) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Initialize type of content. the first of element[index is 0] of |
||
| 533 | * $contentTypes will be used. |
||
| 534 | * @param \yii\base\Event $event |
||
| 535 | */ |
||
| 536 | 36 | public function onInitContentType($event) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Initialize description property with $initDescription. |
||
| 552 | * @param \yii\base\Event $event |
||
| 553 | */ |
||
| 554 | 37 | public function onInitDescription($event) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Attach events associated with blameable model. |
||
| 568 | */ |
||
| 569 | 37 | public function initBlameableEvents() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * @inheritdoc |
||
| 587 | */ |
||
| 588 | 11 | public function enabledFields() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
| 617 | * identity will be taken. |
||
| 618 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
| 619 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
| 620 | * regarded as sum of models in one page. |
||
| 621 | * @param integer $currentPage The current page number, begun with 0. |
||
| 622 | * @param $userClass $identity |
||
| 623 | * @return static[] If no follows, null will be given, or return follow array. |
||
| 624 | */ |
||
| 625 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
| 635 | * identity will be taken. If $identity doesn't has the follower, null will |
||
| 636 | * be given. |
||
| 637 | * @param integer $id user id. |
||
| 638 | * @param boolean $throwException |
||
| 639 | * @param $userClass $identity |
||
| 640 | * @return static |
||
| 641 | * @throws NotFoundHttpException |
||
| 642 | */ |
||
| 643 | public static function findOneById($id, $throwException = true, $identity = null) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get total of follows of specified identity. |
||
| 658 | * @param $userClass $identity |
||
| 659 | * @return integer total. |
||
| 660 | */ |
||
| 661 | public static function countByIdentity($identity = null) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get pagination, used for building contents page by page. |
||
| 668 | * @param integer $limit |
||
| 669 | * @param $userClass $identity |
||
| 670 | * @return Pagination |
||
| 671 | */ |
||
| 672 | public static function getPagination($limit = 10, $identity = null) |
||
| 681 | } |
||
| 682 |
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: