Skip to content

Commit 372b627

Browse files
committed
refactor: replace switch with match
1 parent 89c4222 commit 372b627

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/Timer.php

+13-21
Original file line numberDiff line numberDiff line change
@@ -88,40 +88,32 @@ public static function resetAll(): void {
8888
*
8989
* @return mixed The formatted time, formatted by the formatter string passed for $format.
9090
* @throws LogicException
91-
* If the timer was not started, a \LogicException will be thrown. Use @see \Ayesh\PHP_Timer\Timer::start()
91+
* If the timer was not started, a \LogicException will be thrown. Use @see Timer::start
9292
* to start a timer.
9393
*/
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);
94+
public static function read(string $key = 'default', string $format = self::FORMAT_MILLISECONDS): string {
95+
if (!isset(self::$timers[$key])) {
96+
throw new LogicException('Reading timer when the given key timer was not initialized.');
9797
}
9898

99-
throw new LogicException('Reading timer when the given key timer was not initialized.');
99+
return self::formatTime(self::$timers[$key]->read(), $format);
100100
}
101101

102102
/**
103103
* Formats the given time the processor into the given format.
104104
*
105105
* @param float $value
106-
* @param string|bool $format
106+
* @param string $format
107107
*
108108
* @return string
109109
*/
110-
private static function formatTime(float $value, $format): string {
111-
switch ($format) {
112-
case static::FORMAT_MILLISECONDS:
113-
return (string) round($value * 1000, 2);
114-
115-
case static::FORMAT_SECONDS:
116-
return (string) round($value, 3);
117-
118-
case static::FORMAT_HUMAN:
119-
return static::secondsToTimeString($value);
120-
121-
case static::FORMAT_PRECISE:
122-
default:
123-
return (string) ($value * 1000);
124-
}
110+
private static function formatTime(float $value, string $format): string {
111+
return match ($format) {
112+
static::FORMAT_MILLISECONDS => (string) round($value * 1000, 2),
113+
static::FORMAT_SECONDS => (string) round($value, 3),
114+
static::FORMAT_HUMAN => static::secondsToTimeString($value),
115+
default => (string) ($value * 1000),
116+
};
125117
}
126118

127119
private static function secondsToTimeString(float $time): string {

0 commit comments

Comments
 (0)