diff --git a/src/Application/RectorRegistry.php b/src/Application/RectorRegistry.php new file mode 100644 index 00000000000..4e2679f3b57 --- /dev/null +++ b/src/Application/RectorRegistry.php @@ -0,0 +1,53 @@ +rectors = $rectors; + } + + /** + * @return RectorInterface[] + * + * @api used in tests + */ + public function forPath(string $filePath): array + { + $rectorsForPath = []; + foreach ($this->rectors as $rector) { + if ($this->skipper->shouldSkipRectorAndFile($rector, $filePath)) { + continue; + } + + $rectorsForPath[] = $rector; + } + + return $rectorsForPath; + } +} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index c31bb18f62d..1a06549745f 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -16,6 +16,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\Application\FileProcessor; use Rector\Application\Provider\CurrentFileProvider; +use Rector\Application\RectorRegistry; use Rector\BetterPhpDocParser\Comment\CommentsMerger; use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface; use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ArrayTypePhpDocNodeVisitor; @@ -168,6 +169,7 @@ private function registerFileProcessing(RectorConfig $rectorConfig): void { $rectorConfig->singleton(FileProcessor::class); $rectorConfig->singleton(PostFileProcessor::class); + $rectorConfig->singleton(RectorRegistry::class); // shared state: collects used skips across the skipper, the path skipper and the file processor $rectorConfig->singleton(UsedSkipCollector::class); diff --git a/tests/Application/RectorRegistryTest.php b/tests/Application/RectorRegistryTest.php new file mode 100644 index 00000000000..f20fbd0acff --- /dev/null +++ b/tests/Application/RectorRegistryTest.php @@ -0,0 +1,48 @@ + ['*/skipped_directory/*'], + ]); + + $this->rectorRegistry = $this->make(RectorRegistry::class); + $this->rectorRegistry->refreshRectors([new SkippableRector()]); + } + + protected function tearDown(): void + { + SimpleParameterProvider::setParameter(Option::SKIP, []); + } + + public function testForPathSkipsRectorForSkippedPath(): void + { + $rectorsForPath = $this->rectorRegistry->forPath(__DIR__ . '/skipped_directory/some_file.php'); + + $this->assertSame([], $rectorsForPath); + } + + public function testForPathKeepsRectorForOtherPath(): void + { + $rectorsForPath = $this->rectorRegistry->forPath(__DIR__ . '/some_file.php'); + + $this->assertCount(1, $rectorsForPath); + $this->assertInstanceOf(SkippableRector::class, $rectorsForPath[0]); + } +} diff --git a/tests/Application/Source/SkippableRector.php b/tests/Application/Source/SkippableRector.php new file mode 100644 index 00000000000..d7a63644f10 --- /dev/null +++ b/tests/Application/Source/SkippableRector.php @@ -0,0 +1,27 @@ +