From 07e36e3c27072e9eeeae83993f0836bae433a2f2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 24 Aug 2023 22:33:11 +0700 Subject: [PATCH] Revert "use NewOptimizedDirectorySourceLocator" (#4845) * Revert "Remove unused patches/ files as already in vendor-patches directory and use NewOptimizedDirectorySourceLocator (#4844)" This reverts commit a480c51c46e5e71c654b9d70e3cd7663e1844464. * remove patch dir * phpstan notice --- .../Skipper/Skipper/SkipperRectorRuleTest.php | 4 +- .../DynamicSourceLocatorProvider.php | 36 ++++++++++-------- phpstan.neon | 6 +++ src/FileSystem/PhpFilesFinder.php | 37 +++++++++++++++++++ .../DynamicSourceLocatorDecorator.php | 7 +++- 5 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 src/FileSystem/PhpFilesFinder.php diff --git a/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php b/packages-tests/Skipper/Skipper/SkipperRectorRuleTest.php index d641d21864f..7fb95e592a6 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(FileNodesFetcher::class); + $container->make(PhpFilesFinder::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 f6757b79770..26bbd4bc337 100644 --- a/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php +++ b/packages/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php @@ -6,11 +6,13 @@ 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\OptimizedDirectorySourceLocatorFactory; +use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocator; use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator; use Rector\Core\Contract\DependencyInjection\ResetableInterface; use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment; +use Webmozart\Assert\Assert; /** * @api phpstan external @@ -23,15 +25,15 @@ final class DynamicSourceLocatorProvider implements ResetableInterface private array $filePaths = []; /** - * @var string[] + * @var array */ - private array $directories = []; + private array $filesByDirectory = []; private ?AggregateSourceLocator $aggregateSourceLocator = null; public function __construct( private readonly FileNodesFetcher $fileNodesFetcher, - private readonly OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory + private readonly PhpVersion $phpVersion ) { } @@ -48,14 +50,6 @@ 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 @@ -70,8 +64,8 @@ public function provide(): SourceLocator $sourceLocators[] = new OptimizedSingleFileSourceLocator($this->fileNodesFetcher, $file); } - foreach ($this->directories as $directory) { - $sourceLocators[] = $this->optimizedDirectorySourceLocatorFactory->createByDirectory($directory); + foreach ($this->filesByDirectory as $files) { + $sourceLocators[] = new OptimizedDirectorySourceLocator($this->fileNodesFetcher, $this->phpVersion, $files); } $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); @@ -79,9 +73,19 @@ 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->directories === []; + return $this->filePaths === [] && $this->filesByDirectory === []; } /** @@ -90,7 +94,7 @@ public function isPathsEmpty(): bool public function reset(): void { $this->filePaths = []; - $this->directories = []; + $this->filesByDirectory = []; $this->aggregateSourceLocator = null; } } diff --git a/phpstan.neon b/phpstan.neon index f39ec55eb37..89e176ba747 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -606,3 +606,9 @@ 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 new file mode 100644 index 00000000000..26b2b9aad45 --- /dev/null +++ b/src/FileSystem/PhpFilesFinder.php @@ -0,0 +1,37 @@ +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 68d744ce39e..b23ab572bba 100644 --- a/src/StaticReflection/DynamicSourceLocatorDecorator.php +++ b/src/StaticReflection/DynamicSourceLocatorDecorator.php @@ -6,6 +6,7 @@ use Rector\Core\FileSystem\FileAndDirectoryFilter; use Rector\Core\FileSystem\FilesystemTweaker; +use Rector\Core\FileSystem\PhpFilesFinder; use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider; /** @@ -16,6 +17,7 @@ final class DynamicSourceLocatorDecorator { public function __construct( private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, + private readonly PhpFilesFinder $phpFilesFinder, private readonly FileAndDirectoryFilter $fileAndDirectoryFilter, private readonly FilesystemTweaker $filesystemTweaker ) { @@ -36,7 +38,10 @@ public function addPaths(array $paths): void $this->dynamicSourceLocatorProvider->addFiles($files); $directories = $this->fileAndDirectoryFilter->filterDirectories($paths); - $this->dynamicSourceLocatorProvider->addDirectories($directories); + foreach ($directories as $directory) { + $filesInDirectory = $this->phpFilesFinder->findInPaths([$directory]); + $this->dynamicSourceLocatorProvider->addFilesByDirectory($directory, $filesInDirectory); + } } public function isPathsEmpty(): bool