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 | 37 | public function rules() |
|
143 | { |
||
144 | 37 | return $this->getBlameableRules(); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | 37 | public function behaviors() |
|
151 | { |
||
152 | 37 | return $this->getBlameableBehaviors(); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Get total of contents which owned by their owner. |
||
157 | * @return integer |
||
158 | */ |
||
159 | 2 | public function countOfOwner() |
|
160 | { |
||
161 | 2 | $createdByAttribute = $this->createdByAttribute; |
|
162 | 2 | return static::find()->where([$createdByAttribute => $this->$createdByAttribute])->count(); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get content. |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function getContent() |
||
170 | { |
||
171 | $contentAttribute = $this->contentAttribute; |
||
172 | if ($contentAttribute === false) { |
||
173 | return null; |
||
174 | } |
||
175 | if (is_array($contentAttribute)) { |
||
176 | $content = []; |
||
177 | foreach ($contentAttribute as $key => $value) { |
||
178 | $content[$key] = $this->$value; |
||
179 | } |
||
180 | return $content; |
||
181 | } |
||
182 | return $this->$contentAttribute; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Set content. |
||
187 | * @param mixed $content |
||
188 | */ |
||
189 | public function setContent($content) |
||
190 | { |
||
191 | $contentAttribute = $this->contentAttribute; |
||
192 | if ($contentAttribute === false) { |
||
193 | return; |
||
194 | } |
||
195 | if (is_array($contentAttribute)) { |
||
196 | foreach ($contentAttribute as $key => $value) { |
||
197 | $this->$value = $content[$key]; |
||
198 | } |
||
199 | return; |
||
200 | } |
||
201 | $this->$contentAttribute = $content; |
||
202 | } |
||
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() |
||
211 | { |
||
212 | if ($this->contentAttribute === false) { |
||
213 | return false; |
||
214 | } |
||
215 | throw new \yii\base\NotSupportedException("This method is not implemented."); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Check it has been ever edited. |
||
220 | * @return boolean Whether this content has ever been edited. |
||
221 | */ |
||
222 | public function hasEverEdited() |
||
223 | { |
||
224 | $createdAtAttribute = $this->createdByAttribute; |
||
225 | $updatedAtAttribute = $this->updatedByAttribute; |
||
226 | if (!$createdAtAttribute || !$updatedAtAttribute) { |
||
227 | return false; |
||
228 | } |
||
229 | return $this->$createdAtAttribute === $this->$updatedAtAttribute; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get blameable rules cache key. |
||
234 | * @return string cache key. |
||
235 | */ |
||
236 | 37 | public function getBlameableRulesCacheKey() |
|
240 | |||
241 | /** |
||
242 | * Get blameable rules cache tag. |
||
243 | * @return string cache tag |
||
244 | */ |
||
245 | 13 | public function getBlameableRulesCacheTag() |
|
249 | |||
250 | /** |
||
251 | * Get the rules associated with content to be blamed. |
||
252 | * @return array rules. |
||
253 | */ |
||
254 | 37 | public function getBlameableRules() |
|
255 | { |
||
256 | 37 | $cache = $this->getCache(); |
|
257 | 37 | if ($cache) { |
|
258 | 37 | $this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey()); |
|
259 | 37 | } |
|
260 | // 若当前规则不为空,且是数组,则认为是规则数组,直接返回。 |
||
261 | 37 | if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) { |
|
262 | 30 | return $this->blameableLocalRules; |
|
263 | } |
||
264 | |||
265 | // 父类规则与确认规则合并。 |
||
266 | 13 | if ($cache) { |
|
267 | 13 | TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]); |
|
268 | 13 | } |
|
269 | 13 | $rules = array_merge( |
|
270 | 13 | parent::rules(), |
|
271 | 13 | $this->getConfirmationRules(), |
|
272 | 13 | $this->getBlameableAttributeRules(), |
|
273 | 13 | $this->getDescriptionRules(), |
|
274 | 13 | $this->getContentRules(), |
|
275 | 13 | $this->getSelfBlameableRules() |
|
276 | 13 | ); |
|
277 | 13 | $this->setBlameableRules($rules); |
|
278 | 13 | return $this->blameableLocalRules; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
283 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
284 | * @return array rules. |
||
285 | */ |
||
286 | 13 | public function getBlameableAttributeRules() |
|
287 | { |
||
288 | 13 | $rules = []; |
|
289 | // 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。 |
||
290 | 13 | if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) { |
|
291 | throw new \yii\base\NotSupportedException('You must assign the creator.'); |
||
292 | } |
||
293 | 13 | $rules[] = [ |
|
294 | 13 | [$this->createdByAttribute], |
|
295 | 13 | 'safe', |
|
296 | ]; |
||
297 | |||
298 | 13 | if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) { |
|
299 | 3 | $rules[] = [ |
|
300 | 3 | [$this->updatedByAttribute], |
|
301 | 3 | 'safe', |
|
302 | ]; |
||
303 | 3 | } |
|
304 | |||
305 | 13 | if ($this->idCreatorCombinatedUnique && is_string($this->idAttribute)) { |
|
306 | 12 | $rules [] = [ |
|
307 | 12 | [$this->idAttribute, |
|
308 | 12 | $this->createdByAttribute], |
|
309 | 12 | 'unique', |
|
310 | 12 | 'targetAttribute' => [$this->idAttribute, |
|
311 | 12 | $this->createdByAttribute], |
|
312 | ]; |
||
313 | 12 | } |
|
314 | 13 | return $rules; |
|
315 | } |
||
316 | |||
317 | /** |
||
318 | * Get the rules associated with `description` attribute. |
||
319 | * @return array rules. |
||
320 | */ |
||
321 | 13 | public function getDescriptionRules() |
|
322 | { |
||
323 | 13 | $rules = []; |
|
324 | 13 | if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) { |
|
325 | 1 | $rules[] = [ |
|
326 | 1 | [$this->descriptionAttribute], |
|
327 | 'string' |
||
328 | 1 | ]; |
|
329 | 1 | $rules[] = [ |
|
330 | 1 | [$this->descriptionAttribute], |
|
331 | 1 | 'default', |
|
332 | 1 | 'value' => $this->initDescription, |
|
333 | ]; |
||
334 | 1 | } |
|
335 | 13 | return $rules; |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * Get the rules associated with `content` and `contentType` attributes. |
||
340 | * @return array rules. |
||
341 | */ |
||
342 | 13 | public function getContentRules() |
|
343 | { |
||
344 | 13 | if (!$this->contentAttribute) { |
|
345 | 2 | return []; |
|
346 | } |
||
347 | 11 | $rules = []; |
|
348 | 11 | $rules[] = [[ |
|
349 | 11 | $this->contentAttribute], |
|
350 | 11 | 'required']; |
|
351 | 11 | if ($this->contentAttributeRule) { |
|
352 | 11 | if (is_string($this->contentAttributeRule)) { |
|
353 | $this->contentAttributeRule = [$this->contentAttributeRule]; |
||
354 | } |
||
355 | 11 | if (is_array($this->contentAttributeRule)) { |
|
356 | 11 | $rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule); |
|
357 | 11 | } |
|
358 | 11 | } |
|
359 | |||
360 | 11 | if (!$this->contentTypeAttribute) { |
|
361 | 9 | return $rules; |
|
362 | } |
||
363 | |||
364 | 2 | if (is_array($this->contentTypes) && !empty($this->contentTypes)) { |
|
365 | 2 | $rules[] = [[ |
|
366 | 2 | $this->contentTypeAttribute], |
|
367 | 2 | 'required']; |
|
368 | 2 | $rules[] = [[ |
|
369 | 2 | $this->contentTypeAttribute], |
|
370 | 2 | 'in', |
|
371 | 2 | 'range' => array_keys($this->contentTypes)]; |
|
372 | 2 | } |
|
373 | 2 | return $rules; |
|
374 | } |
||
375 | |||
376 | /** |
||
377 | * Set blameable rules. |
||
378 | * @param array $rules |
||
379 | */ |
||
380 | 13 | protected function setBlameableRules($rules = []) |
|
381 | { |
||
382 | 13 | $this->blameableLocalRules = $rules; |
|
383 | 13 | $cache = $this->getCache(); |
|
384 | 13 | if ($cache) { |
|
385 | 13 | $tagDependency = new \yii\caching\TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]); |
|
386 | 13 | $cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency); |
|
387 | 13 | } |
|
388 | 13 | } |
|
389 | |||
390 | /** |
||
391 | * Get blameable behaviors cache key. |
||
392 | * @return string cache key. |
||
393 | */ |
||
394 | 37 | public function getBlameableBehaviorsCacheKey() |
|
398 | |||
399 | /** |
||
400 | * Get blameable behaviors cache tag. |
||
401 | * @return string cache tag. |
||
402 | */ |
||
403 | 13 | public function getBlameableBehaviorsCacheTag() |
|
407 | |||
408 | /** |
||
409 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
410 | * array will be given. |
||
411 | * @return array |
||
412 | */ |
||
413 | 37 | public function getBlameableBehaviors() |
|
414 | { |
||
415 | 37 | $cache = $this->getCache(); |
|
416 | 37 | if ($cache) { |
|
417 | 37 | $this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey()); |
|
418 | 37 | } |
|
419 | 37 | if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) { |
|
420 | 13 | if ($cache) { |
|
421 | 13 | TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]); |
|
422 | 13 | } |
|
423 | 13 | $behaviors = parent::behaviors(); |
|
424 | 13 | $behaviors['blameable'] = [ |
|
425 | 13 | 'class' => BlameableBehavior::className(), |
|
426 | 13 | 'createdByAttribute' => $this->createdByAttribute, |
|
427 | 13 | 'updatedByAttribute' => $this->updatedByAttribute, |
|
428 | 13 | 'value' => [$this, |
|
429 | 13 | 'onGetCurrentUserGuid'], |
|
430 | ]; |
||
431 | 13 | $this->setBlameableBehaviors($behaviors); |
|
432 | 13 | } |
|
433 | 37 | return $this->blameableLocalBehaviors; |
|
434 | } |
||
435 | |||
436 | /** |
||
437 | * Set blameable behaviors. |
||
438 | * @param array $behaviors |
||
439 | */ |
||
440 | 13 | protected function setBlameableBehaviors($behaviors = []) |
|
441 | { |
||
442 | 13 | $this->blameableLocalBehaviors = $behaviors; |
|
443 | 13 | $cache = $this->getCache(); |
|
444 | 13 | if ($cache) { |
|
445 | 13 | $tagDependencyConfig = ['tags' => [$this->getBlameableBehaviorsCacheTag()]]; |
|
446 | 13 | $tagDependency = new \yii\caching\TagDependency($tagDependencyConfig); |
|
447 | 13 | $cache->set($this->getBlameableBehaviorsCacheKey(), $behaviors, 0, $tagDependency); |
|
448 | 13 | } |
|
449 | 13 | } |
|
450 | |||
451 | /** |
||
452 | * Set description. |
||
453 | * @return string description. |
||
454 | */ |
||
455 | public function getDescription() |
||
460 | |||
461 | /** |
||
462 | * Get description. |
||
463 | * @param string $desc description. |
||
464 | * @return string|null description if enabled, or null if disabled. |
||
465 | */ |
||
466 | public function setDescription($desc) |
||
471 | |||
472 | /** |
||
473 | * Get blame who owned this blameable model. |
||
474 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
475 | * specify it in `init()` method. |
||
476 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
477 | */ |
||
478 | 1 | public function getUser() |
|
484 | |||
485 | /** |
||
486 | * Get updater who updated this blameable model recently. |
||
487 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
488 | * specify it in `init()` method. |
||
489 | * @return \vistart\Models\queries\BaseUserQuery user. |
||
490 | */ |
||
491 | 1 | public function getUpdater() |
|
492 | { |
||
493 | 1 | if (!is_string($this->updatedByAttribute)) { |
|
494 | 1 | return null; |
|
495 | } |
||
496 | $userClass = $this->userClass; |
||
497 | $model = $userClass::buildNoInitModel(); |
||
498 | return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->updatedByAttribute]); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * This event is triggered before the model update. |
||
503 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
504 | * override or modify it directly, unless you know the consequences. |
||
505 | * @param \yii\base\Event $event |
||
506 | */ |
||
507 | 11 | public function onContentChanged($event) |
|
508 | { |
||
509 | 11 | $sender = $event->sender; |
|
510 | 11 | $sender->resetConfirmation(); |
|
511 | 11 | } |
|
512 | |||
513 | /** |
||
514 | * Return the current user's GUID if current model doesn't specify the owner |
||
515 | * yet, or return the owner's GUID if current model has been specified. |
||
516 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
517 | * override or modify it directly, unless you know the consequences. |
||
518 | * @param \yii\base\Event $event |
||
519 | * @return string the GUID of current user or the owner. |
||
520 | */ |
||
521 | 37 | public function onGetCurrentUserGuid($event) |
|
522 | { |
||
523 | 37 | $sender = $event->sender; |
|
524 | 37 | if (isset($sender->attributes[$sender->createdByAttribute])) { |
|
525 | 37 | return $sender->attributes[$sender->createdByAttribute]; |
|
526 | } |
||
527 | $identity = \Yii::$app->user->identity; |
||
528 | if ($identity) { |
||
529 | $igAttribute = $identity->guidAttribute; |
||
530 | return $identity->$igAttribute; |
||
531 | } |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Initialize type of content. the first of element[index is 0] of |
||
536 | * $contentTypes will be used. |
||
537 | * @param \yii\base\Event $event |
||
538 | */ |
||
539 | 36 | public function onInitContentType($event) |
|
552 | |||
553 | /** |
||
554 | * Initialize description property with $initDescription. |
||
555 | * @param \yii\base\Event $event |
||
556 | */ |
||
557 | 37 | public function onInitDescription($event) |
|
568 | |||
569 | /** |
||
570 | * Attach events associated with blameable model. |
||
571 | */ |
||
572 | 37 | public function initBlameableEvents() |
|
587 | |||
588 | /** |
||
589 | * @inheritdoc |
||
590 | */ |
||
591 | 11 | public function enabledFields() |
|
617 | } |
||
618 |
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: