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 |
||
48 | trait BlameableTrait |
||
49 | { |
||
50 | use ConfirmationTrait, |
||
51 | SelfBlameableTrait; |
||
52 | |||
53 | private $blameableLocalRules = []; |
||
54 | private $blameableLocalBehaviors = []; |
||
55 | |||
56 | /** |
||
57 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
58 | * there is only one content attribute, you can assign its name. Or there |
||
59 | * is multiple attributes associated with contents, you can assign their |
||
60 | * names in array. If you don't want to use this feature, please assign |
||
61 | * false. |
||
62 | * For example: |
||
63 | * ```php |
||
64 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
65 | * ``` |
||
66 | * or |
||
67 | * ```php |
||
68 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
69 | * ``` |
||
70 | * or |
||
71 | * ```php |
||
72 | * public $contentAttribute = false; // no need of this feature. |
||
73 | * ``` |
||
74 | * If you don't need this feature, you should add rules corresponding with |
||
75 | * `content` in `rules()` method of your user model by yourself. |
||
76 | */ |
||
77 | public $contentAttribute = 'content'; |
||
78 | |||
79 | /** |
||
80 | * @var array built-in validator name or validatation method name and |
||
81 | * additional parameters. |
||
82 | */ |
||
83 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
84 | |||
85 | /** |
||
86 | * @var boolean|string Specify the field which stores the type of content. |
||
87 | */ |
||
88 | public $contentTypeAttribute = false; |
||
89 | |||
90 | /** |
||
91 | * @var boolean|array Specify the logic type of content, not data type. If |
||
92 | * your content doesn't need this feature. please specify false. If the |
||
93 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
94 | * ```php |
||
95 | * public $contentTypes = [ |
||
96 | * 'public', |
||
97 | * 'private', |
||
98 | * 'friend', |
||
99 | * ]; |
||
100 | * ``` |
||
101 | */ |
||
102 | public $contentTypes = false; |
||
103 | |||
104 | /** |
||
105 | * @var boolean|string This attribute speicfy the name of description |
||
106 | * attribute. If this attribute is assigned to false, this feature will be |
||
107 | * skipped. |
||
108 | */ |
||
109 | public $descriptionAttribute = false; |
||
110 | |||
111 | /** |
||
112 | * @var string |
||
113 | */ |
||
114 | public $initDescription = ''; |
||
115 | |||
116 | /** |
||
117 | * @var string the attribute that will receive current user ID value. This |
||
118 | * attribute must be assigned. |
||
119 | */ |
||
120 | public $createdByAttribute = "user_guid"; |
||
121 | |||
122 | /** |
||
123 | * @var string the attribute that will receive current user ID value. |
||
124 | * Set this property to false if you do not want to record the updater ID. |
||
125 | */ |
||
126 | public $updatedByAttribute = "user_guid"; |
||
127 | |||
128 | /** |
||
129 | * @var boolean Add combinated unique rule if assigned to true. |
||
130 | */ |
||
131 | public $idCreatorCombinatedUnique = true; |
||
132 | |||
133 | /** |
||
134 | * @var boolean|string The name of user class which own the current entity. |
||
135 | * If this attribute is assigned to false, this feature will be skipped, and |
||
136 | * when you use create() method of UserTrait, it will be assigned with |
||
137 | * current user class. |
||
138 | */ |
||
139 | //public $userClass; |
||
140 | |||
141 | /** |
||
142 | * @var boolean|string |
||
143 | */ |
||
144 | public $hostClass; |
||
145 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
146 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
147 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
148 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | * ------------ |
||
153 | * The classical rules is like following: |
||
154 | * [ |
||
155 | * ['guid', 'required'], |
||
156 | * ['guid', 'unique'], |
||
157 | * ['guid', 'string', 'max' => 36], |
||
158 | * |
||
159 | * ['id', 'required'], |
||
160 | * ['id', 'unique'], |
||
161 | * ['id', 'string', 'max' => 4], |
||
162 | * |
||
163 | * ['created_at', 'safe'], |
||
164 | * ['updated_at', 'safe'], |
||
165 | * |
||
166 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
167 | * ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
||
168 | * ] |
||
169 | * @return array |
||
170 | */ |
||
171 | 84 | public function rules() |
|
175 | |||
176 | /** |
||
177 | * @inheritdoc |
||
178 | */ |
||
179 | 87 | public function behaviors() |
|
183 | |||
184 | /** |
||
185 | * Get total of contents which owned by their owner. |
||
186 | * @return integer |
||
187 | */ |
||
188 | 1 | public function countOfOwner() |
|
193 | |||
194 | /** |
||
195 | * Get content. |
||
196 | * @return mixed |
||
197 | */ |
||
198 | 1 | public function getContent() |
|
213 | |||
214 | /** |
||
215 | * Set content. |
||
216 | * @param mixed $content |
||
217 | */ |
||
218 | 24 | public function setContent($content) |
|
232 | |||
233 | /** |
||
234 | * Determines whether content could be edited. Your should implement this |
||
235 | * method by yourself. |
||
236 | * @return boolean |
||
237 | * @throws NotSupportedException |
||
238 | */ |
||
239 | public function getContentCanBeEdited() |
||
246 | |||
247 | /** |
||
248 | * Get blameable rules cache key. |
||
249 | * @return string cache key. |
||
250 | */ |
||
251 | 84 | public function getBlameableRulesCacheKey() |
|
255 | |||
256 | /** |
||
257 | * Get blameable rules cache tag. |
||
258 | * @return string cache tag |
||
259 | */ |
||
260 | 84 | public function getBlameableRulesCacheTag() |
|
264 | |||
265 | /** |
||
266 | * Get the rules associated with content to be blamed. |
||
267 | * @return array rules. |
||
268 | */ |
||
269 | 84 | public function getBlameableRules() |
|
295 | |||
296 | /** |
||
297 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
298 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
299 | * @return array rules. |
||
300 | */ |
||
301 | 84 | public function getBlameableAttributeRules() |
|
331 | |||
332 | /** |
||
333 | * Get the rules associated with `description` attribute. |
||
334 | * @return array rules. |
||
335 | */ |
||
336 | 84 | public function getDescriptionRules() |
|
352 | |||
353 | /** |
||
354 | * Get the rules associated with `content` and `contentType` attributes. |
||
355 | * @return array rules. |
||
356 | */ |
||
357 | 84 | public function getContentRules() |
|
388 | |||
389 | /** |
||
390 | * Set blameable rules. |
||
391 | * @param array $rules |
||
392 | */ |
||
393 | 84 | protected function setBlameableRules($rules = []) |
|
402 | |||
403 | /** |
||
404 | * Get blameable behaviors cache key. |
||
405 | * @return string cache key. |
||
406 | */ |
||
407 | 87 | public function getBlameableBehaviorsCacheKey() |
|
411 | |||
412 | /** |
||
413 | * Get blameable behaviors cache tag. |
||
414 | * @return string cache tag. |
||
415 | */ |
||
416 | 87 | public function getBlameableBehaviorsCacheTag() |
|
420 | |||
421 | /** |
||
422 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
423 | * array will be given. |
||
424 | * @return array |
||
425 | */ |
||
426 | 87 | public function getBlameableBehaviors() |
|
448 | |||
449 | /** |
||
450 | * Set blameable behaviors. |
||
451 | * @param array $behaviors |
||
452 | */ |
||
453 | 87 | protected function setBlameableBehaviors($behaviors = []) |
|
463 | |||
464 | /** |
||
465 | * Set description. |
||
466 | * @return string description. |
||
467 | */ |
||
468 | 1 | public function getDescription() |
|
473 | |||
474 | /** |
||
475 | * Get description. |
||
476 | * @param string $desc description. |
||
477 | * @return string|null description if enabled, or null if disabled. |
||
478 | */ |
||
479 | 1 | public function setDescription($desc) |
|
484 | |||
485 | /** |
||
486 | * Get blame who owned this blameable model. |
||
487 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
488 | * specify it in `init()` method. |
||
489 | * @return BaseUserQuery user. |
||
490 | */ |
||
491 | 8 | public function getUser() |
|
495 | |||
496 | /** |
||
497 | * |
||
498 | * @return |
||
499 | */ |
||
500 | 12 | public function getHost() |
|
506 | |||
507 | /** |
||
508 | * |
||
509 | * @param string $host |
||
510 | * @return type |
||
511 | */ |
||
512 | 42 | public function setHost($host) |
|
525 | |||
526 | /** |
||
527 | * |
||
528 | * @param BaseUserModel|string $user |
||
529 | * @return boolean |
||
530 | */ |
||
531 | public function setUser($user) |
||
535 | |||
536 | /** |
||
537 | * Get updater who updated this blameable model recently. |
||
538 | * NOTICE! This method will not check whether `$userClass` exists. You should |
||
539 | * specify it in `init()` method. |
||
540 | * @return BaseUserQuery user. |
||
541 | */ |
||
542 | 1 | public function getUpdater() |
|
552 | |||
553 | /** |
||
554 | * |
||
555 | * @param BaseUserModel|string $user |
||
556 | * @return boolean |
||
557 | */ |
||
558 | public function setUpdater($updater) |
||
574 | |||
575 | /** |
||
576 | * This event is triggered before the model update. |
||
577 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
578 | * override or modify it directly, unless you know the consequences. |
||
579 | * @param ModelEvent $event |
||
580 | */ |
||
581 | 10 | public function onContentChanged($event) |
|
587 | |||
588 | /** |
||
589 | * Return the current user's GUID if current model doesn't specify the owner |
||
590 | * yet, or return the owner's GUID if current model has been specified. |
||
591 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
592 | * override or modify it directly, unless you know the consequences. |
||
593 | * @param ModelEvent $event |
||
594 | * @return string the GUID of current user or the owner. |
||
595 | */ |
||
596 | 72 | public function onGetCurrentUserGuid($event) |
|
609 | |||
610 | /** |
||
611 | * Initialize type of content. the first of element[index is 0] of |
||
612 | * $contentTypes will be used. |
||
613 | * @param ModelEvent $event |
||
614 | */ |
||
615 | 12 | public function onInitContentType($event) |
|
629 | |||
630 | /** |
||
631 | * Initialize description property with $initDescription. |
||
632 | * @param ModelEvent $event |
||
633 | */ |
||
634 | 45 | public function onInitDescription($event) |
|
646 | |||
647 | /** |
||
648 | * Attach events associated with blameable model. |
||
649 | */ |
||
650 | 87 | public function initBlameableEvents() |
|
665 | |||
666 | /** |
||
667 | * @inheritdoc |
||
668 | */ |
||
669 | 32 | public function enabledFields() |
|
696 | |||
697 | /** |
||
698 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
699 | * identity will be taken. |
||
700 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
701 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
702 | * regarded as sum of models in one page. |
||
703 | * @param integer $currentPage The current page number, begun with 0. |
||
704 | * @param {$this->userClass} $identity |
||
705 | * @return static[] If no follows, null will be given, or return follow array. |
||
706 | */ |
||
707 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
714 | |||
715 | /** |
||
716 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
717 | * identity will be taken. If $identity doesn't has the follower, null will |
||
718 | * be given. |
||
719 | * @param integer $id user id. |
||
720 | * @param boolean $throwException |
||
721 | * @param {$this->userClass} $identity |
||
722 | * @return static |
||
723 | * @throws InvalidParamException |
||
724 | */ |
||
725 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
737 | |||
738 | /** |
||
739 | * Get total of follows of specified identity. |
||
740 | * @param {$this->userClass} $identity |
||
741 | * @return integer total. |
||
742 | */ |
||
743 | 1 | public static function countByIdentity($identity = null) |
|
747 | |||
748 | /** |
||
749 | * Get pagination, used for building contents page by page. |
||
750 | * @param integer $limit |
||
751 | * @param {$this->userClass} $identity |
||
752 | * @return Pagination |
||
753 | */ |
||
754 | public static function getPagination($limit = 10, $identity = null) |
||
763 | } |
||
764 |
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: