1
1
<?php
2
- declare (strict_types = 1 );
2
+ declare (strict_types= 1 );
3
3
4
4
namespace Ayesh \PHP_Timer ;
5
5
12
12
* @package Ayesh\PHP_Timer
13
13
*/
14
14
class Timer {
15
- public const FORMAT_PRECISE = FALSE ;
15
+
16
+ public const FORMAT_PRECISE = false ;
17
+
16
18
public const FORMAT_MILLISECONDS = 'ms ' ;
19
+
17
20
public const FORMAT_SECONDS = 's ' ;
21
+
18
22
public const FORMAT_HUMAN = 'h ' ;
19
23
20
24
private const TIMES = [
@@ -25,6 +29,7 @@ class Timer {
25
29
26
30
/**
27
31
* Stores all the timers statically.
32
+ *
28
33
* @var Stopwatch[]
29
34
*/
30
35
static private $ timers = [];
@@ -33,13 +38,14 @@ class Timer {
33
38
* Start or resume the timer.
34
39
*
35
40
* Call this method to start the timer with a given key. The default key
36
- * is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset()
41
+ * is "default", and used in @param string $key
42
+ *
43
+ * @see \Ayesh\PHP_Timer\Timer::read() and reset()
37
44
* methods as well
38
45
*
39
46
* Calling this with the same $key will not restart the timer if it has already
40
47
* started.
41
48
*
42
- * @param string $key
43
49
*/
44
50
public static function start (string $ key = 'default ' ): void {
45
51
if (isset (self ::$ timers [$ key ])) {
@@ -52,8 +58,9 @@ public static function start(string $key = 'default'): void {
52
58
53
59
/**
54
60
* Resets a specific timer, or default timer if not passed the $key parameter.
55
- * To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll().
56
- * @param string $key
61
+ * To reset all timers, call @param string $key
62
+ *
63
+ * @see \Ayesh\PHP_Timer\Timer::resetAll().
57
64
*/
58
65
public static function reset (string $ key = 'default ' ): void {
59
66
unset(self ::$ timers [$ key ]);
@@ -67,10 +74,36 @@ public static function resetAll(): void {
67
74
self ::$ timers = [];
68
75
}
69
76
77
+ /**
78
+ * Returns the time elapsed in the format requested in the $format parameter.
79
+ * To access a specific timer, pass the same key that
80
+ *
81
+ * @param string $key The key that the timer was started with. Default value is
82
+ * "default" throughout the class.
83
+ * @param string $format
84
+ *
85
+ * @return mixed The formatted time.
86
+ * @throws \LogicException
87
+ * @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not
88
+ * started, a \LogicException will be thrown.
89
+ *
90
+ * The default format is milliseconds. See the class constants for additional
91
+ * formats.
92
+ *
93
+ */
94
+ public static function read (string $ key = 'default ' , $ format = self ::FORMAT_MILLISECONDS ) {
95
+ if (isset (self ::$ timers [$ key ])) {
96
+ return self ::formatTime (self ::$ timers [$ key ]->read (), $ format );
97
+ }
98
+ throw new \LogicException ('Reading timer when the given key timer was not initialized. ' );
99
+ }
100
+
70
101
/**
71
102
* Formats the given time the processor into the given format.
103
+ *
72
104
* @param float $value
73
105
* @param string|bool $format
106
+ *
74
107
* @return string
75
108
*/
76
109
private static function formatTime (float $ value , $ format ): string {
@@ -103,28 +136,6 @@ private static function secondsToTimeString(float $time): string {
103
136
return $ ms . ' ms ' ;
104
137
}
105
138
106
- /**
107
- * Returns the time elapsed in the format requested in the $format parameter.
108
- * To access a specific timer, pass the same key that
109
- * @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not
110
- * started, a \LogicException will be thrown.
111
- *
112
- * The default format is milliseconds. See the class constants for additional
113
- * formats.
114
- *
115
- * @param string $key The key that the timer was started with. Default value is
116
- * "default" throughout the class.
117
- * @param string $format
118
- * @return mixed The formatted time.
119
- * @throws \LogicException
120
- */
121
- public static function read (string $ key = 'default ' , $ format = self ::FORMAT_MILLISECONDS ) {
122
- if (isset (self ::$ timers [$ key ])) {
123
- return self ::formatTime (self ::$ timers [$ key ]->read (), $ format );
124
- }
125
- throw new \LogicException ('Reading timer when the given key timer was not initialized. ' );
126
- }
127
-
128
139
/**
129
140
* Stops the timer with the given key. Default key is "default"
130
141
*
@@ -135,13 +146,15 @@ public static function read(string $key = 'default', $format = self::FORMAT_MILL
135
146
public static function stop ($ key = 'default ' ): void {
136
147
if (isset (self ::$ timers [$ key ])) {
137
148
self ::$ timers [$ key ]->stop ();
138
- } else {
149
+ }
150
+ else {
139
151
throw new \LogicException ('Stopping timer when the given key timer was not initialized. ' );
140
152
}
141
153
}
142
154
143
155
/**
144
156
* Return a list of timer names. Note that resetting a timer removes the timer.
157
+ *
145
158
* @return string[]
146
159
*/
147
160
public static function getTimers (): array {
0 commit comments