Complex classes like MessageTrait 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 MessageTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | trait MessageTrait |
||
23 | { |
||
24 | |||
25 | public $otherGuidAttribute = 'other_guid'; |
||
26 | public $attachmentAttribute = 'attachment'; |
||
27 | public $receivedAtAttribute = 'received_at'; |
||
28 | public $readAtAttribute = 'read_at'; |
||
29 | public static $eventMessageReceived = 'messageReceived'; |
||
30 | public static $eventMessageRead = 'messageRead'; |
||
31 | public $permitChangeContent = false; |
||
32 | public $permitChangeReceivedAt = false; |
||
33 | public $permitChangeReadAt = false; |
||
34 | |||
35 | 4 | public function hasBeenReceived() |
|
39 | |||
40 | 4 | public function hasBeenRead() |
|
44 | |||
45 | 2 | public function touchReceived() |
|
49 | |||
50 | 2 | public function touchRead() |
|
54 | |||
55 | 4 | public function getReceivedAt() |
|
63 | |||
64 | 8 | public function setReceivedAt($receivedAt) |
|
72 | |||
73 | 4 | public function getReadAt() |
|
81 | |||
82 | 8 | public function setReadAt($readAt) |
|
90 | |||
91 | /** |
||
92 | * @param \yii\base\ModelEvent $event |
||
93 | */ |
||
94 | 8 | public function onInitReceivedAtAttribute($event) |
|
100 | |||
101 | /** |
||
102 | * @param \yii\base\ModelEvent $event |
||
103 | */ |
||
104 | 8 | public function onInitReadAtAttribute($event) |
|
110 | |||
111 | /** |
||
112 | * We consider you have received the message if you read it. |
||
113 | * @param \yii\base\ModelEvent $event |
||
114 | */ |
||
115 | 4 | public function onReadAtChanged($event) |
|
116 | { |
||
117 | 4 | $sender = $event->sender; |
|
118 | 4 | $raAttribute = $sender->readAtAttribute; |
|
119 | 4 | if (!is_string($raAttribute)) { |
|
120 | return; |
||
121 | } |
||
122 | 4 | $reaAttribute = $sender->receivedAtAttribute; |
|
123 | 4 | if (is_string($reaAttribute) && !$sender->isInitDatetime($sender->$raAttribute) && $sender->isInitDatetime($sender->$reaAttribute)) { |
|
124 | 2 | $sender->$reaAttribute = $sender->currentDatetime(); |
|
125 | 2 | } |
|
126 | 4 | if ($sender->permitChangeReadAt) { |
|
127 | return; |
||
128 | } |
||
129 | 4 | $oldRa = $sender->getOldAttribute($raAttribute); |
|
130 | 4 | if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) { |
|
131 | $sender->$raAttribute = $oldRa; |
||
132 | return; |
||
133 | } |
||
134 | 4 | } |
|
135 | |||
136 | /** |
||
137 | * You are not allowed to change receive time if you have received it. |
||
138 | * @param \yii\base\ModelEvent $event |
||
139 | */ |
||
140 | 4 | public function onReceivedAtChanged($event) |
|
156 | |||
157 | /** |
||
158 | * You are not allowed to change the content if it is not new message. |
||
159 | * @param \yii\base\ModelEvent $event |
||
160 | */ |
||
161 | 4 | public function onContentChanged($event) |
|
173 | |||
174 | /** |
||
175 | * Trigger message received or read events. |
||
176 | * @param \yii\db\AfterSaveEvent $event |
||
177 | */ |
||
178 | 4 | public function onMessageUpdated($event) |
|
190 | |||
191 | 8 | public function initMessageEvents() |
|
200 | |||
201 | 8 | public function getMessageRules() |
|
221 | |||
222 | 8 | public function rules() |
|
226 | |||
227 | 8 | public function enabledFields() |
|
244 | } |
||
245 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.