The "\Go\ParserReflection\ReflectionClass::getMethods" returns wrong instance objects
The \Go\ParserReflection\ReflectionClass::getMethods method is used, then instead of returning all objects of \Go\ParserReflection\ReflectionMethod class it:
- returns that class methods using
\Go\ParserReflection\ReflectionMethodclass objects - returns inherited methods using
\ReflectionMethodclass objects

This results in Fatal Error, when trying to process PHP 7 code using PHP 5.
Same for getProperties and maybe other methods that collect data from parent classes.
The above described behavior happens due following code (2 places) where if class is autoloaded prior to library usage, then \ReflectionClass instead of \Go\ParserReflection\ReflectionClass is used:
- https://github.com/goaop/parser-reflection/blob/master/src/ReflectionParameter.php#L187-L189
- https://github.com/goaop/parser-reflection/blob/master/src/Traits/ReflectionClassLikeTrait.php#L448
This might be done like so to avoid running extra code, when we already got class autoloaded anyway. However such optimization results in inconsistent classes being returned out of the library.
I'm proposing to doing something like, class exists:
$reflectionClass = new \ReflectionClass($className);
$reflectionFile = \Go\ParserReflection\ReflectionFile($reflectionClass->getFileName());
foreach ( $reflectionFile->getFileNamespaces() as $namespace ) {
foreach ( $namespace->getClasses() as $class ) {
if ( $class->getName() === $className ) {
return $class;
}
}
}
Hello, this library was designed in such a way to be as close to the original behavior as possible, so if class is already loaded into the memory (or internal one) then no need in parser reflection objects, because you will receive the same information from internal reflection much easier.
So, you shouldn't expect ParserReflection instances all the time. Of course, I can wrap all reflection objects with my own, but this will be simple proxying without any benefits and I decided to return just original objects instead.
I wanted to make use of PHP-Parser feature to parse PHP code of newer versions (e.g. PHP 7), than one it currently runs on (PHP 5). For that purpose a reparsing of already autoloaded classes is needed. Not sure what a performance hit of such behavior should be. Theoretically that shouldn't be much of an issue considering that none of user-defined classes should autoloaded prior parsing is started anyway.
For now, in my project, I've limited library usage to handle only methods/properties of processed class (without looking into parent classes). This guaranteed me correct class instances and ability to parse PHP 7 code while using PHP 5 do do this.