Skip to content

Commit

Permalink
Updated Rector to commit 2e36fffb357edc40a64d69266e8747fcf9fdd679
Browse files Browse the repository at this point in the history
rectorphp/rector-src@2e36fff [Bc Break] [Alternative] Drop children class detection (#5995)
  • Loading branch information
TomasVotruba committed Jun 21, 2024
1 parent d5a6bce commit d6dc063
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 213 deletions.
21 changes: 1 addition & 20 deletions src/Application/ApplicationFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
namespace Rector\Application;

use RectorPrefix202406\Nette\Utils\FileSystem as UtilsFileSystem;
use Rector\Caching\Cache;
use Rector\Caching\Detector\ChangedFilesDetector;
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;
Expand Down Expand Up @@ -79,16 +77,6 @@ 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
*/
Expand All @@ -97,7 +85,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, DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Cache $cache)
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)
{
$this->symfonyStyle = $symfonyStyle;
$this->fileFactory = $fileFactory;
Expand All @@ -109,8 +97,6 @@ public function __construct(SymfonyStyle $symfonyStyle, FileFactory $fileFactory
$this->fileProcessor = $fileProcessor;
$this->arrayParametersMerger = $arrayParametersMerger;
$this->vendorMissAnalyseGuard = $vendorMissAnalyseGuard;
$this->dynamicSourceLocatorProvider = $dynamicSourceLocatorProvider;
$this->cache = $cache;
}
public function run(Configuration $configuration, InputInterface $input) : ProcessResult
{
Expand All @@ -121,9 +107,6 @@ public function run(Configuration $configuration, InputInterface $input) : Proce
if ($filePaths === []) {
return new ProcessResult([], []);
}
// ensure clear classnames collection caches on repetitive call
$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
Expand All @@ -147,8 +130,6 @@ public function run(Configuration $configuration, InputInterface $input) : Proce
} else {
$preFileCallback = null;
}
// trigger cache class names collection
$this->dynamicSourceLocatorProvider->provide();
if ($configuration->isParallel()) {
$processResult = $this->runParallel($filePaths, $input, $postFileCallback);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '07dc58043d4a4634e6376094af98a3233719a2da';
public const PACKAGE_VERSION = '2e36fffb357edc40a64d69266e8747fcf9fdd679';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-06-21 02:28:40';
public const RELEASE_DATE = '2024-06-21 09:42:39';
/**
* @var int
*/
Expand Down
4 changes: 0 additions & 4 deletions src/Caching/Enum/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,4 @@ final class CacheKey
* @var string
*/
public const FILE_HASH_KEY = 'file_hash';
/**
* @var string
*/
public const CLASSNAMES_HASH_KEY = 'classnames_hash';
}
4 changes: 0 additions & 4 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@
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 RectorPrefix202406\Symfony\Component\Console\Application;
Expand Down Expand Up @@ -290,9 +289,6 @@ public function create() : RectorConfig
$phpStanServicesFactory = $container->make(PHPStanServicesFactory::class);
return $phpStanServicesFactory->createDynamicSourceLocatorProvider();
});
$rectorConfig->afterResolving(DynamicSourceLocatorProvider::class, static function (DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Container $container) : void {
$dynamicSourceLocatorProvider->autowire($container->make(Cache::class), $container->make(FileHasher::class));
});
// resetables
$rectorConfig->tag(DynamicSourceLocatorProvider::class, ResetableInterface::class);
$rectorConfig->tag(RenamedClassesDataCollector::class, ResetableInterface::class);
Expand Down
68 changes: 1 addition & 67 deletions src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use Rector\Caching\Cache;
use Rector\Caching\Enum\CacheKey;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Util\Reflection\PrivatesAccessor;
final class FamilyRelationsAnalyzer
{
/**
Expand All @@ -27,53 +21,10 @@ final class FamilyRelationsAnalyzer
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\Util\Reflection\PrivatesAccessor
*/
private $privatesAccessor;
/**
* @readonly
* @var \Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider
*/
private $dynamicSourceLocatorProvider;
/**
* @readonly
* @var \Rector\Caching\Cache
*/
private $cache;
/**
* @var bool
*/
private $hasClassNamesCachedOrLoadOneLocator = \false;
public function __construct(ReflectionProvider $reflectionProvider, NodeNameResolver $nodeNameResolver, PrivatesAccessor $privatesAccessor, DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Cache $cache, bool $hasClassNamesCachedOrLoadOneLocator = \false)
public function __construct(ReflectionProvider $reflectionProvider, NodeNameResolver $nodeNameResolver)
{
$this->reflectionProvider = $reflectionProvider;
$this->nodeNameResolver = $nodeNameResolver;
$this->privatesAccessor = $privatesAccessor;
$this->dynamicSourceLocatorProvider = $dynamicSourceLocatorProvider;
$this->cache = $cache;
$this->hasClassNamesCachedOrLoadOneLocator = $hasClassNamesCachedOrLoadOneLocator;
}
/**
* @return ClassReflection[]
*/
public function getChildrenOfClassReflection(ClassReflection $desiredClassReflection) : array
{
if ($desiredClassReflection->isFinalByKeyword()) {
return [];
}
$this->loadClasses();
/** @var ClassReflection[] $classReflections */
$classReflections = $this->privatesAccessor->getPrivateProperty($this->reflectionProvider, 'classes');
$childrenClassReflections = [];
foreach ($classReflections as $classReflection) {
if (!$classReflection->isSubclassOf($desiredClassReflection->getName())) {
continue;
}
$childrenClassReflections[] = $classReflection;
}
return $childrenClassReflections;
}
/**
* @api
Expand Down Expand Up @@ -113,21 +64,4 @@ public function getClassLikeAncestorNames($classOrName) : array
/** @var string[] $ancestorNames */
return $ancestorNames;
}
private function loadClasses() : void
{
if ($this->hasClassNamesCachedOrLoadOneLocator) {
return;
}
$key = $this->dynamicSourceLocatorProvider->getCacheClassNameKey();
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
if (\is_array($classNamesCache)) {
foreach ($classNamesCache as $classNameCache) {
try {
$this->reflectionProvider->getClass($classNameCache);
} catch (ClassNotFoundException|ShouldNotHappenException $exception) {
}
}
}
$this->hasClassNamesCachedOrLoadOneLocator = \true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
declare (strict_types=1);
namespace Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider;

use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\NewOptimizedDirectorySourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
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
*/
Expand Down Expand Up @@ -43,40 +37,15 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
* @var \PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator|null
*/
private $aggregateSourceLocator;
/**
* @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(Cache $cache, FileHasher $fileHasher) : void
{
$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
*/
Expand Down Expand Up @@ -105,9 +74,7 @@ public function provide() : SourceLocator
foreach ($this->directories as $directory) {
$sourceLocators[] = $this->optimizedDirectorySourceLocatorFactory->createByDirectory($directory);
}
$aggregateSourceLocator = $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators);
$this->collectClasses($aggregateSourceLocator, $sourceLocators);
return $aggregateSourceLocator;
return $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators);
}
public function isPathsEmpty() : bool
{
Expand All @@ -122,33 +89,4 @@ public function reset() : void
$this->directories = [];
$this->aggregateSourceLocator = null;
}
/**
* @param OptimizedSingleFileSourceLocator[]|NewOptimizedDirectorySourceLocator[] $sourceLocators
*/
private function collectClasses(AggregateSourceLocator $aggregateSourceLocator, array $sourceLocators) : void
{
if ($sourceLocators === []) {
return;
}
// no need to collect classes on single file, will auto collected
if (\count($sourceLocators) === 1 && $sourceLocators[0] instanceof OptimizedSingleFileSourceLocator) {
return;
}
$key = $this->getCacheClassNameKey();
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
if (\is_array($classNamesCache)) {
return;
}
$reflector = new DefaultReflector($aggregateSourceLocator);
$classNames = [];
// trigger collect "classes" on get class on locate identifier
try {
$reflections = $reflector->reflectAllClasses();
foreach ($reflections as $reflection) {
$classNames[] = $reflection->getName();
}
} catch (CouldNotReadFileException $exception) {
}
$this->cache->save($key, CacheKey::CLASSNAMES_HASH_KEY, $classNames);
}
}
Loading

0 comments on commit d6dc063

Please sign in to comment.