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 |
||
| 31 | trait TimestampTrait |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string|false the attribute that receive datetime value |
||
| 35 | * Set this property to false if you do not want to record the creation time. |
||
| 36 | */ |
||
| 37 | public $createdAtAttribute = 'created_at'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string|false the attribute that receive datetime value. |
||
| 41 | * Set this property to false if you do not want to record the update time. |
||
| 42 | */ |
||
| 43 | public $updatedAtAttribute = 'updated_at'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string|false the attribute that determine when this entity expire. |
||
| 47 | * If this entity does not expire, set to false. |
||
| 48 | */ |
||
| 49 | public $expiredAfterAttribute = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var integer Determine the format of timestamp. |
||
| 53 | */ |
||
| 54 | public $timeFormat = 0; |
||
| 55 | public static $timeFormatDatetime = 0; |
||
| 56 | public static $timeFormatTimestamp = 1; |
||
| 57 | public static $initDatetime = '1970-01-01 00:00:00'; |
||
| 58 | public static $initTimestamp = 0; |
||
| 59 | /** |
||
| 60 | * @var Closure |
||
| 61 | */ |
||
| 62 | public $expiredRemovingCallback; |
||
| 63 | public static $eventExpiredRemoved = 'expiredRemoved'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Check this entity whether expired. |
||
| 67 | * This feature require creation time. If creation time didn't record, false |
||
| 68 | * is returned. |
||
| 69 | * This feature also need expiration duration. If expiration duration didn't |
||
| 70 | * record, false is returned. |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | 95 | public function getIsExpired() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Remove myself if expired. |
||
| 84 | * The `expiredRemovingCallback` will be called before removing itself, |
||
| 85 | * then it would trigger `static::$eventExpiredRemoved` event, and attach |
||
| 86 | * the removing results. |
||
| 87 | * @return boolean |
||
| 88 | */ |
||
| 89 | 92 | public function removeIfExpired() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Remove self. |
||
| 104 | * You can override this method for implementing more complex features. |
||
| 105 | * @see delete() |
||
| 106 | * @return integer |
||
| 107 | */ |
||
| 108 | 7 | public function removeSelf() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * We recommened you attach this event when after finding this active record. |
||
| 115 | * @param ModelEvent $event |
||
| 116 | * @return boolean |
||
| 117 | */ |
||
| 118 | 92 | public function onRemoveExpired($event) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 125 | * You can override this method to customize the return value. |
||
| 126 | * @param ModelEvent $event |
||
| 127 | * @return string Date & Time. |
||
| 128 | */ |
||
| 129 | 184 | public static function getCurrentDatetime($event) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Get current date & time, by current time format. |
||
| 137 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 138 | */ |
||
| 139 | 185 | public function currentDatetime() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Get offset date & time, by current time format. |
||
| 152 | * @param string|int $time Date &time string or timestamp. |
||
| 153 | * @param int $offset Offset in seconds. |
||
| 154 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 155 | */ |
||
| 156 | 13 | public function offsetDatetime($time = null, $offset = 0) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 169 | * @param ModelEvent $event |
||
| 170 | * @return string|int |
||
| 171 | */ |
||
| 172 | 3 | public static function getInitDatetime($event) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Get init date & time, by current time format. |
||
| 180 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 181 | */ |
||
| 182 | 4 | public function initDatetime() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Check whether the attribute is init datetime. |
||
| 195 | * @param mixed $attribute |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | 3 | protected function isInitDatetime($attribute) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
| 211 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 212 | * override or modify it directly, unless you know the consequences. |
||
| 213 | * @param ModelEvent $event |
||
| 214 | * @return string Date & Time. |
||
| 215 | */ |
||
| 216 | 184 | public function onUpdateCurrentDatetime($event) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Behaviors associated with timestamp. |
||
| 223 | * @return array behaviors |
||
| 224 | */ |
||
| 225 | 215 | public function getTimestampBehaviors() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Get creation time. |
||
| 239 | * @return string timestamp |
||
| 240 | */ |
||
| 241 | 113 | public function getCreatedAt() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get rules associated with createdAtAttribute. |
||
| 252 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
| 253 | * the creation time automatically. |
||
| 254 | * Under normal circumstances is not recommended to amend. |
||
| 255 | * If `createdAtAttribute` is not specified, the empty array will be given. |
||
| 256 | * @return array rules |
||
| 257 | */ |
||
| 258 | 195 | public function getCreatedAtRules() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Get update time. |
||
| 270 | * @return string timestamp |
||
| 271 | */ |
||
| 272 | 35 | public function getUpdatedAt() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Get rules associated with `updatedAtAttribute`. |
||
| 283 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
| 284 | * the last update time automatically. |
||
| 285 | * Under normal circumstances is not recommended to amend. |
||
| 286 | * If `updatedAtAttribute` is not specified, the empty array will be given. |
||
| 287 | * @return array rules |
||
| 288 | */ |
||
| 289 | 195 | public function getUpdatedAtRules() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get expiration duration. |
||
| 301 | * If `expiredAfterAttribute` is not specified, false will be given. |
||
| 302 | * @return boolean |
||
| 303 | */ |
||
| 304 | 95 | public function getExpiredAfter() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Set expiration duration (in seconds). |
||
| 314 | * If `expiredAfterAttribute` is not specified, this feature will be skipped, |
||
| 315 | * and return false. |
||
| 316 | * @param integer $expiredAfter the duration after which is expired (in seconds). |
||
| 317 | * @return boolean|integer |
||
| 318 | */ |
||
| 319 | 15 | public function setExpiredAfter($expiredAfter) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Get rules associated with `expiredAfterAttribute`. |
||
| 329 | * The default rule is unsigned integer. |
||
| 330 | * Under normal circumstances is not recommended to amend. |
||
| 331 | * If `expiredAfterAttribute` is not specified, the empty array will be given. |
||
| 332 | * @return array The key of array is not specified. |
||
| 333 | */ |
||
| 334 | 195 | public function getExpiredAfterRules() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get enabled fields associated with timestamp, including `createdAtAttribute`, |
||
| 346 | * `updatedAtAttribute` and `expiredAfterAttribute`. |
||
| 347 | * @return array field list. The keys of array are not specified. |
||
| 348 | */ |
||
| 349 | 80 | public function enabledTimestampFields() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Check it has been ever edited. |
||
| 366 | * The judgement principle is to compare the creation time and the last update time, |
||
| 367 | * if one of the two does not exist, then that has not been modified, |
||
| 368 | * if both exist but not consistent, that modified. |
||
| 369 | * You can override this method to implement more complex function. |
||
| 370 | * @return boolean Whether this entity has ever been edited. |
||
| 371 | */ |
||
| 372 | 11 | public function hasEverEdited() |
|
| 379 | } |
||
| 380 |
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.