Closed klimick closed 2 months ago
Do you mean that it should throw a different exception?
I think, this is the way it should be. Native reflection behaves the same way:
is this really reflection behavior? exactly the same exception is thrown if you just create a class. https://3v4l.org/HHHia
Is it possible to avoid throwing any exception? Maybe return some container with or without a value.
I haven't tried to get inferred type of a constant, but I expect to get never
. I don't expect any exception in this case.
I understand when an exception is thrown if a file could not be read due to unix permissions. But a tool that claims to work in a static analysis environment should not panic with exceptions in such simple situations.
is this really reflection behavior?
The declaration of a class itself does not throw anything on the parsing level. TyphoonReflection
and ReflectionClassConstant
do not throw as well: https://3v4l.org/5KA9s until you request the value.
I haven't tried to get inferred type of a constant, but I expect to get never. I don't expect any exception in this case.
Not exactly, but no exception is thrown:
use Typhoon\Reflection\TypeKind;
use Typhoon\Reflection\TyphoonReflector;
final class C
{
public const VALUE = [] & 'str';
}
$constant = TyphoonReflector::build()->reflectClass(C::class)->constants()['VALUE'];
var_dump($constant->type(TypeKind::Inferred)); // null
var_dump($constant->type(TypeKind::Resolved)); // types::mixed
But a tool that claims to work in a static analysis environment should not panic with exceptions in such simple situations
Static analysis tools must not use $constantReflection->value()
, because it returns the actual runtime values an potentially autoloads smth. For instance, if you have
final class MyClass {}
function myFunction(MyClass $object = new MyClass()): void {}
then TyphoonReflector::build()->reflectFunction('myFunction')->parameters()['object']->defaultValue()
will autoload MyClass
, because the actual value is requested. There's nothing we can or should do about this.
Maybe return some container with or without a value.
$reflectionClassConstant->value(...)
is your container. :)
To summarize: