1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/lib/InvalidYieldError.php
Niklas Keller 1299e51ca6 Add tests for InvalidYieldError, improve key display
Without something like var_export, null and false are casted to an empty string.
2016-12-11 17:08:17 +01:00

37 lines
1.0 KiB
PHP

<?php declare(strict_types = 1);
namespace Amp;
class InvalidYieldError extends \Error {
/**
* @param \Generator $generator
* @param string $prefix
*/
public function __construct(\Generator $generator, string $prefix) {
$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);
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()
));
}
}