Complex classes like TimestampTrait 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 TimestampTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | trait TimestampTrait |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string the attribute that will receive datetime value |
||
| 34 | * Set this property to false if you do not want to record the creation time. |
||
| 35 | */ |
||
| 36 | public $createdAtAttribute = 'create_time'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string the attribute that will receive datetime value. |
||
| 40 | * Set this property to false if you do not want to record the update time. |
||
| 41 | */ |
||
| 42 | public $updatedAtAttribute = 'update_time'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var integer Determine the format of timestamp. |
||
| 46 | */ |
||
| 47 | public $timeFormat = 0; |
||
| 48 | public static $timeFormatDatetime = 0; |
||
| 49 | public static $timeFormatTimestamp = 1; |
||
| 50 | public static $initDatetime = '1970-01-01 00:00:00'; |
||
| 51 | public static $initTimestamp = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int|false the expiration in seconds, or false if it will not be expired. |
||
| 55 | */ |
||
| 56 | public $expiredAt = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Closure |
||
| 60 | */ |
||
| 61 | public $expiredRemovingCallback; |
||
| 62 | public static $eventExpiredRemoved = 'expiredRemoved'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Check this entity whether expired. |
||
| 66 | * @return boolean |
||
| 67 | */ |
||
| 68 | 39 | public function getIsExpired() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Remove myself if expired. |
||
| 79 | * @return boolean |
||
| 80 | */ |
||
| 81 | 38 | public function removeIfExpired() |
|
| 92 | |||
| 93 | 38 | public function onRemoveExpired($event) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 100 | * You can override this method to customize the return value. |
||
| 101 | * @param \yii\base\ModelEvent $event |
||
| 102 | * @return string Date & Time. |
||
| 103 | * @since 1.1 |
||
| 104 | */ |
||
| 105 | 48 | public static function getCurrentDatetime($event) |
|
| 106 | { |
||
| 107 | 48 | $sender = $event->sender; |
|
| 108 | 48 | return $sender->currentDatetime(); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get current date & time, by current time format. |
||
| 113 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 114 | */ |
||
| 115 | 48 | public function currentDatetime() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get offset date & time, by current time format. |
||
| 128 | * @param string|int $time Date &time string or timestamp. |
||
| 129 | * @param int $offset Offset int seconds. |
||
| 130 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 131 | */ |
||
| 132 | 6 | public function offsetDatetime($time = null, $offset = 0) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
||
| 145 | * @param \yii\base\ModelEvent $event |
||
| 146 | * @return string|int |
||
| 147 | */ |
||
| 148 | 8 | public static function getInitDatetime($event) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get init date & time, by current time format. |
||
| 156 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 157 | */ |
||
| 158 | 8 | public function initDatetime() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Check whether the attribute is init datetime. |
||
| 171 | * @param mixed $attribute |
||
| 172 | * @return boolean |
||
| 173 | */ |
||
| 174 | 4 | protected function isInitDatetime($attribute) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
| 187 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 188 | * override or modify it directly, unless you know the consequences. |
||
| 189 | * @param \yii\base\Event $event |
||
| 190 | * @return string Date & Time. |
||
| 191 | * @since 1.1 |
||
| 192 | */ |
||
| 193 | 48 | public function onUpdateCurrentDatetime($event) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Behaviors associated with timestamp. |
||
| 200 | * @return array behaviors |
||
| 201 | */ |
||
| 202 | 15 | public function getTimestampBehaviors() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Get createdAt. |
||
| 216 | * @return string timestamp |
||
| 217 | */ |
||
| 218 | 39 | public function getCreatedAt() |
|
| 219 | { |
||
| 220 | 39 | $createdAtAttribute = $this->createdAtAttribute; |
|
| 221 | 39 | if (!is_string($createdAtAttribute)) { |
|
| 222 | 2 | return null; |
|
| 223 | } |
||
| 224 | 37 | return $this->$createdAtAttribute; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get rules associated with createdAtAttribute. |
||
| 229 | * @return array rules |
||
| 230 | */ |
||
| 231 | 15 | public function getCreatedAtRules() |
|
| 232 | { |
||
| 233 | 15 | if (!$this->createdAtAttribute) { |
|
| 234 | 2 | return []; |
|
| 235 | } |
||
| 236 | return [ |
||
| 237 | 13 | [[$this->createdAtAttribute], 'safe'], |
|
| 238 | 13 | ]; |
|
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get updatedAt. |
||
| 243 | * @return string timestamp |
||
| 244 | */ |
||
| 245 | public function getUpdatedAt() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get rules associated with updatedAtAttribute. |
||
| 256 | * @return array rules |
||
| 257 | */ |
||
| 258 | 15 | public function getUpdatedAtRules() |
|
| 267 | |||
| 268 | 13 | public function enabledTimestampFields() |
|
| 269 | { |
||
| 270 | 13 | $fields = []; |
|
| 280 | } |
||
| 281 |
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.