ClassMethodsHydrator::extract does not work with anonymous classes
luiz-brandao opened this issue · 2 comments
Bug Report
Q | A |
---|---|
Version(s) | 3.1.0 |
Summary
Using OptionalParametersFilter with an anonymous class fails saying a method does not exist.
Current behavior
The property FQN generated at ClassMethodsHydrator.php:157
does not work for anonymous classes. For instance, in my case I get something like this:
class@anonymous\000/path/to/file/MyTest.php0x7fabdb08bdf5::getSomeField
Consequently, OptionalParametersFilter.php:48
fails because new ReflectionMethod can't locate it without an object.
$reflectionMethod = new ReflectionMethod($property);
How to reproduce
See unit test:
Expected behavior
Values to be properly extracted
Possible solutions
It seems we can't extract using the FQN but instead we need to use a reference to the object.
$reflectionMethod = new ReflectionMethod($object, $methodName);
The issue with providing a fix is that the FilterInterface::filter
expects a string and changing that would affect the whole framework.
Locally, as a proof-of-concept, I managed to fix the issue by creating a new OptionalParametersFilter::filterWithoutFqn
method. Basically it's a copy of OptionalParametersFilter::filter that accepts an object instance and the method name instead of a property FQN. That also means I had to change ClassMethodsHydrator::extract
to call the new method.
Feedback from someone that has more in depth knowledge of this library would be essential.
Thank you.