1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/lib/InvalidYieldError.php
Aaron Piotrowski 39b95255d2 Reduce code duplication in Coroutine
Hot path is still duplicated for performance, but conversion of non-promise values to promises should be a single method.
2017-03-15 19:10:23 -05:00

38 lines
1.1 KiB
PHP

<?php
namespace Amp;
class InvalidYieldError extends \Error {
/**
* @param \Generator $generator
* @param string $prefix
* @param \Throwable|null $previous
*/
public function __construct(\Generator $generator, string $prefix, \Throwable $previous = null) {
$yielded = $generator->current();
$prefix .= \sprintf(
"; %s yielded at key %s",
\is_object($yielded) ? \get_class($yielded) : \gettype($yielded),
\var_export($generator->key(), true)
);
if (!$generator->valid()) {
parent::__construct($prefix, 0, $previous);
return;
}
$reflGen = new \ReflectionGenerator($generator);
$exeGen = $reflGen->getExecutingGenerator();
if ($isSubgenerator = ($exeGen !== $generator)) {
$reflGen = new \ReflectionGenerator($exeGen);
}
parent::__construct(\sprintf(
"%s on line %s in %s",
$prefix,
$reflGen->getExecutingLine(),
$reflGen->getExecutingFile()
), 0, $previous);
}
}