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 |
||
47 | trait BlameableTrait |
||
48 | { |
||
49 | use ConfirmationTrait, |
||
50 | SelfBlameableTrait; |
||
51 | |||
52 | private $blameableLocalRules = []; |
||
53 | private $blameableLocalBehaviors = []; |
||
54 | |||
55 | /** |
||
56 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
57 | * there is only one content attribute, you can assign its name. Or there |
||
58 | * is multiple attributes associated with contents, you can assign their |
||
59 | * names in array. If you don't want to use this feature, please assign |
||
60 | * false. |
||
61 | * For example: |
||
62 | * ```php |
||
63 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
64 | * ``` |
||
65 | * or |
||
66 | * ```php |
||
67 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
68 | * ``` |
||
69 | * or |
||
70 | * ```php |
||
71 | * public $contentAttribute = false; // no need of this feature. |
||
72 | * ``` |
||
73 | * If you don't need this feature, you should add rules corresponding with |
||
74 | * `content` in `rules()` method of your user model by yourself. |
||
75 | */ |
||
76 | public $contentAttribute = 'content'; |
||
77 | |||
78 | /** |
||
79 | * @var array built-in validator name or validatation method name and |
||
80 | * additional parameters. |
||
81 | */ |
||
82 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
83 | |||
84 | /** |
||
85 | * @var boolean|string Specify the field which stores the type of content. |
||
86 | */ |
||
87 | public $contentTypeAttribute = false; |
||
88 | |||
89 | /** |
||
90 | * @var boolean|array Specify the logic type of content, not data type. If |
||
91 | * your content doesn't need this feature. please specify false. If the |
||
92 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
93 | * ```php |
||
94 | * public $contentTypes = [ |
||
95 | * 'public', |
||
96 | * 'private', |
||
97 | * 'friend', |
||
98 | * ]; |
||
99 | * ``` |
||
100 | */ |
||
101 | public $contentTypes = false; |
||
102 | |||
103 | /** |
||
104 | * @var boolean|string This attribute speicfy the name of description |
||
105 | * attribute. If this attribute is assigned to false, this feature will be |
||
106 | * skipped. |
||
107 | */ |
||
108 | public $descriptionAttribute = false; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | public $initDescription = ''; |
||
114 | |||
115 | /** |
||
116 | * @var string the attribute that will receive current user ID value. This |
||
117 | * attribute must be assigned. |
||
118 | */ |
||
119 | public $createdByAttribute = "user_guid"; |
||
120 | |||
121 | /** |
||
122 | * @var string the attribute that will receive current user ID value. |
||
123 | * Set this property to false if you do not want to record the updater ID. |
||
124 | */ |
||
125 | public $updatedByAttribute = "user_guid"; |
||
126 | |||
127 | /** |
||
128 | * @var boolean Add combinated unique rule if assigned to true. |
||
129 | */ |
||
130 | public $idCreatorCombinatedUnique = true; |
||
131 | |||
132 | /** |
||
133 | * @var boolean|string The name of user class which own the current entity. |
||
134 | * If this attribute is assigned to false, this feature will be skipped, and |
||
135 | * when you use create() method of UserTrait, it will be assigned with |
||
136 | * current user class. |
||
137 | */ |
||
138 | public $userClass; |
||
139 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
140 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
141 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
142 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 42 | public function rules() |
|
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 42 | public function behaviors() |
|
159 | |||
160 | /** |
||
161 | * Get total of contents which owned by their owner. |
||
162 | * @return integer |
||
163 | */ |
||
164 | 2 | public function countOfOwner() |
|
169 | |||
170 | /** |
||
171 | * Get content. |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function getContent() |
||
189 | |||
190 | /** |
||
191 | * Set content. |
||
192 | * @param mixed $content |
||
193 | */ |
||
194 | public function setContent($content) |
||
208 | |||
209 | /** |
||
210 | * Determines whether content could be edited. Your should implement this |
||
211 | * method by yourself. |
||
212 | * @return boolean |
||
213 | * @throws NotSupportedException |
||
214 | */ |
||
215 | public function getContentCanBeEdited() |
||
222 | |||
223 | /** |
||
224 | * Check it has been ever edited. |
||
225 | * @return boolean Whether this content has ever been edited. |
||
226 | */ |
||
227 | public function hasEverEdited() |
||
236 | |||
237 | /** |
||
238 | * Get blameable rules cache key. |
||
239 | * @return string cache key. |
||
240 | */ |
||
241 | 42 | public function getBlameableRulesCacheKey() |
|
245 | |||
246 | /** |
||
247 | * Get blameable rules cache tag. |
||
248 | * @return string cache tag |
||
249 | */ |
||
250 | 15 | public function getBlameableRulesCacheTag() |
|
254 | |||
255 | /** |
||
256 | * Get the rules associated with content to be blamed. |
||
257 | * @return array rules. |
||
258 | */ |
||
259 | 42 | public function getBlameableRules() |
|
260 | { |
||
261 | 42 | $cache = $this->getCache(); |
|
262 | 42 | if ($cache) { |
|
263 | 42 | $this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey()); |
|
264 | 42 | } |
|
265 | // 若当前规则不为空,且是数组,则认为是规则数组,直接返回。 |
||
266 | 42 | if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) { |
|
267 | 33 | return $this->blameableLocalRules; |
|
268 | 2 | } |
|
269 | |||
270 | // 父类规则与确认规则合并。 |
||
271 | 15 | if ($cache) { |
|
272 | 15 | TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]); |
|
273 | 15 | } |
|
274 | 15 | $rules = array_merge( |
|
275 | 15 | parent::rules(), |
|
276 | 15 | $this->getConfirmationRules(), |
|
277 | 15 | $this->getBlameableAttributeRules(), |
|
278 | 15 | $this->getDescriptionRules(), |
|
279 | 15 | $this->getContentRules(), |
|
280 | 15 | $this->getSelfBlameableRules() |
|
281 | 15 | ); |
|
282 | 15 | $this->setBlameableRules($rules); |
|
283 | 15 | return $this->blameableLocalRules; |
|
284 | } |
||
285 | |||
286 | /** |
||
287 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
288 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
289 | * @return array rules. |
||
290 | */ |
||
291 | 15 | public function getBlameableAttributeRules() |
|
321 | |||
322 | /** |
||
323 | * Get the rules associated with `description` attribute. |
||
324 | * @return array rules. |
||
325 | */ |
||
326 | 15 | public function getDescriptionRules() |
|
342 | |||
343 | /** |
||
344 | * Get the rules associated with `content` and `contentType` attributes. |
||
345 | * @return array rules. |
||
346 | */ |
||
347 | 15 | public function getContentRules() |
|
378 | |||
379 | /** |
||
380 | * Set blameable rules. |
||
381 | * @param array $rules |
||
382 | */ |
||
383 | 15 | protected function setBlameableRules($rules = []) |
|
392 | |||
393 | /** |
||
394 | * Get blameable behaviors cache key. |
||
395 | * @return string cache key. |
||
396 | */ |
||
397 | 42 | public function getBlameableBehaviorsCacheKey() |
|
401 | |||
402 | /** |
||
403 | * Get blameable behaviors cache tag. |
||
404 | * @return string cache tag. |
||
405 | */ |
||
406 | 15 | public function getBlameableBehaviorsCacheTag() |
|
410 | |||
411 | /** |
||
412 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
413 | * array will be given. |
||
414 | * @return array |
||
415 | */ |
||
416 | 42 | public function getBlameableBehaviors() |
|
417 | { |
||
418 | 42 | $cache = $this->getCache(); |
|
419 | 42 | if ($cache) { |
|
420 | 42 | $this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey()); |
|
421 | 42 | } |
|
422 | 42 | if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) { |
|
423 | 15 | if ($cache) { |
|
424 | 15 | TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]); |
|
425 | 15 | } |
|
426 | 15 | $behaviors = parent::behaviors(); |
|
427 | 15 | $behaviors['blameable'] = [ |
|
428 | 15 | 'class' => BlameableBehavior::className(), |
|
429 | 15 | 'createdByAttribute' => $this->createdByAttribute, |
|
430 | 15 | 'updatedByAttribute' => $this->updatedByAttribute, |
|
431 | 15 | 'value' => [$this, |
|
432 | 15 | 'onGetCurrentUserGuid'], |
|
433 | ]; |
||
434 | 15 | $this->setBlameableBehaviors($behaviors); |
|
435 | 15 | } |
|
436 | 42 | return $this->blameableLocalBehaviors; |
|
437 | } |
||
438 | |||
439 | /** |
||
440 | * Set blameable behaviors. |
||
441 | * @param array $behaviors |
||
442 | */ |
||
443 | 15 | protected function setBlameableBehaviors($behaviors = []) |
|
453 | |||
454 | /** |
||
455 | * Set description. |
||
456 | * @return string description. |
||
457 | */ |
||
458 | public function getDescription() |
||
463 | |||
464 | /** |
||
465 | * Get description. |
||
466 | * @param string $desc description. |
||
467 | * @return string|null description if enabled, or null if disabled. |
||
468 | */ |
||
469 | public function setDescription($desc) |
||
474 | |||
475 | /** |
||
476 | * Get blame who owned this blameable model. |
||
477 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
478 | * specify it in `init()` method. |
||
479 | * @return BaseUserQuery user. |
||
480 | */ |
||
481 | 1 | public function getUser() |
|
487 | |||
488 | /** |
||
489 | * Get updater who updated this blameable model recently. |
||
490 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
491 | * specify it in `init()` method. |
||
492 | * @return BaseUserQuery user. |
||
493 | */ |
||
494 | 1 | public function getUpdater() |
|
503 | |||
504 | /** |
||
505 | * This event is triggered before the model update. |
||
506 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
507 | * override or modify it directly, unless you know the consequences. |
||
508 | * @param ModelEvent $event |
||
509 | */ |
||
510 | 14 | public function onContentChanged($event) |
|
515 | |||
516 | /** |
||
517 | * Return the current user's GUID if current model doesn't specify the owner |
||
518 | * yet, or return the owner's GUID if current model has been specified. |
||
519 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
520 | * override or modify it directly, unless you know the consequences. |
||
521 | * @param ModelEvent $event |
||
522 | * @return string the GUID of current user or the owner. |
||
523 | */ |
||
524 | 42 | public function onGetCurrentUserGuid($event) |
|
536 | |||
537 | /** |
||
538 | * Initialize type of content. the first of element[index is 0] of |
||
539 | * $contentTypes will be used. |
||
540 | * @param ModelEvent $event |
||
541 | */ |
||
542 | 41 | public function onInitContentType($event) |
|
555 | |||
556 | /** |
||
557 | * Initialize description property with $initDescription. |
||
558 | * @param ModelEvent $event |
||
559 | */ |
||
560 | 42 | public function onInitDescription($event) |
|
571 | |||
572 | /** |
||
573 | * Attach events associated with blameable model. |
||
574 | */ |
||
575 | 42 | public function initBlameableEvents() |
|
590 | |||
591 | /** |
||
592 | * @inheritdoc |
||
593 | */ |
||
594 | 14 | public function enabledFields() |
|
620 | |||
621 | /** |
||
622 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
623 | * identity will be taken. |
||
624 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
625 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
626 | * regarded as sum of models in one page. |
||
627 | * @param integer $currentPage The current page number, begun with 0. |
||
628 | * @param {$this->userClass} $identity |
||
629 | * @return static[] If no follows, null will be given, or return follow array. |
||
630 | */ |
||
631 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
||
638 | |||
639 | /** |
||
640 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
641 | * identity will be taken. If $identity doesn't has the follower, null will |
||
642 | * be given. |
||
643 | * @param integer $id user id. |
||
644 | * @param boolean $throwException |
||
645 | * @param {$this->userClass} $identity |
||
646 | * @return static |
||
647 | * @throws InvalidParamException |
||
648 | */ |
||
649 | public static function findOneById($id, $throwException = true, $identity = null) |
||
661 | |||
662 | /** |
||
663 | * Get total of follows of specified identity. |
||
664 | * @param {$this->userClass} $identity |
||
665 | * @return integer total. |
||
666 | */ |
||
667 | public static function countByIdentity($identity = null) |
||
671 | |||
672 | /** |
||
673 | * Get pagination, used for building contents page by page. |
||
674 | * @param integer $limit |
||
675 | * @param {$this->userClass} $identity |
||
676 | * @return Pagination |
||
677 | */ |
||
678 | public static function getPagination($limit = 10, $identity = null) |
||
687 | } |
||
688 |
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: