1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 09:29:45 +01:00
amp/lib/Internal/functions.php

85 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Internal;
/**
* Formats a stacktrace obtained via `debug_backtrace()`.
*
* @param array $trace Output of `debug_backtrace()`.
*
* @return string Formatted stacktrace.
*
* @codeCoverageIgnore
* @internal
*/
2018-06-18 20:00:01 +02:00
function formatStacktrace(array $trace): string
{
return \implode("\n", \array_map(function ($e, $i) {
2017-12-02 17:03:38 +01:00
$line = "#{$i} ";
if (isset($e["file"])) {
$line .= "{$e['file']}:{$e['line']} ";
}
2017-12-07 18:23:19 +01:00
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.
2018-06-18 20:00:01 +02:00
* @param mixed $given Given value.
*
* @return \TypeError
*
* @internal
*/
2018-06-18 20:00:01 +02:00
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;
2019-05-31 19:48:32 +02:00
if (\PHP_INT_SIZE === 4) {
if ($startTime === null) {
2019-05-31 19:48:32 +02:00
$startTime = \PHP_VERSION_ID >= 70300 ? \hrtime(false)[0] : \time();
}
2019-05-31 19:48:32 +02:00
if (\PHP_VERSION_ID >= 70300) {
list($seconds, $nanoseconds) = \hrtime(false);
$seconds -= $startTime;
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
}
return ((\microtime(true) - $startTime) * 1000);
}
if (\PHP_VERSION_ID >= 70300) {
list($seconds, $nanoseconds) = \hrtime(false);
return (int) ($seconds * 1000 + $nanoseconds / 1000000);
}
2019-05-31 19:48:32 +02:00
return (\microtime(true) * 1000);
}