laminas/laminas-hydrator

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:

#26

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.

Honestly, this all predates anonymous classes by a number of years, so I'm not surprised it doesn't work.

I've labelled it as both a bug and a feature (bug, in that it should work; feature, in that it's technically new functionality), and built PR #30 off of your PR #26 to address it.