mirror of
https://github.com/danog/amp.git
synced 2024-12-12 09:29:45 +01:00
5316e741b7
Prior version made time run backwards… oops.
83 lines
1.8 KiB
PHP
83 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Amp\Internal;
|
|
|
|
/**
|
|
* Formats a stacktrace obtained via `debug_backtrace()`.
|
|
*
|
|
* @param array $trace Output of `debug_backtrace()`.
|
|
*
|
|
* @return string Formatted stacktrace.
|
|
*
|
|
* @codeCoverageIgnore
|
|
* @internal
|
|
*/
|
|
function formatStacktrace(array $trace): string
|
|
{
|
|
return \implode("\n", \array_map(function ($e, $i) {
|
|
$line = "#{$i} ";
|
|
|
|
if (isset($e["file"])) {
|
|
$line .= "{$e['file']}:{$e['line']} ";
|
|
}
|
|
|
|
if (isset($e["type"])) {
|
|
$line .= $e["class"] . $e["type"];
|
|
}
|
|
|
|
return $line . $e["function"] . "()";
|
|
}, $trace, \array_keys($trace)));
|
|
}
|
|
|
|
/**
|
|
* Creates a `TypeError` with a standardized error message.
|
|
*
|
|
* @param string[] $expected Expected types.
|
|
* @param mixed $given Given value.
|
|
*
|
|
* @return \TypeError
|
|
*
|
|
* @internal
|
|
*/
|
|
function createTypeError(array $expected, $given): \TypeError
|
|
{
|
|
$givenType = \is_object($given) ? \sprintf("instance of %s", \get_class($given)) : \gettype($given);
|
|
|
|
if (\count($expected) === 1) {
|
|
$expectedType = "Expected the following type: " . \array_pop($expected);
|
|
} else {
|
|
$expectedType = "Expected one of the following types: " . \implode(", ", $expected);
|
|
}
|
|
|
|
return new \TypeError("{$expectedType}; {$givenType} given");
|
|
}
|
|
|
|
/**
|
|
* Returns the current time relative to an arbitrary point in time.
|
|
*
|
|
* @return int Time in milliseconds.
|
|
*/
|
|
function getCurrentTime(): int
|
|
{
|
|
static $startTime;
|
|
|
|
if (\PHP_VERSION_ID >= 70300) {
|
|
list($seconds, $nanoseconds) = \hrtime(false);
|
|
$now = $seconds * 1000 + $nanoseconds / 1000000;
|
|
} else {
|
|
$now = \microtime(true) * 1000;
|
|
}
|
|
|
|
$time = (int) $now;
|
|
|
|
if ($time < 0) {
|
|
if ($startTime === null) {
|
|
$startTime = $now;
|
|
}
|
|
|
|
$time = (int) ($now - $startTime);
|
|
}
|
|
|
|
return $time;
|
|
}
|