1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/lib/Internal/functions.php
Niklas Keller 79ab41e5bf Update php-cs-fixer to version 2 and upgrade rules
This also fixes the code style according to the new rules.
2017-04-24 16:22:02 +02:00

47 lines
1.2 KiB
PHP

<?php
namespace Amp\Internal;
/**
* Formats a stacktrace obtained via `debug_backtrace()`.
*
* @param array $trace Output of `debug_backtrace()`.
*
* @return string Formatted stacktrace.
*
* @internal
*/
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)));
}
/**
* 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");
}