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 = 1; |
||
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 | 219 | public function getIsExpired() |
|
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 | 214 | 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 | 214 | 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 | 342 | 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 | 342 | 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 | 1 | public function currentUtcDatetime() |
|
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 | 13 | public function offsetDatetime($time = null, $offset = 0) |
|
200 | |||
201 | /** |
||
202 | * Get offset date & time relative to Greenwich time(UTC), by current time format. |
||
203 | * @param string|int $time Date &time string or timestamp. |
||
204 | * @param int $offset Offset in seconds. |
||
205 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
206 | */ |
||
207 | public function offsetUtcDatetime($time = null, $offset = 0) |
||
217 | |||
218 | /** |
||
219 | * Get init date & time in format of "Y-m-d H:i:s" or timestamp. |
||
220 | * @param ModelEvent $event |
||
221 | * @return string|int |
||
222 | */ |
||
223 | 23 | public static function getInitDatetime($event) |
|
228 | |||
229 | /** |
||
230 | * Get init date & time, by current time format. |
||
231 | * @return string|int Date & time string if format is datetime, or timestamp. |
||
232 | */ |
||
233 | 24 | public function initDatetime() |
|
243 | |||
244 | /** |
||
245 | * Check whether the attribute is init datetime. |
||
246 | * @param mixed $attribute |
||
247 | * @return boolean |
||
248 | */ |
||
249 | 7 | protected function isInitDatetime($attribute) |
|
259 | |||
260 | /** |
||
261 | * Get the current date & time in format of "Y-m-d H:i:s". |
||
262 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
263 | * override or modify it directly, unless you know the consequences. |
||
264 | * @param ModelEvent $event |
||
265 | * @return string Date & Time. |
||
266 | */ |
||
267 | 342 | public function onUpdateCurrentDatetime($event) |
|
271 | |||
272 | /** |
||
273 | * Behaviors associated with timestamp. |
||
274 | * @return array behaviors |
||
275 | */ |
||
276 | 380 | public function getTimestampBehaviors() |
|
287 | |||
288 | /** |
||
289 | * Get creation time. |
||
290 | * @return string timestamp |
||
291 | */ |
||
292 | 242 | public function getCreatedAt() |
|
300 | |||
301 | /** |
||
302 | * Get rules associated with createdAtAttribute. |
||
303 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
304 | * the creation time automatically. |
||
305 | * Under normal circumstances is not recommended to amend. |
||
306 | * If `createdAtAttribute` is not specified, the empty array will be given. |
||
307 | * @return array rules |
||
308 | */ |
||
309 | 353 | public function getCreatedAtRules() |
|
318 | |||
319 | /** |
||
320 | * Get update time. |
||
321 | * @return string timestamp |
||
322 | */ |
||
323 | 35 | public function getUpdatedAt() |
|
331 | |||
332 | /** |
||
333 | * Get rules associated with `updatedAtAttribute`. |
||
334 | * The default rule is safe. Because the [[TimestampBehavior]] will attach |
||
335 | * the last update time automatically. |
||
336 | * Under normal circumstances is not recommended to amend. |
||
337 | * If `updatedAtAttribute` is not specified, the empty array will be given. |
||
338 | * @return array rules |
||
339 | */ |
||
340 | 353 | public function getUpdatedAtRules() |
|
349 | |||
350 | /** |
||
351 | * Get expiration duration. |
||
352 | * If `expiredAfterAttribute` is not specified, false will be given. |
||
353 | * @return boolean |
||
354 | */ |
||
355 | 219 | public function getExpiredAfter() |
|
362 | |||
363 | /** |
||
364 | * Set expiration duration (in seconds). |
||
365 | * If `expiredAfterAttribute` is not specified, this feature will be skipped, |
||
366 | * and return false. |
||
367 | * @param integer $expiredAfter the duration after which is expired (in seconds). |
||
368 | * @return boolean|integer |
||
369 | */ |
||
370 | 15 | public function setExpiredAfter($expiredAfter) |
|
377 | |||
378 | /** |
||
379 | * Get rules associated with `expiredAfterAttribute`. |
||
380 | * The default rule is unsigned integer. |
||
381 | * Under normal circumstances is not recommended to amend. |
||
382 | * If `expiredAfterAttribute` is not specified, the empty array will be given. |
||
383 | * @return array The key of array is not specified. |
||
384 | */ |
||
385 | 353 | public function getExpiredAfterRules() |
|
394 | |||
395 | /** |
||
396 | * Get enabled fields associated with timestamp, including `createdAtAttribute`, |
||
397 | * `updatedAtAttribute` and `expiredAfterAttribute`. |
||
398 | * @return array field list. The keys of array are not specified. |
||
399 | */ |
||
400 | 138 | public function enabledTimestampFields() |
|
414 | |||
415 | /** |
||
416 | * Check it has been ever edited. |
||
417 | * The judgement principle is to compare the creation time and the last update time, |
||
418 | * if one of the two does not exist, then that has not been modified, |
||
419 | * if both exist but not consistent, that modified. |
||
420 | * You can override this method to implement more complex function. |
||
421 | * @return boolean Whether this entity has ever been edited. |
||
422 | */ |
||
423 | 11 | public function hasEverEdited() |
|
430 | } |
||
431 |
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
Idable
provides a methodequalsId
that 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.