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 |
||
| 30 | trait TimestampTrait |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string the attribute that will receive datetime value |
||
| 35 | * Set this property to false if you do not want to record the creation time. |
||
| 36 | */ |
||
| 37 | public $createdAtAttribute = 'create_time'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string the attribute that will receive datetime value. |
||
| 41 | * Set this property to false if you do not want to record the update time. |
||
| 42 | */ |
||
| 43 | public $updatedAtAttribute = 'update_time'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var integer Determine the format of timestamp. |
||
| 47 | */ |
||
| 48 | public $timeFormat = 0; |
||
| 49 | public static $timeFormatDatetime = 0; |
||
| 50 | public static $timeFormatTimestamp = 1; |
||
| 51 | public static $initDatetime = '1970-01-01 00:00:00'; |
||
| 52 | public static $initTimestamp = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int|false the expiration in seconds, or false if it will not be expired. |
||
| 56 | */ |
||
| 57 | public $expiredAt = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Closure |
||
| 61 | */ |
||
| 62 | public $expiredRemovingCallback; |
||
| 63 | public static $eventExpiredRemoved = 'expiredRemoved'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Check this entity whether expired. |
||
| 67 | * @return boolean |
||
| 68 | */ |
||
| 69 | 45 | public function getIsExpired() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Remove myself if expired. |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | 44 | public function removeIfExpired() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * We recommened you attach this event when after finding this active record. |
||
| 96 | * @param ModelEvent $event |
||
| 97 | * @return boolean |
||
| 98 | */ |
||
| 99 | 44 | public function onRemoveExpired($event) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 106 | * You can override this method to customize the return value. |
||
| 107 | * @param ModelEvent $event |
||
| 108 | * @return string Date & Time. |
||
| 109 | * @since 1.1 |
||
| 110 | */ |
||
| 111 | 58 | public static function getCurrentDatetime($event) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get current date & time, by current time format. |
||
| 119 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 120 | */ |
||
| 121 | 58 | public function currentDatetime() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Get offset date & time, by current time format. |
||
| 134 | * @param string|int $time Date &time string or timestamp. |
||
| 135 | * @param int $offset Offset int seconds. |
||
| 136 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 137 | */ |
||
| 138 | 9 | public function offsetDatetime($time = null, $offset = 0) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp.s |
||
| 151 | * @param ModelEvent $event |
||
| 152 | * @return string|int |
||
| 153 | */ |
||
| 154 | 8 | public static function getInitDatetime($event) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Get init date & time, by current time format. |
||
| 162 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 163 | */ |
||
| 164 | 8 | public function initDatetime() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Check whether the attribute is init datetime. |
||
| 177 | * @param mixed $attribute |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | 4 | protected function isInitDatetime($attribute) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
| 193 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 194 | * override or modify it directly, unless you know the consequences. |
||
| 195 | * @param ModelEvent $event |
||
| 196 | * @return string Date & Time. |
||
| 197 | * @since 1.1 |
||
| 198 | */ |
||
| 199 | 58 | public function onUpdateCurrentDatetime($event) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Behaviors associated with timestamp. |
||
| 206 | * @return array behaviors |
||
| 207 | */ |
||
| 208 | 17 | public function getTimestampBehaviors() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get createdAt. |
||
| 222 | * @return string timestamp |
||
| 223 | */ |
||
| 224 | 45 | public function getCreatedAt() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get rules associated with createdAtAttribute. |
||
| 235 | * @return array rules |
||
| 236 | */ |
||
| 237 | 17 | public function getCreatedAtRules() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Get updatedAt. |
||
| 249 | * @return string timestamp |
||
| 250 | */ |
||
| 251 | public function getUpdatedAt() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get rules associated with updatedAtAttribute. |
||
| 262 | * @return array rules |
||
| 263 | */ |
||
| 264 | 17 | public function getUpdatedAtRules() |
|
| 273 | |||
| 274 | 16 | public function enabledTimestampFields() |
|
| 286 | } |
||
| 287 |
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.