BetterReflection: add support for file locator

This commit is contained in:
TomasVotruba 2017-09-30 15:25:04 +02:00
parent d0786f30b4
commit ddd03ebcda
4 changed files with 96 additions and 2 deletions

View File

@ -4,6 +4,7 @@ namespace Rector\BetterReflection\Reflector;
use Rector\BetterReflection\SourceLocator\SourceLocatorFactory;
use Roave\BetterReflection\Reflector\ClassReflector;
use SplFileInfo;
final class ClassReflectorFactory
{
@ -21,4 +22,11 @@ final class ClassReflectorFactory
{
return new ClassReflector($this->sourceLocatorFactory->create());
}
public function createWithFile(SplFileInfo $fileInfo): ClassReflector
{
$sourceLocator = $this->sourceLocatorFactory->createWithFile($fileInfo);
return new ClassReflector($sourceLocator);
}
}

View File

@ -8,7 +8,9 @@ use Roave\BetterReflection\SourceLocator\Type\AutoloadSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\EvaledCodeSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
use SplFileInfo;
final class SourceLocatorFactory
{
@ -24,10 +26,39 @@ final class SourceLocatorFactory
public function create(): SourceLocator
{
return new MemoizingSourceLocator(new AggregateSourceLocator([
return $this->wrapInMemoizinhSourceLocator($this->createCommonLocators());
}
public function createWithFile(SplFileInfo $fileInfo): SourceLocator
{
return $this->wrapInMemoizinhSourceLocator(
$this->createCommonLocators() +
[$this->createFileSourceLocator($fileInfo)]
);
}
private function createFileSourceLocator(SplFileInfo $fileInfo): SingleFileSourceLocator
{
return new SingleFileSourceLocator($fileInfo->getRealPath(), $this->locator);
}
/**
* @return SourceLocator[]
*/
private function createCommonLocators(): array
{
return [
new PhpInternalSourceLocator($this->locator),
new EvaledCodeSourceLocator($this->locator),
new AutoloadSourceLocator($this->locator),
]));
];
}
/**
* @param SourceLocator[] $sourceLocators
*/
private function wrapInMemoizinhSourceLocator(array $sourceLocators): MemoizingSourceLocator
{
return new MemoizingSourceLocator(new AggregateSourceLocator($sourceLocators));
}
}

View File

@ -0,0 +1,48 @@
<?php declare(strict_types=1);
namespace Rector\BetterReflection\Tests\Reflector;
use Rector\BetterReflection\Reflector\ClassReflectorFactory;
use Rector\Tests\AbstractContainerAwareTestCase;
use Roave\BetterReflection\Reflector\ClassReflector;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use SplFileInfo;
final class ClassReflectorOnSourceTest extends AbstractContainerAwareTestCase
{
/**
* @var SplFileInfo
*/
private $currentProcessedFileInfo;
/**
* @var ClassReflector
*/
private $currentFileAwareClassReflector;
protected function setUp(): void
{
$classReflectorFactory = $this->container->get(ClassReflectorFactory::class);
$this->currentProcessedFileInfo = new SplFileInfo(__DIR__ . '/NotLoadedSource/SomeClass.php');
$this->currentFileAwareClassReflector = $classReflectorFactory->createWithFile($this->currentProcessedFileInfo);
}
public function test(): void
{
$this->assertInstanceOf(ClassReflector::class, $this->currentFileAwareClassReflector);
}
public function testReflectClassThatIsNotLoaded(): void
{
$className = 'NotLoadedSource\SomeClass';
// this should fails
// $this->expectException(IdentifierNotFound::class);
$classReflection = $this->currentFileAwareClassReflector->reflect($className);
// this should pass
// $this->assertInstanceOf(ReflectionClass::class, $classReflection);
}
}

View File

@ -0,0 +1,7 @@
<?php declare(strict_types=1);
namespace NotLoadedSource;
final class SomeClass
{
}