Commit 791dc99a authored by Jan Kuchař's avatar Jan Kuchař

wrapped cases when reflection fails

parent e4275632
Pipeline #14379 passed with stages
in 52 seconds
......@@ -4,7 +4,6 @@ namespace Grifart\Enum;
use Grifart\Enum\Internal\InstanceRegister;
use Grifart\Enum\Internal\Meta;
use PHPStan\Reflection\ClassReflection;
/**
* Enum
......@@ -39,8 +38,13 @@ abstract class Enum
* @return string[]|int[]
*/
protected static function getConstantToScalar(): array
{ // todo: move this to Meta?
return (new \ReflectionClass(static::class))->getConstants();
{
try {
return (new \ReflectionClass(static::class))
->getConstants();
} catch (\ReflectionException $e) {
throw new ReflectionFailedException($e);
}
}
/**
......@@ -86,9 +90,13 @@ abstract class Enum
private static function getEnumClassName(): string
{
$ref = new \ReflectionClass(static::class);
if ($ref->isAnonymous()) { // anonymous objects are used for values
$ref = $ref->getMethod('provideInstances')->getDeclaringClass();
try {
$ref = new \ReflectionClass(static::class);
if ($ref->isAnonymous()) { // anonymous objects are used for values
$ref = $ref->getMethod('provideInstances')->getDeclaringClass();
}
} catch (\ReflectionException $e) {
throw new ReflectionFailedException($e);
}
return $ref->getName();
......
......@@ -11,7 +11,17 @@
namespace Grifart\Enum;
// Project root exceptions:
final class UsageException extends \LogicException {}
use Throwable;
class UsageException extends \LogicException {}
abstract class RuntimeException extends \RuntimeException {}
final class MissingValueDeclarationException extends RuntimeException {}
\ No newline at end of file
final class MissingValueDeclarationException extends RuntimeException {}
final class ReflectionFailedException extends UsageException {
public function __construct(\ReflectionException $previous)
{
parent::__construct('PHP reflection failed.', 0, $previous);
}
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment