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 |
||
| 42 | trait BlameableTrait |
||
| 43 | { |
||
| 44 | use ConfirmationTrait, |
||
| 45 | SelfBlameableTrait; |
||
| 46 | |||
| 47 | private $blameableLocalRules = []; |
||
| 48 | private $blameableLocalBehaviors = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
| 52 | * there is only one content attribute, you can assign its name. Or there |
||
| 53 | * is multiple attributes associated with contents, you can assign their |
||
| 54 | * names in array. If you don't want to use this feature, please assign |
||
| 55 | * false. |
||
| 56 | * For example: |
||
| 57 | * ```php |
||
| 58 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
| 59 | * ``` |
||
| 60 | * or |
||
| 61 | * ```php |
||
| 62 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
| 63 | * ``` |
||
| 64 | * or |
||
| 65 | * ```php |
||
| 66 | * public $contentAttribute = false; // no need of this feature. |
||
| 67 | * ``` |
||
| 68 | * If you don't need this feature, you should add rules corresponding with |
||
| 69 | * `content` in `rules()` method of your user model by yourself. |
||
| 70 | */ |
||
| 71 | public $contentAttribute = 'content'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array built-in validator name or validatation method name and |
||
| 75 | * additional parameters. |
||
| 76 | */ |
||
| 77 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var boolean|string Specify the field which stores the type of content. |
||
| 81 | */ |
||
| 82 | public $contentTypeAttribute = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean|array Specify the logic type of content, not data type. If |
||
| 86 | * your content doesn't need this feature. please specify false. If the |
||
| 87 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
| 88 | * ```php |
||
| 89 | * public $contentTypes = [ |
||
| 90 | * 'public', |
||
| 91 | * 'private', |
||
| 92 | * 'friend', |
||
| 93 | * ]; |
||
| 94 | * ``` |
||
| 95 | */ |
||
| 96 | public $contentTypes = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean|string This attribute speicfy the name of description |
||
| 100 | * attribute. If this attribute is assigned to false, this feature will be |
||
| 101 | * skipped. |
||
| 102 | */ |
||
| 103 | public $descriptionAttribute = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | public $initDescription = ''; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var string the attribute that will receive current user ID value. This |
||
| 112 | * attribute must be assigned. |
||
| 113 | */ |
||
| 114 | public $createdByAttribute = "user_guid"; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string the attribute that will receive current user ID value. |
||
| 118 | * Set this property to false if you do not want to record the updater ID. |
||
| 119 | */ |
||
| 120 | public $updatedByAttribute = "user_guid"; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var boolean Add combinated unique rule if assigned to true. |
||
| 124 | */ |
||
| 125 | public $idCreatorCombinatedUnique = true; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var boolean|string The name of user class which own the current entity. |
||
| 129 | * If this attribute is assigned to false, this feature will be skipped, and |
||
| 130 | * when you use create() method of UserTrait, it will be assigned with |
||
| 131 | * current user class. |
||
| 132 | */ |
||
| 133 | public $userClass; |
||
| 134 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
| 135 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
| 136 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
| 137 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
| 142 | 24 | public function rules() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | 24 | public function behaviors() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get total of contents which owned by their owner. |
||
| 157 | * @return integer |
||
| 158 | */ |
||
| 159 | 2 | public function count() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get content. |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function getContent() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Set content. |
||
| 187 | * @param mixed $content |
||
| 188 | */ |
||
| 189 | public function setContent($content) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Determines whether content could be edited. Your should implement this |
||
| 206 | * method by yourself. |
||
| 207 | * @return boolean |
||
| 208 | * @throws \yii\base\NotSupportedException |
||
| 209 | */ |
||
| 210 | public function getContentCanBeEdited() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Check it has been ever edited. |
||
| 220 | * @return boolean Whether this content has ever been edited. |
||
| 221 | */ |
||
| 222 | public function hasEverEdited() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get blameable rules cache key. |
||
| 234 | * @return string cache key. |
||
| 235 | */ |
||
| 236 | 24 | public function getBlameableRulesCacheKey() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get blameable rules cache tag. |
||
| 243 | * @return string cache tag |
||
| 244 | */ |
||
| 245 | 6 | public function getBlameableRulesCacheTag() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get the rules associated with content to be blamed. |
||
| 252 | * @return array rules. |
||
| 253 | */ |
||
| 254 | 24 | public function getBlameableRules() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
| 278 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
| 279 | * @return array rules. |
||
| 280 | */ |
||
| 281 | 6 | public function getBlameableAttributeRules() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get the rules associated with `description` attribute. |
||
| 314 | * @return array rules. |
||
| 315 | */ |
||
| 316 | 6 | public function getDescriptionRules() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get the rules associated with `content` and `contentType` attributes. |
||
| 335 | * @return array rules. |
||
| 336 | */ |
||
| 337 | 6 | public function getContentRules() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Set blameable rules. |
||
| 373 | * @param array $rules |
||
| 374 | */ |
||
| 375 | 6 | protected function setBlameableRules($rules = []) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get blameable behaviors cache key. |
||
| 387 | * @return string cache key. |
||
| 388 | */ |
||
| 389 | 24 | public function getBlameableBehaviorsCacheKey() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Get blameable behaviors cache tag. |
||
| 396 | * @return string cache tag. |
||
| 397 | */ |
||
| 398 | 6 | public function getBlameableBehaviorsCacheTag() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
| 405 | * array will be given. |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | 24 | public function getBlameableBehaviors() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Set blameable behaviors. |
||
| 433 | * @param array $behaviors |
||
| 434 | */ |
||
| 435 | 6 | protected function setBlameableBehaviors($behaviors = []) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Set description. |
||
| 448 | * @return string description. |
||
| 449 | */ |
||
| 450 | public function getDescription() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get description. |
||
| 458 | * @param string $desc description. |
||
| 459 | * @return string|null description if enabled, or null if disabled. |
||
| 460 | */ |
||
| 461 | public function setDescription($desc) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get blame. |
||
| 469 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
| 470 | */ |
||
| 471 | 1 | public function getUser() |
|
| 472 | { |
||
| 473 | 1 | $userClass = $this->userClass; |
|
| 474 | 1 | $model = $userClass::buildNoInitModel(); |
|
| 475 | 1 | return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->createdByAttribute]); |
|
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * This event is triggered before the model update. |
||
| 480 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 481 | * override or modify it directly, unless you know the consequences. |
||
| 482 | * @param \yii\base\Event $event |
||
| 483 | */ |
||
| 484 | 9 | public function onContentChanged($event) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Return the current user's GUID if current model doesn't specify the owner |
||
| 492 | * yet, or return the owner's GUID if current model has been specified. |
||
| 493 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 494 | * override or modify it directly, unless you know the consequences. |
||
| 495 | * @param \yii\base\Event $event |
||
| 496 | * @return string the GUID of current user or the owner. |
||
| 497 | */ |
||
| 498 | 24 | public function onGetCurrentUserGuid($event) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Initialize type of content. the first of element[index is 0] of |
||
| 513 | * $contentTypes will be used. |
||
| 514 | * @param \yii\base\Event $event |
||
| 515 | */ |
||
| 516 | 24 | public function onInitContentType($event) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Initialize description property with $initDescription. |
||
| 532 | * @param \yii\base\Event $event |
||
| 533 | */ |
||
| 534 | 24 | public function onInitDescription($event) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Attach events associated with blameable model. |
||
| 548 | */ |
||
| 549 | 24 | public function initBlameableEvents() |
|
| 564 | } |
||
| 565 |
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: