Skip to content

Commit

Permalink
Util\Timing::getHumanReadableDuration: magic numbers to constants
Browse files Browse the repository at this point in the history
Make the code more self-descriptive and use less "magic numbers" by declaring a couple of constants.
  • Loading branch information
jrfnl committed Nov 2, 2024
1 parent 296b477 commit 90e2e4b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Util/Timing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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';
}
Expand Down

0 comments on commit 90e2e4b

Please sign in to comment.