From 3c34942dc98015298c9aa3fdb0369190937021d8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 24 Aug 2023 23:35:22 +0700 Subject: [PATCH] Revert "Revert "use NewOptimizedDirectorySourceLocator"" (#4846) * Revert "Revert "use NewOptimizedDirectorySourceLocator" (#4845)" This reverts commit 07e36e3c27072e9eeeae83993f0836bae433a2f2. * wrap Expression * Revert "wrap Expression" This reverts commit f54fa719c1c3cc964e6aab95ecf07c75e938849d. --- .../Skipper/Skipper/SkipperRectorRuleTest.php | 4 +- .../DynamicSourceLocatorProvider.php | 36 ++++++++---------- phpstan.neon | 6 --- src/FileSystem/PhpFilesFinder.php | 37 ------------------- .../DynamicSourceLocatorDecorator.php | 7 +--- 5 files changed, 19 insertions(+), 71 deletions(-) delete mode 100644 src/FileSystem/PhpFilesFinder.php diff --git a/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php b/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php index 7fb95e592a6..d641d21864f 100644 --- a/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php +++ b/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php @@ -4,11 +4,11 @@ namespace Rector\Tests\Skipper\Skipper; +use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher; use Illuminate\Container\RewindableGenerator; use Rector\Core\Configuration\Option; use Rector\Core\Configuration\Parameter\SimpleParameterProvider; use Rector\Core\Contract\Rector\RectorInterface; -use Rector\Core\FileSystem\PhpFilesFinder; use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector; use Rector\Testing\PHPUnit\AbstractLazyTestCase; @@ -34,7 +34,7 @@ public function testRemovingServiceFromContainer(): void $container = self::getContainer(); // to invoke before resolving - $container->make(PhpFilesFinder::class); + $container->make(FileNodesFetcher::class); // here 1 rule should be removed and 1 should remain /** @var RewindableGenerator $rectorsIterator */ diff --git a/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php b/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php index 26bbd4bc337..f6757b79770 100644 --- a/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php +++ b/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php @@ -6,13 +6,11 @@ use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator; use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; -use PHPStan\Php\PhpVersion; use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher; -use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator; +use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator; use Rector\Core\Contract\DependencyInjection\ResetableInterface; use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment; -use Webmozart\Assert\Assert; /** * @api phpstan external @@ -25,15 +23,15 @@ final class DynamicSourceLocatorProvider implements ResetableInterface private array $filePaths = []; /** - * @var array + * @var string[] */ - private array $filesByDirectory = []; + private array $directories = []; private ?AggregateSourceLocator $aggregateSourceLocator = null; public function __construct( private readonly FileNodesFetcher $fileNodesFetcher, - private readonly PhpVersion $phpVersion + private readonly OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory ) { } @@ -50,6 +48,14 @@ public function addFiles(array $files): void $this->filePaths = array_merge($this->filePaths, $files); } + /** + * @param string[] $directories + */ + public function addDirectories(array $directories): void + { + $this->directories = array_merge($this->directories, $directories); + } + public function provide(): SourceLocator { // do not cache for PHPUnit, as in test every fixture is different @@ -64,8 +70,8 @@ public function provide(): SourceLocator $sourceLocators[] = new OptimizedSingleFileSourceLocator($this->fileNodesFetcher, $file); } - foreach ($this->filesByDirectory as $files) { - $sourceLocators[] = new OptimizedDirectorySourceLocator($this->fileNodesFetcher, $this->phpVersion, $files); + foreach ($this->directories as $directory) { + $sourceLocators[] = $this->optimizedDirectorySourceLocatorFactory->createByDirectory($directory); } $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); @@ -73,19 +79,9 @@ public function provide(): SourceLocator return $this->aggregateSourceLocator; } - /** - * @param string[] $files - */ - public function addFilesByDirectory(string $directory, array $files): void - { - Assert::allString($files); - - $this->filesByDirectory[$directory] = $files; - } - public function isPathsEmpty(): bool { - return $this->filePaths === [] && $this->filesByDirectory === []; + return $this->filePaths === [] && $this->directories === []; } /** @@ -94,7 +90,7 @@ public function isPathsEmpty(): bool public function reset(): void { $this->filePaths = []; - $this->filesByDirectory = []; + $this->directories = []; $this->aggregateSourceLocator = null; } } diff --git a/phpstan.neon b/phpstan.neon index 89e176ba747..f39ec55eb37 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -606,9 +606,3 @@ parameters: - message: '#Function "(class_exists|interface_exists)\(\)" cannot be used/left in the code\: use ReflectionProvider\->has\*\(\) instead#' path: packages/Skipper/SkipCriteriaResolver/SkippedClassResolver.php - - # todo: to be updated to use NewOptimizedDirectorySourceLocator later - # currently changing to NewOptimizedDirectorySourceLocator cause error, @see https://github.com/rectorphp/rector-src/actions/runs/5965685296/job/16183665877#step:10:19 - - - message: '#Instantiation of deprecated class PHPStan\\Reflection\\BetterReflection\\SourceLocator\\OptimizedDirectorySourceLocator#' - path: packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php diff --git a/src/FileSystem/PhpFilesFinder.php b/src/FileSystem/PhpFilesFinder.php deleted file mode 100644 index 26b2b9aad45..00000000000 --- a/src/FileSystem/PhpFilesFinder.php +++ /dev/null @@ -1,37 +0,0 @@ -filesFinder->findInDirectoriesAndFiles($paths, ['php'], false); - - // filter out non-PHP files - foreach ($filePaths as $key => $filePath) { - /** - * check .blade.php early so next .php check in next if can be skipped - */ - if (str_ends_with($filePath, '.blade.php')) { - unset($filePaths[$key]); - } - } - - return $this->unchangedFilesFilter->filterFileInfos($filePaths); - } -} diff --git a/src/StaticReflection/DynamicSourceLocatorDecorator.php b/src/StaticReflection/DynamicSourceLocatorDecorator.php index b23ab572bba..68d744ce39e 100644 --- a/src/StaticReflection/DynamicSourceLocatorDecorator.php +++ b/src/StaticReflection/DynamicSourceLocatorDecorator.php @@ -6,7 +6,6 @@ use Rector\Core\FileSystem\FileAndDirectoryFilter; use Rector\Core\FileSystem\FilesystemTweaker; -use Rector\Core\FileSystem\PhpFilesFinder; use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider; /** @@ -17,7 +16,6 @@ final class DynamicSourceLocatorDecorator { public function __construct( private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, - private readonly PhpFilesFinder $phpFilesFinder, private readonly FileAndDirectoryFilter $fileAndDirectoryFilter, private readonly FilesystemTweaker $filesystemTweaker ) { @@ -38,10 +36,7 @@ public function addPaths(array $paths): void $this->dynamicSourceLocatorProvider->addFiles($files); $directories = $this->fileAndDirectoryFilter->filterDirectories($paths); - foreach ($directories as $directory) { - $filesInDirectory = $this->phpFilesFinder->findInPaths([$directory]); - $this->dynamicSourceLocatorProvider->addFilesByDirectory($directory, $filesInDirectory); - } + $this->dynamicSourceLocatorProvider->addDirectories($directories); } public function isPathsEmpty(): bool