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() |
|
282 | |||
283 | /** |
||
284 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
285 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
286 | * @return array rules. |
||
287 | */ |
||
288 | 13 | public function getBlameableAttributeRules() |
|
318 | |||
319 | /** |
||
320 | * Get the rules associated with `description` attribute. |
||
321 | * @return array rules. |
||
322 | */ |
||
323 | 13 | public function getDescriptionRules() |
|
339 | |||
340 | /** |
||
341 | * Get the rules associated with `content` and `contentType` attributes. |
||
342 | * @return array rules. |
||
343 | */ |
||
344 | 13 | public function getContentRules() |
|
377 | |||
378 | /** |
||
379 | * Set blameable rules. |
||
380 | * @param array $rules |
||
381 | */ |
||
382 | 13 | protected function setBlameableRules($rules = []) |
|
391 | |||
392 | /** |
||
393 | * Get blameable behaviors cache key. |
||
394 | * @return string cache key. |
||
395 | */ |
||
396 | 37 | public function getBlameableBehaviorsCacheKey() |
|
400 | |||
401 | /** |
||
402 | * Get blameable behaviors cache tag. |
||
403 | * @return string cache tag. |
||
404 | */ |
||
405 | 13 | public function getBlameableBehaviorsCacheTag() |
|
409 | |||
410 | /** |
||
411 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
412 | * array will be given. |
||
413 | * @return array |
||
414 | */ |
||
415 | 37 | public function getBlameableBehaviors() |
|
437 | |||
438 | /** |
||
439 | * Set blameable behaviors. |
||
440 | * @param array $behaviors |
||
441 | */ |
||
442 | 13 | protected function setBlameableBehaviors($behaviors = []) |
|
452 | |||
453 | /** |
||
454 | * Set description. |
||
455 | * @return string description. |
||
456 | */ |
||
457 | public function getDescription() |
||
462 | |||
463 | /** |
||
464 | * Get description. |
||
465 | * @param string $desc description. |
||
466 | * @return string|null description if enabled, or null if disabled. |
||
467 | */ |
||
468 | public function setDescription($desc) |
||
473 | |||
474 | /** |
||
475 | * Get blame who owned this blameable model. |
||
476 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
477 | * specify it in `init()` method. |
||
478 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
479 | */ |
||
480 | 1 | public function getUser() |
|
486 | |||
487 | /** |
||
488 | * Get updater who updated this blameable model recently. |
||
489 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
490 | * specify it in `init()` method. |
||
491 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
492 | */ |
||
493 | 1 | public function getUpdater() |
|
502 | |||
503 | /** |
||
504 | * This event is triggered before the model update. |
||
505 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
506 | * override or modify it directly, unless you know the consequences. |
||
507 | * @param \yii\base\Event $event |
||
508 | */ |
||
509 | 11 | public function onContentChanged($event) |
|
514 | |||
515 | /** |
||
516 | * Return the current user's GUID if current model doesn't specify the owner |
||
517 | * yet, or return the owner's GUID if current model has been specified. |
||
518 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
519 | * override or modify it directly, unless you know the consequences. |
||
520 | * @param \yii\base\Event $event |
||
521 | * @return string the GUID of current user or the owner. |
||
522 | */ |
||
523 | 37 | public function onGetCurrentUserGuid($event) |
|
535 | |||
536 | /** |
||
537 | * Initialize type of content. the first of element[index is 0] of |
||
538 | * $contentTypes will be used. |
||
539 | * @param \yii\base\Event $event |
||
540 | */ |
||
541 | 36 | public function onInitContentType($event) |
|
554 | |||
555 | /** |
||
556 | * Initialize description property with $initDescription. |
||
557 | * @param \yii\base\Event $event |
||
558 | */ |
||
559 | 37 | public function onInitDescription($event) |
|
570 | |||
571 | /** |
||
572 | * Attach events associated with blameable model. |
||
573 | */ |
||
574 | 37 | public function initBlameableEvents() |
|
589 | |||
590 | /** |
||
591 | * @inheritdoc |
||
592 | */ |
||
593 | 11 | public function enabledFields() |
|
619 | |||
620 | /** |
||
621 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
622 | * identity will be taken. |
||
623 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
624 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
625 | * regarded as sum of models in one page. |
||
626 | * @param integer $currentPage The current page number, begun with 0. |
||
627 | * @param $userClass $identity |
||
628 | * @return static[] If no follows, null will be given, or return follow array. |
||
629 | */ |
||
630 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
||
637 | |||
638 | /** |
||
639 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
640 | * identity will be taken. If $identity doesn't has the follower, null will |
||
641 | * be given. |
||
642 | * @param integer $id user id. |
||
643 | * @param boolean $throwException |
||
644 | * @param $userClass $identity |
||
645 | * @return static |
||
646 | * @throws NotFoundHttpException |
||
647 | */ |
||
648 | public static function findOneById($id, $throwException = true, $identity = null) |
||
660 | |||
661 | /** |
||
662 | * Get total of follows of specified identity. |
||
663 | * @param $userClass $identity |
||
664 | * @return integer total. |
||
665 | */ |
||
666 | public static function countByIdentity($identity = null) |
||
670 | |||
671 | /** |
||
672 | * Get pagination, used for building contents page by page. |
||
673 | * @param integer $limit |
||
674 | * @param $userClass $identity |
||
675 | * @return Pagination |
||
676 | */ |
||
677 | public static function getPagination($limit = 10, $identity = null) |
||
686 | } |
||
687 |
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: