2017-03-29 17:25:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Internal;
|
|
|
|
|
2017-04-23 15:29:08 +02:00
|
|
|
/**
|
|
|
|
* Formats a stacktrace obtained via `debug_backtrace()`.
|
|
|
|
*
|
|
|
|
* @param array $trace Output of `debug_backtrace()`.
|
|
|
|
*
|
|
|
|
* @return string Formatted stacktrace.
|
|
|
|
*
|
2017-05-02 18:59:52 +02:00
|
|
|
* @codeCoverageIgnore
|
2017-04-23 15:29:08 +02:00
|
|
|
* @internal
|
|
|
|
*/
|
2017-03-29 17:25:44 +02:00
|
|
|
function formatStacktrace(array $trace): string {
|
|
|
|
return implode("\n", array_map(function ($e, $i) {
|
|
|
|
$line = "#{$i} {$e['file']}:{$e['line']} ";
|
|
|
|
|
|
|
|
if ($e["type"]) {
|
|
|
|
$line .= $e["class"] . $e["type"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line . $e["function"] . "()";
|
|
|
|
}, $trace, array_keys($trace)));
|
2017-04-23 15:29:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
2017-04-24 15:39:08 +02:00
|
|
|
}
|