2021-02-28 07:47:48 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-02-28 07:47:48 +00:00
|
|
|
namespace Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider;
|
|
|
|
|
|
|
|
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
|
|
|
|
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
|
2021-10-27 19:25:02 +00:00
|
|
|
use PHPStan\Php\PhpVersion;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
|
2021-03-02 23:42:53 +00:00
|
|
|
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator;
|
2021-02-28 07:47:48 +00:00
|
|
|
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
|
|
|
|
use Rector\NodeTypeResolver\Contract\SourceLocatorProviderInterface;
|
2021-03-18 10:29:23 +00:00
|
|
|
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
2021-02-28 07:47:48 +00:00
|
|
|
use Symplify\SmartFileSystem\SmartFileInfo;
|
2021-05-10 22:23:08 +00:00
|
|
|
final class DynamicSourceLocatorProvider implements \Rector\NodeTypeResolver\Contract\SourceLocatorProviderInterface
|
2021-02-28 07:47:48 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-03-02 23:42:53 +00:00
|
|
|
* @var string[]
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
2021-03-02 23:42:53 +00:00
|
|
|
private $files = [];
|
|
|
|
/**
|
|
|
|
* @var array<string, string[]>
|
|
|
|
*/
|
|
|
|
private $filesByDirectory = [];
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2021-10-24 20:38:54 +00:00
|
|
|
* @var \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator|null
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
2021-10-24 20:38:54 +00:00
|
|
|
private $aggregateSourceLocator;
|
2021-03-18 10:29:23 +00:00
|
|
|
/**
|
2021-10-24 20:38:54 +00:00
|
|
|
* @var \PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher
|
2021-03-18 10:29:23 +00:00
|
|
|
*/
|
2021-10-24 20:38:54 +00:00
|
|
|
private $fileNodesFetcher;
|
2021-10-27 19:25:02 +00:00
|
|
|
/**
|
|
|
|
* @var \PHPStan\Php\PhpVersion
|
|
|
|
*/
|
|
|
|
private $phpVersion;
|
|
|
|
public function __construct(\PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher $fileNodesFetcher, \PHPStan\Php\PhpVersion $phpVersion)
|
2021-02-28 07:47:48 +00:00
|
|
|
{
|
|
|
|
$this->fileNodesFetcher = $fileNodesFetcher;
|
2021-10-27 19:25:02 +00:00
|
|
|
$this->phpVersion = $phpVersion;
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2021-07-05 22:50:18 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \Symplify\SmartFileSystem\SmartFileInfo $fileInfo
|
2021-07-05 22:50:18 +00:00
|
|
|
*/
|
|
|
|
public function setFileInfo($fileInfo) : void
|
2021-02-28 07:47:48 +00:00
|
|
|
{
|
2021-03-02 23:42:53 +00:00
|
|
|
$this->files = [$fileInfo->getRealPath()];
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-03-02 23:42:53 +00:00
|
|
|
* @param string[] $files
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function addFiles($files) : void
|
2021-02-28 07:47:48 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$this->files = \array_merge($this->files, $files);
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function provide() : \PHPStan\BetterReflection\SourceLocator\Type\SourceLocator
|
2021-02-28 07:47:48 +00:00
|
|
|
{
|
2021-03-18 10:29:23 +00:00
|
|
|
// do not cache for PHPUnit, as in test every fixture is different
|
2021-05-10 22:23:08 +00:00
|
|
|
$isPHPUnitRun = \Rector\Testing\PHPUnit\StaticPHPUnitEnvironment::isPHPUnitRun();
|
2021-11-28 18:05:13 +00:00
|
|
|
if ($this->aggregateSourceLocator instanceof \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator && !$isPHPUnitRun) {
|
2021-10-24 20:38:54 +00:00
|
|
|
return $this->aggregateSourceLocator;
|
2021-03-18 10:29:23 +00:00
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
$sourceLocators = [];
|
2021-03-02 23:42:53 +00:00
|
|
|
foreach ($this->files as $file) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$sourceLocators[] = new \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator($this->fileNodesFetcher, $file);
|
2021-03-02 23:42:53 +00:00
|
|
|
}
|
|
|
|
foreach ($this->filesByDirectory as $files) {
|
2021-10-27 19:25:02 +00:00
|
|
|
$sourceLocators[] = new \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator($this->fileNodesFetcher, $this->phpVersion, $files);
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2021-10-24 20:38:54 +00:00
|
|
|
$this->aggregateSourceLocator = new \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator($sourceLocators);
|
|
|
|
return $this->aggregateSourceLocator;
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2021-03-02 23:42:53 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $files
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param string $directory
|
2021-03-02 23:42:53 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function addFilesByDirectory($directory, $files) : void
|
2021-03-02 23:42:53 +00:00
|
|
|
{
|
|
|
|
$this->filesByDirectory[$directory] = $files;
|
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|