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 | public $timeType = 0; |
||
| 60 | public static $timeTypeUtc = 0; |
||
| 61 | public static $timeTypeLocal = 1; |
||
| 62 | public static $timeTypes = [ |
||
| 63 | 0 => 'GMT', |
||
| 64 | 1 => 'local', |
||
| 65 | ]; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var Closure |
||
| 69 | */ |
||
| 70 | public $expiredRemovingCallback; |
||
| 71 | public static $eventExpiredRemoved = 'expiredRemoved'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Check this entity whether expired. |
||
| 75 | * This feature require creation time. If creation time didn't record, false |
||
| 76 | * is returned. |
||
| 77 | * This feature also need expiration duration. If expiration duration didn't |
||
| 78 | * record, false is returned. |
||
| 79 | * @return boolean |
||
| 80 | */ |
||
| 81 | 220 | public function getIsExpired() |
|
| 82 | { |
||
| 83 | 220 | $createdAt = $this->getCreatedAt(); |
|
| 84 | 220 | if ($this->getExpiredAfter() === false || $createdAt === null) { |
|
| 85 | 212 | return false; |
|
| 86 | } |
||
| 87 | 11 | if ($this->timeType == static::$timeTypeLocal) { |
|
| 88 | return $this->getDatetimeOffset($this->offsetDatetime($this->currentDatetime(), -$this->getExpiredAfter()), $createdAt) > 0; |
||
| 89 | 11 | } elseif ($this->timeType == static::$timeTypeUtc) { |
|
| 90 | 11 | return $this->getDatetimeOffset($this->offsetDatetime($this->currentUtcDatetime(), -$this->getExpiredAfter()), $createdAt) > 0; |
|
| 91 | } |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Remove myself if expired. |
||
| 97 | * The `expiredRemovingCallback` will be called before removing itself, |
||
| 98 | * then it would trigger `static::$eventExpiredRemoved` event, and attach |
||
| 99 | * the removing results. |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | 215 | public function removeIfExpired() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Remove self. |
||
| 117 | * You can override this method for implementing more complex features. |
||
| 118 | * @see delete() |
||
| 119 | * @return integer |
||
| 120 | */ |
||
| 121 | 7 | public function removeSelf() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * We recommened you attach this event when after finding this active record. |
||
| 128 | * @param ModelEvent $event |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | 215 | public function onRemoveExpired($event) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the current date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 138 | * You can override this method to customize the return value. |
||
| 139 | * @param ModelEvent $event |
||
| 140 | * @return string Date & Time. |
||
| 141 | */ |
||
| 142 | 343 | public static function getCurrentDatetime($event) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get current date & time, by current time format. |
||
| 156 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 157 | */ |
||
| 158 | 8 | public function currentDatetime() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get current Greenwich date & time (UTC), by current time format. |
||
| 171 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 172 | */ |
||
| 173 | 342 | public function currentUtcDatetime() |
|
| 174 | { |
||
| 175 | 342 | if ($this->timeFormat === static::$timeFormatDatetime) { |
|
| 176 | 334 | return gmdate('Y-m-d H:i:s'); |
|
| 177 | } |
||
| 178 | 9 | if ($this->timeFormat === static::$timeFormatTimestamp) { |
|
| 179 | 9 | return time(); |
|
| 180 | } |
||
| 181 | return null; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get offset date & time, by current time format. |
||
| 186 | * @param string|int $time Date &time string or timestamp. |
||
| 187 | * @param int $offset Offset in seconds. |
||
| 188 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 189 | */ |
||
| 190 | 15 | public function offsetDatetime($time = null, $offset = 0) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Calculate the time difference(in seconds). |
||
| 203 | * @param string|integer $datetime1 |
||
| 204 | * @param string|integer $datetime2 |
||
| 205 | * @return integer Positive integer if $datetime1 is later than $datetime2, and vise versa. |
||
| 206 | */ |
||
| 207 | 11 | public function getDatetimeOffset($datetime1, $datetime2 = null) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp. |
||
| 225 | * @param ModelEvent $event |
||
| 226 | * @return string|int |
||
| 227 | */ |
||
| 228 | 23 | public static function getInitDatetime($event) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get init date & time, by current time format. |
||
| 236 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
| 237 | */ |
||
| 238 | 24 | public function initDatetime() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Check whether the attribute is init datetime. |
||
| 251 | * @param mixed $attribute |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | 7 | protected function isInitDatetime($attribute) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
| 267 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
| 268 | * override or modify it directly, unless you know the consequences. |
||
| 269 | * @param ModelEvent $event |
||
| 270 | * @return string Date & Time. |
||
| 271 | */ |
||
| 272 | 343 | public function onUpdateCurrentDatetime($event) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Behaviors associated with timestamp. |
||
| 279 | * @return array behaviors |
||
| 280 | */ |
||
| 281 | 383 | public function getTimestampBehaviors() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get creation time. |
||
| 295 | * @return string timestamp |
||
| 296 | */ |
||
| 297 | 243 | public function getCreatedAt() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get rules associated with createdAtAttribute. |
||
| 308 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
| 309 | * the creation time automatically. |
||
| 310 | * Under normal circumstances is not recommended to amend. |
||
| 311 | * If `createdAtAttribute` is not specified, the empty array will be given. |
||
| 312 | * @return array rules |
||
| 313 | */ |
||
| 314 | 354 | public function getCreatedAtRules() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get update time. |
||
| 326 | * @return string timestamp |
||
| 327 | */ |
||
| 328 | 35 | public function getUpdatedAt() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Get rules associated with `updatedAtAttribute`. |
||
| 339 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
| 340 | * the last update time automatically. |
||
| 341 | * Under normal circumstances is not recommended to amend. |
||
| 342 | * If `updatedAtAttribute` is not specified, the empty array will be given. |
||
| 343 | * @return array rules |
||
| 344 | */ |
||
| 345 | 354 | public function getUpdatedAtRules() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Get expiration duration. |
||
| 357 | * If `expiredAfterAttribute` is not specified, false will be given. |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | 220 | public function getExpiredAfter() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Set expiration duration (in seconds). |
||
| 370 | * If `expiredAfterAttribute` is not specified, this feature will be skipped, |
||
| 371 | * and return false. |
||
| 372 | * @param integer $expiredAfter the duration after which is expired (in seconds). |
||
| 373 | * @return boolean|integer |
||
| 374 | */ |
||
| 375 | 15 | public function setExpiredAfter($expiredAfter) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get rules associated with `expiredAfterAttribute`. |
||
| 385 | * The default rule is unsigned integer. |
||
| 386 | * Under normal circumstances is not recommended to amend. |
||
| 387 | * If `expiredAfterAttribute` is not specified, the empty array will be given. |
||
| 388 | * @return array The key of array is not specified. |
||
| 389 | */ |
||
| 390 | 354 | public function getExpiredAfterRules() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Get enabled fields associated with timestamp, including `createdAtAttribute`, |
||
| 402 | * `updatedAtAttribute` and `expiredAfterAttribute`. |
||
| 403 | * @return array field list. The keys of array are not specified. |
||
| 404 | */ |
||
| 405 | 138 | public function enabledTimestampFields() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Check it has been ever edited. |
||
| 422 | * The judgement principle is to compare the creation time and the last update time, |
||
| 423 | * if one of the two does not exist, then that has not been modified, |
||
| 424 | * if both exist but not consistent, that modified. |
||
| 425 | * You can override this method to implement more complex function. |
||
| 426 | * @return boolean Whether this entity has ever been edited. |
||
| 427 | */ |
||
| 428 | 11 | public function hasEverEdited() |
|
| 435 | } |
||
| 436 |
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.