12
12
* @package Ayesh\PHP_Timer
13
13
*/
14
14
class Timer {
15
- const FORMAT_PRECISE = FALSE ;
16
- const FORMAT_MILLISECONDS = 'ms ' ;
17
- const FORMAT_SECONDS = 's ' ;
18
- const FORMAT_HUMAN = 'h ' ;
15
+ public const FORMAT_PRECISE = FALSE ;
16
+ public const FORMAT_MILLISECONDS = 'ms ' ;
17
+ public const FORMAT_SECONDS = 's ' ;
18
+ public const FORMAT_HUMAN = 'h ' ;
19
19
20
20
/**
21
21
* Stores all the timers statically.
22
- * @var array
22
+ * @var Stopwatch[]
23
23
*/
24
24
static private $ timers = [];
25
25
26
- /**
27
- * Returns the current time as a float.
28
- * @return float
29
- */
30
- private static function getCurrentTime (): float {
31
- return microtime (true );
32
- }
33
-
34
26
/**
35
27
* Start or resume the timer.
36
28
*
@@ -43,18 +35,12 @@ private static function getCurrentTime(): float {
43
35
*
44
36
* @param string $key
45
37
*/
46
- public static function start (string $ key = 'default ' ) {
38
+ public static function start (string $ key = 'default ' ): void {
47
39
if (isset (self ::$ timers [$ key ])) {
48
- if (empty (self ::$ timers [$ key ][0 ])) {
49
- self ::$ timers [$ key ][0 ] = true ;
50
- self ::$ timers [$ key ][1 ] = self ::getCurrentTime ();
51
- }
52
- } else {
53
- self ::$ timers [$ key ] = [
54
- true ,
55
- self ::getCurrentTime (),
56
- 0
57
- ];
40
+ self ::$ timers [$ key ]->start ();
41
+ }
42
+ else {
43
+ self ::$ timers [$ key ] = new Stopwatch ();
58
44
}
59
45
}
60
46
@@ -63,51 +49,38 @@ public static function start(string $key = 'default') {
63
49
* To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll().
64
50
* @param string $key
65
51
*/
66
- public static function reset (string $ key = 'default ' ) {
52
+ public static function reset (string $ key = 'default ' ): void {
67
53
unset(self ::$ timers [$ key ]);
68
54
}
69
55
70
56
/**
71
57
* Resets ALL timers.
72
58
* To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset().
73
59
*/
74
- public static function resetAll () {
60
+ public static function resetAll (): void {
75
61
self ::$ timers = [];
76
62
}
77
63
78
- /**
79
- * Processes the internal timer state to return the time elapsed.
80
- * @param $value
81
- * @param $format
82
- * @return mixed
83
- */
84
- protected static function processTimerValue ($ value , $ format ) {
85
- if ($ value [0 ]) {
86
- return self ::formatTime ((self ::getCurrentTime () - $ value [1 ]) + $ value [2 ], $ format );
87
- }
88
- return self ::formatTime ($ value [2 ], $ format );
89
- }
90
-
91
64
/**
92
65
* Formats the given time the processor into the given format.
93
66
* @param $value
94
67
* @param $format
95
68
* @return float
96
69
*/
97
- private static function formatTime ($ value , $ format ) {
70
+ private static function formatTime ($ value , $ format ): string {
98
71
switch ($ format ) {
99
72
100
73
case static ::FORMAT_PRECISE :
101
- return $ value * 1000 ;
74
+ return ( string ) ( $ value * 1000 ) ;
102
75
103
76
case static ::FORMAT_MILLISECONDS :
104
- return round ($ value * 1000 , 2 );
77
+ return ( string ) round ($ value * 1000 , 2 );
105
78
106
79
case static ::FORMAT_SECONDS :
107
- return round ($ value , 3 );
80
+ return ( string ) round ($ value , 3 );
108
81
109
82
default :
110
- return $ value * 1000 ;
83
+ return ( string ) ( $ value * 1000 ) ;
111
84
}
112
85
}
113
86
@@ -128,7 +101,7 @@ private static function formatTime($value, $format) {
128
101
*/
129
102
public static function read (string $ key = 'default ' , $ format = self ::FORMAT_MILLISECONDS ) {
130
103
if (isset (self ::$ timers [$ key ])) {
131
- return self ::processTimerValue (self ::$ timers [$ key ], $ format );
104
+ return self ::formatTime (self ::$ timers [$ key ]-> read () , $ format );
132
105
}
133
106
throw new \LogicException ('Reading timer when the given key timer was not initialized. ' );
134
107
}
@@ -140,11 +113,9 @@ public static function read(string $key = 'default', $format = self::FORMAT_MILL
140
113
*
141
114
* @throws \LogicException If the attempted timer has not started already.
142
115
*/
143
- public static function stop ($ key = 'default ' ) {
116
+ public static function stop ($ key = 'default ' ): void {
144
117
if (isset (self ::$ timers [$ key ])) {
145
- $ ct = self ::getCurrentTime ();
146
- self ::$ timers [$ key ][0 ] = false ;
147
- self ::$ timers [$ key ][2 ] += $ ct - self ::$ timers [$ key ][1 ];
118
+ self ::$ timers [$ key ]->stop ();
148
119
} else {
149
120
throw new \LogicException ('Stopping timer when the given key timer was not initialized. ' );
150
121
}
0 commit comments