Permalink
Browse files

Refactor (and remove workaround (which provided wrong results) for ed…

…ge case where REQUEST_TIME is not available)
  • Loading branch information...
1 parent 13e6131 commit d107f347d368dd8a384601398280c7c608390ab7 @sebastianbergmann committed Mar 7, 2017
Showing with 13 additions and 14 deletions.
  1. +13 −14 src/Timer.php
View
@@ -28,11 +28,6 @@ class PHP_Timer
private static $startTimes = array();
/**
- * @var float
- */
- public static $requestTime;
-
- /**
* Starts the timer.
*/
public static function start()
@@ -75,16 +70,28 @@ public static function secondsToTimeString($time)
* Formats the elapsed time since the start of the request as a string.
*
* @return string
+ *
+ * @throws RuntimeException
*/
public static function timeSinceStartOfRequest()
{
- return self::secondsToTimeString(microtime(true) - self::$requestTime);
+ if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
+ $startOfRequest = $_SERVER['REQUEST_TIME_FLOAT'];
+ } elseif (isset($_SERVER['REQUEST_TIME'])) {
+ $startOfRequest = $_SERVER['REQUEST_TIME'];
+ } else {
+ throw new RuntimeException('Cannot determine time at which the request started');
+ }
+
+ return self::secondsToTimeString(microtime(true) - $startOfRequest);
}
/**
* Returns the resources (time, memory) of the request as a string.
*
* @return string
+ *
+ * @throws RuntimeException
*/
public static function resourceUsage()
{
@@ -95,11 +102,3 @@ public static function resourceUsage()
);
}
}
-
-if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
- PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
-} elseif (isset($_SERVER['REQUEST_TIME'])) {
- PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME'];
-} else {
- PHP_Timer::$requestTime = microtime(true);
-}

0 comments on commit d107f34

Please sign in to comment.