Skip to content

Commit 762e6fc

Browse files
committed
Rewrite Timer class to use Stopwatch objects
1 parent 9ce270b commit 762e6fc

File tree

1 file changed

+20
-49
lines changed

1 file changed

+20
-49
lines changed

src/Timer.php

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,17 @@
1212
* @package Ayesh\PHP_Timer
1313
*/
1414
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';
1919

2020
/**
2121
* Stores all the timers statically.
22-
* @var array
22+
* @var Stopwatch[]
2323
*/
2424
static private $timers = [];
2525

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-
3426
/**
3527
* Start or resume the timer.
3628
*
@@ -43,18 +35,12 @@ private static function getCurrentTime(): float {
4335
*
4436
* @param string $key
4537
*/
46-
public static function start(string $key = 'default') {
38+
public static function start(string $key = 'default'): void {
4739
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();
5844
}
5945
}
6046

@@ -63,51 +49,38 @@ public static function start(string $key = 'default') {
6349
* To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll().
6450
* @param string $key
6551
*/
66-
public static function reset(string $key = 'default') {
52+
public static function reset(string $key = 'default'): void {
6753
unset(self::$timers[$key]);
6854
}
6955

7056
/**
7157
* Resets ALL timers.
7258
* To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset().
7359
*/
74-
public static function resetAll() {
60+
public static function resetAll(): void {
7561
self::$timers = [];
7662
}
7763

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-
9164
/**
9265
* Formats the given time the processor into the given format.
9366
* @param $value
9467
* @param $format
9568
* @return float
9669
*/
97-
private static function formatTime($value, $format) {
70+
private static function formatTime($value, $format): string {
9871
switch ($format) {
9972

10073
case static::FORMAT_PRECISE:
101-
return $value * 1000;
74+
return (string) ($value * 1000);
10275

10376
case static::FORMAT_MILLISECONDS:
104-
return round($value * 1000, 2);
77+
return (string) round($value * 1000, 2);
10578

10679
case static::FORMAT_SECONDS:
107-
return round($value, 3);
80+
return (string) round($value, 3);
10881

10982
default:
110-
return $value * 1000;
83+
return (string) ($value * 1000);
11184
}
11285
}
11386

@@ -128,7 +101,7 @@ private static function formatTime($value, $format) {
128101
*/
129102
public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) {
130103
if (isset(self::$timers[$key])) {
131-
return self::processTimerValue(self::$timers[$key], $format);
104+
return self::formatTime(self::$timers[$key]->read(), $format);
132105
}
133106
throw new \LogicException('Reading timer when the given key timer was not initialized.');
134107
}
@@ -140,11 +113,9 @@ public static function read(string $key = 'default', $format = self::FORMAT_MILL
140113
*
141114
* @throws \LogicException If the attempted timer has not started already.
142115
*/
143-
public static function stop($key = 'default') {
116+
public static function stop($key = 'default'): void {
144117
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();
148119
} else {
149120
throw new \LogicException('Stopping timer when the given key timer was not initialized.');
150121
}

0 commit comments

Comments
 (0)