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 |
||
43 | trait BlameableTrait |
||
44 | { |
||
45 | use ConfirmationTrait, |
||
46 | SelfBlameableTrait; |
||
47 | |||
48 | private $blameableLocalRules = []; |
||
49 | private $blameableLocalBehaviors = []; |
||
50 | |||
51 | /** |
||
52 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
53 | * there is only one content attribute, you can assign its name. Or there |
||
54 | * is multiple attributes associated with contents, you can assign their |
||
55 | * names in array. If you don't want to use this feature, please assign |
||
56 | * false. |
||
57 | * For example: |
||
58 | * ```php |
||
59 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
60 | * ``` |
||
61 | * or |
||
62 | * ```php |
||
63 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
64 | * ``` |
||
65 | * or |
||
66 | * ```php |
||
67 | * public $contentAttribute = false; // no need of this feature. |
||
68 | * ``` |
||
69 | * If you don't need this feature, you should add rules corresponding with |
||
70 | * `content` in `rules()` method of your user model by yourself. |
||
71 | */ |
||
72 | public $contentAttribute = 'content'; |
||
73 | |||
74 | /** |
||
75 | * @var array built-in validator name or validatation method name and |
||
76 | * additional parameters. |
||
77 | */ |
||
78 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
79 | |||
80 | /** |
||
81 | * @var boolean|string Specify the field which stores the type of content. |
||
82 | */ |
||
83 | public $contentTypeAttribute = false; |
||
84 | |||
85 | /** |
||
86 | * @var boolean|array Specify the logic type of content, not data type. If |
||
87 | * your content doesn't need this feature. please specify false. If the |
||
88 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
89 | * ```php |
||
90 | * public $contentTypes = [ |
||
91 | * 'public', |
||
92 | * 'private', |
||
93 | * 'friend', |
||
94 | * ]; |
||
95 | * ``` |
||
96 | */ |
||
97 | public $contentTypes = false; |
||
98 | |||
99 | /** |
||
100 | * @var boolean|string This attribute speicfy the name of description |
||
101 | * attribute. If this attribute is assigned to false, this feature will be |
||
102 | * skipped. |
||
103 | */ |
||
104 | public $descriptionAttribute = false; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | */ |
||
109 | public $initDescription = ''; |
||
110 | |||
111 | /** |
||
112 | * @var string the attribute that will receive current user ID value. This |
||
113 | * attribute must be assigned. |
||
114 | */ |
||
115 | public $createdByAttribute = "user_guid"; |
||
116 | |||
117 | /** |
||
118 | * @var string the attribute that will receive current user ID value. |
||
119 | * Set this property to false if you do not want to record the updater ID. |
||
120 | */ |
||
121 | public $updatedByAttribute = "user_guid"; |
||
122 | |||
123 | /** |
||
124 | * @var boolean Add combinated unique rule if assigned to true. |
||
125 | */ |
||
126 | public $idCreatorCombinatedUnique = true; |
||
127 | |||
128 | /** |
||
129 | * @var boolean|string The name of user class which own the current entity. |
||
130 | * If this attribute is assigned to false, this feature will be skipped, and |
||
131 | * when you use create() method of UserTrait, it will be assigned with |
||
132 | * current user class. |
||
133 | */ |
||
134 | public $userClass; |
||
135 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
136 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
137 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
138 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 25 | public function rules() |
|
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 25 | public function behaviors() |
|
155 | |||
156 | /** |
||
157 | * Get total of contents which owned by their owner. |
||
158 | * @return integer |
||
159 | */ |
||
160 | 2 | public function count() |
|
165 | |||
166 | /** |
||
167 | * Get content. |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function getContent() |
||
185 | |||
186 | /** |
||
187 | * Set content. |
||
188 | * @param mixed $content |
||
189 | */ |
||
190 | public function setContent($content) |
||
204 | |||
205 | /** |
||
206 | * Determines whether content could be edited. Your should implement this |
||
207 | * method by yourself. |
||
208 | * @return boolean |
||
209 | * @throws \yii\base\NotSupportedException |
||
210 | */ |
||
211 | public function getContentCanBeEdited() |
||
218 | |||
219 | /** |
||
220 | * Check it has been ever edited. |
||
221 | * @return boolean Whether this content has ever been edited. |
||
222 | */ |
||
223 | public function hasEverEdited() |
||
232 | |||
233 | /** |
||
234 | * Get blameable rules cache key. |
||
235 | * @return string cache key. |
||
236 | */ |
||
237 | 25 | public function getBlameableRulesCacheKey() |
|
241 | |||
242 | /** |
||
243 | * Get blameable rules cache tag. |
||
244 | * @return string cache tag |
||
245 | */ |
||
246 | 6 | public function getBlameableRulesCacheTag() |
|
250 | |||
251 | /** |
||
252 | * Get the rules associated with content to be blamed. |
||
253 | * @return array rules. |
||
254 | */ |
||
255 | 25 | public function getBlameableRules() |
|
281 | |||
282 | /** |
||
283 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
284 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
285 | * @return array rules. |
||
286 | */ |
||
287 | 6 | public function getBlameableAttributeRules() |
|
317 | |||
318 | /** |
||
319 | * Get the rules associated with `description` attribute. |
||
320 | * @return array rules. |
||
321 | */ |
||
322 | 6 | public function getDescriptionRules() |
|
338 | |||
339 | /** |
||
340 | * Get the rules associated with `content` and `contentType` attributes. |
||
341 | * @return array rules. |
||
342 | */ |
||
343 | 6 | public function getContentRules() |
|
376 | |||
377 | /** |
||
378 | * Set blameable rules. |
||
379 | * @param array $rules |
||
380 | */ |
||
381 | 6 | protected function setBlameableRules($rules = []) |
|
390 | |||
391 | /** |
||
392 | * Get blameable behaviors cache key. |
||
393 | * @return string cache key. |
||
394 | */ |
||
395 | 25 | public function getBlameableBehaviorsCacheKey() |
|
399 | |||
400 | /** |
||
401 | * Get blameable behaviors cache tag. |
||
402 | * @return string cache tag. |
||
403 | */ |
||
404 | 6 | public function getBlameableBehaviorsCacheTag() |
|
408 | |||
409 | /** |
||
410 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
411 | * array will be given. |
||
412 | * @return array |
||
413 | */ |
||
414 | 25 | public function getBlameableBehaviors() |
|
436 | |||
437 | /** |
||
438 | * Set blameable behaviors. |
||
439 | * @param array $behaviors |
||
440 | */ |
||
441 | 6 | protected function setBlameableBehaviors($behaviors = []) |
|
451 | |||
452 | /** |
||
453 | * Set description. |
||
454 | * @return string description. |
||
455 | */ |
||
456 | public function getDescription() |
||
461 | |||
462 | /** |
||
463 | * Get description. |
||
464 | * @param string $desc description. |
||
465 | * @return string|null description if enabled, or null if disabled. |
||
466 | */ |
||
467 | public function setDescription($desc) |
||
472 | |||
473 | /** |
||
474 | * Get blame. |
||
475 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
476 | */ |
||
477 | 1 | public function getUser() |
|
483 | |||
484 | 1 | public function getUpdater() |
|
493 | |||
494 | /** |
||
495 | * This event is triggered before the model update. |
||
496 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
497 | * override or modify it directly, unless you know the consequences. |
||
498 | * @param \yii\base\Event $event |
||
499 | */ |
||
500 | 9 | public function onContentChanged($event) |
|
505 | |||
506 | /** |
||
507 | * Return the current user's GUID if current model doesn't specify the owner |
||
508 | * yet, or return the owner's GUID if current model has been specified. |
||
509 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
510 | * override or modify it directly, unless you know the consequences. |
||
511 | * @param \yii\base\Event $event |
||
512 | * @return string the GUID of current user or the owner. |
||
513 | */ |
||
514 | 25 | public function onGetCurrentUserGuid($event) |
|
526 | |||
527 | /** |
||
528 | * Initialize type of content. the first of element[index is 0] of |
||
529 | * $contentTypes will be used. |
||
530 | * @param \yii\base\Event $event |
||
531 | */ |
||
532 | 24 | public function onInitContentType($event) |
|
545 | |||
546 | /** |
||
547 | * Initialize description property with $initDescription. |
||
548 | * @param \yii\base\Event $event |
||
549 | */ |
||
550 | 25 | public function onInitDescription($event) |
|
561 | |||
562 | /** |
||
563 | * Attach events associated with blameable model. |
||
564 | */ |
||
565 | 25 | public function initBlameableEvents() |
|
580 | } |
||
581 |
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: