Default enum value for a parameter throw an exception
forrest79 opened this issue · 10 comments
Hi, I figured out, that reflecting a class like this will fail on getting the default value for the $x
parameter:
enum TestEnum: int {
case X = 1;
}
class TestClass {
public function text(TestEnum $x = TestEnum::X) {
}
}
I have got Uncaught Roave\BetterReflection\NodeCompiler\Exception\UnableToCompileNode: Could not locate constant TestEnum::X while trying to evaluate constant expression in method TestClass::test() in file ...
.
As a quick workaround, I added something like this:
if ($classReflection instanceof ReflectionEnum) {
$case = $classReflection->getCase($constantName);
if ($case === null) {
throw Exception\UnableToCompileNode::becauseOfNotFoundClassConstantReference($context, $classReflection, $node);
}
return $case->getValue();
}
Into src/NodeCompiler/CompileNodeToValue.php
method getClassConstantValue()
. I wanted to return the correct enum instance, but I got another error, the enum is not supported as return value.
This is correct, because BetterReflection would have to instantiate the object, therefore loading it.
BTW, the exception message is obviously confusing:
Could not locate constant TestEnum::X while trying to evaluate constant expression in method TestClass::test() in file
We could improve that 🤔
Thanks for a rapid response. I solved this using getDefaultValueExpression
(my bad :-()
@forrest79 are we OK with just raising a better exception message here?
Absolutely :-) It was my fault, I was expecting, that Enum could be simply returned as a returned value.
@forrest79 are we OK with just raising a better exception message here? Would you be able to help with that, perhaps?
Yes, I will be glad to help. My idea is just to detect, if the class is an Enum and throw a different exception?
I'd say so, yep 👍