parser-reflection
parser-reflection copied to clipboard
Add support for analysing multiple classes in one file without asking the locator
It seems calling getMethods()
on a parsed child class causes Fatal error: Uncaught exception 'InvalidArgumentException' with message 'xxx was not found by locator'
.
For example,
Sample.php
<?php
namespace Foo\Bar;
abstract class TestBase {
public function __construct() {
}
}
class Test extends TestBase {
public function __construct() {
parent::__construct();
}
}
test.php
include( './vendor/autoload.php' );
$parsedFile = new \Go\ParserReflection\ReflectionFile( __DIR__ . '/Sample.php');
foreach ($parsedFile->getFileNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
echo "Found class method: ", $class->getName(), '::', $method->getName(), PHP_EOL;
}
}
}
By running test.php
, the line foreach ($class->getMethods() as $method) {
causes the above error.
You are trying to analyze several classes, defined in one file. Currently it's not supported by the engine. You should use PSR standard and put each class into it's own file, so composer should know where to look for them.
Alternative way is to define your custom locator for that:
If project uses a custom autoloader then you should follow the next steps:
Create a new class that implements \Go\ParserReflection\LocatorInterface Create an instance of that class and pass it to the ReflectionEngine::init() method for initial configuration
I want to convert this issue to the improvement request, but not for the next version. Engine will be able to look at parsed classes in the cache.
Looking forward to it.
I'm thinking issue #73 is a duplicate of this, but I might mark it the other way around, as I already have a working solution for this. @lisachenko, what do you think? (I don't expect to have a PR publicly available for about 2 weeks... pending permission.)