mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
Updated Rector to commit 50407e2f2d1c2eb7f67c1f59e8db777e6f0bd081
50407e2f2d
[Performance] Ensure call $reflector->reflectAllClasses() once on Worker take 3 (#5878)
This commit is contained in:
parent
89f91b63be
commit
efe89cff77
|
@ -4,10 +4,13 @@ declare (strict_types=1);
|
|||
namespace Rector\Application;
|
||||
|
||||
use RectorPrefix202405\Nette\Utils\FileSystem as UtilsFileSystem;
|
||||
use Rector\Caching\Cache;
|
||||
use Rector\Caching\Detector\ChangedFilesDetector;
|
||||
use Rector\Caching\Enum\CacheKey;
|
||||
use Rector\Configuration\Option;
|
||||
use Rector\Configuration\Parameter\SimpleParameterProvider;
|
||||
use Rector\Configuration\VendorMissAnalyseGuard;
|
||||
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
|
||||
use Rector\Parallel\Application\ParallelFileProcessor;
|
||||
use Rector\Provider\CurrentFileProvider;
|
||||
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
||||
|
@ -77,6 +80,16 @@ final class ApplicationFileProcessor
|
|||
* @var \Rector\Configuration\VendorMissAnalyseGuard
|
||||
*/
|
||||
private $vendorMissAnalyseGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider
|
||||
*/
|
||||
private $dynamicSourceLocatorProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Caching\Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@ -85,7 +98,7 @@ final class ApplicationFileProcessor
|
|||
* @var SystemError[]
|
||||
*/
|
||||
private $systemErrors = [];
|
||||
public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory, ParallelFileProcessor $parallelFileProcessor, ScheduleFactory $scheduleFactory, CpuCoreCountProvider $cpuCoreCountProvider, ChangedFilesDetector $changedFilesDetector, CurrentFileProvider $currentFileProvider, \Rector\Application\FileProcessor $fileProcessor, ArrayParametersMerger $arrayParametersMerger, VendorMissAnalyseGuard $vendorMissAnalyseGuard)
|
||||
public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory, ParallelFileProcessor $parallelFileProcessor, ScheduleFactory $scheduleFactory, CpuCoreCountProvider $cpuCoreCountProvider, ChangedFilesDetector $changedFilesDetector, CurrentFileProvider $currentFileProvider, \Rector\Application\FileProcessor $fileProcessor, ArrayParametersMerger $arrayParametersMerger, VendorMissAnalyseGuard $vendorMissAnalyseGuard, DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Cache $cache)
|
||||
{
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->fileFactory = $fileFactory;
|
||||
|
@ -97,6 +110,8 @@ final class ApplicationFileProcessor
|
|||
$this->fileProcessor = $fileProcessor;
|
||||
$this->arrayParametersMerger = $arrayParametersMerger;
|
||||
$this->vendorMissAnalyseGuard = $vendorMissAnalyseGuard;
|
||||
$this->dynamicSourceLocatorProvider = $dynamicSourceLocatorProvider;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
public function run(Configuration $configuration, InputInterface $input) : ProcessResult
|
||||
{
|
||||
|
@ -109,6 +124,9 @@ final class ApplicationFileProcessor
|
|||
if ($filePaths === []) {
|
||||
return new ProcessResult([], []);
|
||||
}
|
||||
// ensure clear classnames collection caches on repetitive call
|
||||
$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->dynamicSourceLocatorProvider->getCacheClassNameKey();
|
||||
$this->cache->clean($key);
|
||||
$this->configureCustomErrorHandler();
|
||||
/**
|
||||
* Mimic @see https://github.com/phpstan/phpstan-src/blob/ab154e1da54d42fec751e17a1199b3e07591e85e/src/Command/AnalyseApplication.php#L188C23-L244
|
||||
|
@ -132,6 +150,8 @@ final class ApplicationFileProcessor
|
|||
} else {
|
||||
$preFileCallback = null;
|
||||
}
|
||||
// trigger cache class names collection
|
||||
$this->dynamicSourceLocatorProvider->provide();
|
||||
if ($configuration->isParallel()) {
|
||||
$processResult = $this->runParallel($filePaths, $configuration, $input, $postFileCallback);
|
||||
} else {
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'a5bf80cb2fdd49d64777cf13250e7205ddd0a3be';
|
||||
public const PACKAGE_VERSION = '50407e2f2d1c2eb7f67c1f59e8db777e6f0bd081';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-05-14 20:46:32';
|
||||
public const RELEASE_DATE = '2024-05-15 02:22:10';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -16,4 +16,8 @@ final class CacheKey
|
|||
* @var string
|
||||
*/
|
||||
public const FILE_HASH_KEY = 'file_hash';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CLASSNAMES_HASH_KEY = 'classnames_hash';
|
||||
}
|
||||
|
|
|
@ -179,6 +179,7 @@ use Rector\StaticTypeMapper\PhpParser\NullableTypeNodeMapper;
|
|||
use Rector\StaticTypeMapper\PhpParser\StringNodeMapper;
|
||||
use Rector\StaticTypeMapper\PhpParser\UnionTypeNodeMapper;
|
||||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
use Rector\Util\FileHasher;
|
||||
use Rector\Utils\Command\MissingInSetCommand;
|
||||
use Rector\Utils\Command\OutsideAnySetCommand;
|
||||
use RectorPrefix202405\Symfony\Component\Console\Application;
|
||||
|
@ -290,7 +291,7 @@ final class LazyContainerFactory
|
|||
return $phpStanServicesFactory->createDynamicSourceLocatorProvider();
|
||||
});
|
||||
$rectorConfig->afterResolving(DynamicSourceLocatorProvider::class, static function (DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Container $container) : void {
|
||||
$dynamicSourceLocatorProvider->autowire($container->make(ReflectionProvider::class));
|
||||
$dynamicSourceLocatorProvider->autowire($container->make(ReflectionProvider::class), $container->make(Cache::class), $container->make(FileHasher::class));
|
||||
});
|
||||
// resetables
|
||||
$rectorConfig->tag(DynamicSourceLocatorProvider::class, ResetableInterface::class);
|
||||
|
|
|
@ -13,8 +13,11 @@ use PHPStan\Reflection\BetterReflection\SourceLocator\NewOptimizedDirectorySourc
|
|||
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
|
||||
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Caching\Cache;
|
||||
use Rector\Caching\Enum\CacheKey;
|
||||
use Rector\Contract\DependencyInjection\ResetableInterface;
|
||||
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
||||
use Rector\Util\FileHasher;
|
||||
/**
|
||||
* @api phpstan external
|
||||
*/
|
||||
|
@ -46,19 +49,41 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
|
|||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
/**
|
||||
* @var \Rector\Caching\Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var \Rector\Util\FileHasher
|
||||
*/
|
||||
private $fileHasher;
|
||||
public function __construct(FileNodesFetcher $fileNodesFetcher, OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory)
|
||||
{
|
||||
$this->fileNodesFetcher = $fileNodesFetcher;
|
||||
$this->optimizedDirectorySourceLocatorFactory = $optimizedDirectorySourceLocatorFactory;
|
||||
}
|
||||
public function autowire(ReflectionProvider $reflectionProvider) : void
|
||||
public function autowire(ReflectionProvider $reflectionProvider, Cache $cache, FileHasher $fileHasher) : void
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->cache = $cache;
|
||||
$this->fileHasher = $fileHasher;
|
||||
}
|
||||
public function setFilePath(string $filePath) : void
|
||||
{
|
||||
$this->filePaths = [$filePath];
|
||||
}
|
||||
public function getCacheClassNameKey() : string
|
||||
{
|
||||
$paths = [];
|
||||
foreach ($this->filePaths as $filePath) {
|
||||
$paths[] = (string) \realpath($filePath);
|
||||
}
|
||||
foreach ($this->directories as $directory) {
|
||||
$paths[] = (string) \realpath($directory);
|
||||
}
|
||||
$paths = \array_filter($paths);
|
||||
return CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->fileHasher->hash((string) \json_encode($paths));
|
||||
}
|
||||
/**
|
||||
* @param string[] $files
|
||||
*/
|
||||
|
@ -104,6 +129,18 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
|
|||
$this->directories = [];
|
||||
$this->aggregateSourceLocator = null;
|
||||
}
|
||||
/**
|
||||
* @param class-string[] $classNamesCache
|
||||
*/
|
||||
private function locateCachedClassNames(array $classNamesCache) : void
|
||||
{
|
||||
foreach ($classNamesCache as $classNameCache) {
|
||||
try {
|
||||
$this->reflectionProvider->getClass($classNameCache);
|
||||
} catch (ClassNotFoundException $exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param OptimizedSingleFileSourceLocator[]|NewOptimizedDirectorySourceLocator[] $sourceLocators
|
||||
*/
|
||||
|
@ -116,18 +153,32 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
|
|||
if (\count($sourceLocators) === 1 && $sourceLocators[0] instanceof OptimizedSingleFileSourceLocator) {
|
||||
return;
|
||||
}
|
||||
$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->getCacheClassNameKey();
|
||||
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
|
||||
if (\is_string($classNamesCache)) {
|
||||
$classNamesCache = \json_decode($classNamesCache);
|
||||
if (\is_array($classNamesCache)) {
|
||||
$this->locateCachedClassNames($classNamesCache);
|
||||
return;
|
||||
}
|
||||
}
|
||||
$reflector = new DefaultReflector($aggregateSourceLocator);
|
||||
$classNames = [];
|
||||
// trigger collect "classes" on get class on locate identifier
|
||||
try {
|
||||
$reflections = $reflector->reflectAllClasses();
|
||||
foreach ($reflections as $reflection) {
|
||||
$className = $reflection->getName();
|
||||
// make 'classes' collection
|
||||
try {
|
||||
$this->reflectionProvider->getClass($reflection->getName());
|
||||
$this->reflectionProvider->getClass($className);
|
||||
} catch (ClassNotFoundException $exception) {
|
||||
continue;
|
||||
}
|
||||
$classNames[] = $className;
|
||||
}
|
||||
} catch (CouldNotReadFileException $exception) {
|
||||
}
|
||||
$this->cache->save($key, CacheKey::CLASSNAMES_HASH_KEY, \json_encode($classNames));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user