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 |
||
| 28 | trait MessageTrait |
||
| 29 | { |
||
| 30 | use MutualTrait; |
||
| 31 | |||
| 32 | public $attachmentAttribute = 'attachment'; |
||
| 33 | public $receivedAtAttribute = 'received_at'; |
||
| 34 | public $readAtAttribute = 'read_at'; |
||
| 35 | public static $eventMessageReceived = 'messageReceived'; |
||
| 36 | public static $eventMessageRead = 'messageRead'; |
||
| 37 | public $permitChangeContent = false; |
||
| 38 | public $permitChangeReceivedAt = false; |
||
| 39 | public $permitChangeReadAt = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Whether the message has been received. |
||
| 43 | * Note: This trait should be used for models which use [[TimestampTrait]]. |
||
| 44 | * @return boolean |
||
| 45 | */ |
||
| 46 | 1 | public function hasBeenReceived() |
|
| 47 | { |
||
| 48 | 1 | return is_string($this->receivedAtAttribute) ? !$this->isInitDatetime($this->getReceivedAt()) : false; |
|
|
|
|||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Whether the message has been read. |
||
| 53 | * If a message has been read, it must have been received. |
||
| 54 | * Note: This trait should be used for models which use [[TimestampTrait]]. |
||
| 55 | * @return boolean |
||
| 56 | */ |
||
| 57 | 1 | public function hasBeenRead() |
|
| 58 | { |
||
| 59 | 1 | return is_string($this->readAtAttribute) ? !$this->isInitDatetime($this->getReadAt()) : false; |
|
| 60 | } |
||
| 61 | |||
| 62 | 1 | public function touchReceived() |
|
| 63 | { |
||
| 64 | 1 | return $this->setReceivedAt(static::currentDatetime()); |
|
| 65 | } |
||
| 66 | |||
| 67 | public function touchRead() |
||
| 71 | |||
| 72 | 1 | public function getReceivedAt() |
|
| 73 | { |
||
| 74 | 1 | if (is_string($this->receivedAtAttribute) && !empty($this->receivedAtAttribute)) { |
|
| 80 | |||
| 81 | 4 | public function setReceivedAt($receivedAt) |
|
| 89 | |||
| 90 | 1 | public function getReadAt() |
|
| 98 | |||
| 99 | 4 | public function setReadAt($readAt) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | * @param ModelEvent $event |
||
| 111 | */ |
||
| 112 | 4 | public function onInitReceivedAtAttribute($event) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * |
||
| 121 | * @param ModelEvent $event |
||
| 122 | */ |
||
| 123 | 4 | public function onInitReadAtAttribute($event) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * We consider you have received the message if you read it. |
||
| 132 | * @param ModelEvent $event |
||
| 133 | */ |
||
| 134 | 1 | public function onReadAtChanged($event) |
|
| 135 | { |
||
| 136 | 1 | $sender = $event->sender; |
|
| 137 | /* @var $sender static */ |
||
| 138 | 1 | $raAttribute = $sender->readAtAttribute; |
|
| 139 | 1 | if (!is_string($raAttribute) || empty($raAttribute)) { |
|
| 140 | return; |
||
| 141 | } |
||
| 142 | 1 | $reaAttribute = $sender->receivedAtAttribute; |
|
| 143 | 1 | if (is_string($reaAttribute) && !$sender->isInitDatetime($sender->$raAttribute) && $sender->isInitDatetime($sender->$reaAttribute)) { |
|
| 144 | $sender->$reaAttribute = $sender->currentDatetime(); |
||
| 145 | } |
||
| 146 | // If it is permitted to change read time, it will return directly. |
||
| 147 | 1 | if ($sender->permitChangeReadAt) { |
|
| 148 | return; |
||
| 149 | } |
||
| 150 | 1 | $oldRa = $sender->getOldAttribute($raAttribute); |
|
| 151 | 1 | if ($oldRa != null && !$sender->isInitDatetime($oldRa) && $sender->$raAttribute != $oldRa) { |
|
| 152 | $sender->$raAttribute = $oldRa; |
||
| 153 | } |
||
| 154 | 1 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * You are not allowed to change receiving time if you have received it. |
||
| 158 | * @param ModelEvent $event |
||
| 159 | */ |
||
| 160 | 1 | public function onReceivedAtChanged($event) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * You are not allowed to change the content if it is not new message. |
||
| 179 | * @param ModelEvent $event |
||
| 180 | */ |
||
| 181 | 1 | public function onContentChanged($event) |
|
| 182 | { |
||
| 183 | 1 | $sender = $event->sender; |
|
| 184 | // If it is permitted to change content, then it will return directly. |
||
| 185 | 1 | if ($sender->permitChangeContent) { |
|
| 186 | return; |
||
| 187 | } |
||
| 188 | // The messgage will be reversed if it changed (current message isn't |
||
| 189 | // same as the old). |
||
| 190 | 1 | $cAttribute = $sender->contentAttribute; |
|
| 191 | 1 | $oldContent = $sender->getOldAttribute($cAttribute); |
|
| 192 | 1 | if ($oldContent != $sender->$cAttribute) { |
|
| 193 | $sender->$cAttribute = $oldContent; |
||
| 194 | } |
||
| 195 | 1 | } |
|
| 196 | |||
| 197 | /** |
||
| 198 | * |
||
| 199 | * @param AfterSaveEvent $event |
||
| 200 | */ |
||
| 201 | 1 | public function onMessageUpdated($event) |
|
| 202 | { |
||
| 203 | 1 | $sender = $event->sender; |
|
| 204 | /* @var $sender static */ |
||
| 205 | 1 | $reaAttribute = $sender->receivedAtAttribute; |
|
| 206 | 1 | if (isset($event->changedAttributes[$reaAttribute]) && $event->changedAttributes[$reaAttribute] != $sender->$reaAttribute) { |
|
| 207 | 1 | $sender->trigger(static::$eventMessageReceived); |
|
| 208 | } |
||
| 209 | 1 | $raAttribute = $sender->readAtAttribute; |
|
| 210 | 1 | if (isset($event->changedAttributes[$raAttribute]) && $event->changedAttributes[$raAttribute] != $sender->$raAttribute) { |
|
| 211 | $sender->trigger(static::$eventMessageRead); |
||
| 212 | } |
||
| 213 | 1 | } |
|
| 214 | |||
| 215 | /** |
||
| 216 | * |
||
| 217 | */ |
||
| 218 | 4 | public function initMessageEvents() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Return rules associated with message. |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 4 | public function getMessageRules() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @inheritdoc |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | 4 | public function rules() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritdoc |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 4 | public function enabledFields() |
|
| 278 | } |
||
| 279 |
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
Idableprovides a methodequalsIdthat 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.