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 |
||
21 | trait MessageTrait |
||
22 | { |
||
23 | |||
24 | public $otherGuidAttribute = 'other_guid'; |
||
25 | public $attachmentAttribute = 'attachment'; |
||
26 | public $expiration = 604800; // 7 days |
||
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 | |||
34 | 4 | public function hasBeenReceived() |
|
38 | |||
39 | 4 | public function hasBeenRead() |
|
43 | |||
44 | 2 | public function touchReceived() |
|
48 | |||
49 | 2 | public function touchRead() |
|
53 | |||
54 | 4 | public function getReceivedAt() |
|
55 | { |
||
56 | 4 | if (is_string($this->receivedAtAttribute)) { |
|
57 | 4 | $raAttribute = $this->receivedAtAttribute; |
|
58 | 4 | return $this->$raAttribute; |
|
59 | } |
||
60 | return null; |
||
61 | } |
||
62 | |||
63 | 6 | public function setReceivedAt($receivedAt) |
|
71 | |||
72 | 4 | public function getReadAt() |
|
73 | { |
||
74 | 4 | if (is_string($this->readAtAttribute)) { |
|
75 | 4 | $raAttribute = $this->readAtAttribute; |
|
76 | 4 | return $this->$raAttribute; |
|
77 | } |
||
78 | return null; |
||
79 | } |
||
80 | |||
81 | 6 | public function setReadAt($readAt) |
|
89 | |||
90 | /** |
||
91 | * @param \yii\base\ModelEvent $event |
||
92 | */ |
||
93 | 6 | public function onInitReceivedAtAttribute($event) |
|
99 | |||
100 | /** |
||
101 | * @param \yii\base\ModelEvent $event |
||
102 | */ |
||
103 | 6 | public function onInitReadAtAttribute($event) |
|
109 | |||
110 | /** |
||
111 | * We consider you have received the message if you read it. |
||
112 | * @param \yii\base\ModelEvent $event |
||
113 | */ |
||
114 | 4 | public function onReadAtChanged($event) |
|
131 | |||
132 | /** |
||
133 | * You are not allowed to change receive time if you have received it. |
||
134 | * @param \yii\base\ModelEvent $event |
||
135 | */ |
||
136 | 4 | public function onReceivedAtChanged($event) |
|
149 | |||
150 | /** |
||
151 | * You are not allowed to change the content if it is not new message. |
||
152 | * @param \yii\base\ModelEvent $event |
||
153 | */ |
||
154 | 4 | public function onContentChanged($event) |
|
166 | |||
167 | /** |
||
168 | * Trigger message received or read events. |
||
169 | * @param \yii\db\AfterSaveEvent $event |
||
170 | */ |
||
171 | 4 | public function onMessageUpdated($event) |
|
183 | |||
184 | 6 | public function initMessageEvents() |
|
193 | |||
194 | 6 | public function getMessageRules() |
|
211 | |||
212 | 6 | public function rules() |
|
216 | |||
217 | 6 | public function enabledFields() |
|
232 | } |
||
233 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.