From 90e2e4bdf7e2ab532a0b7b1e1a4647454d4cf9d5 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 31 Oct 2024 19:33:27 +0100 Subject: [PATCH] Util\Timing::getHumanReadableDuration: magic numbers to constants Make the code more self-descriptive and use less "magic numbers" by declaring a couple of constants. --- src/Util/Timing.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Util/Timing.php b/src/Util/Timing.php index 36ba400e76..9f9a43e1bb 100644 --- a/src/Util/Timing.php +++ b/src/Util/Timing.php @@ -12,6 +12,20 @@ class Timing { + /** + * Number of milliseconds in a minute. + * + * @var int + */ + const MINUTE_IN_MS = 60000; + + /** + * Number of milliseconds in a second. + * + * @var int + */ + const SECOND_IN_MS = 1000; + /** * The start time of the run in microseconds. * @@ -67,15 +81,15 @@ public static function getDuration() public static function getHumanReadableDuration($duration) { $timeString = ''; - if ($duration >= 60000) { - $mins = floor($duration / 60000); - $secs = round((fmod($duration, 60000) / 1000), 2); + if ($duration >= self::MINUTE_IN_MS) { + $mins = floor($duration / self::MINUTE_IN_MS); + $secs = round((fmod($duration, self::MINUTE_IN_MS) / self::SECOND_IN_MS), 2); $timeString = $mins.' mins'; if ($secs >= 0.01) { $timeString .= ", $secs secs"; } - } else if ($duration >= 1000) { - $timeString = round(($duration / 1000), 2).' secs'; + } else if ($duration >= self::SECOND_IN_MS) { + $timeString = round(($duration / self::SECOND_IN_MS), 2).' secs'; } else { $timeString = round($duration).'ms'; }