From 9881065a2c6432db628356b8c51f3469f4315b51 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 16 Nov 2026 22:02:38 +0100 Subject: [PATCH 1/2] [dx] Send skipped file paths from parallel workers, so unnecessary skips are reported by default --- src/Application/ApplicationFileProcessor.php | 8 ++- src/Console/Command/ProcessCommand.php | 2 +- src/Console/Command/WorkerCommand.php | 1 + .../Application/ParallelResultCollector.php | 31 +++++++++- src/Parallel/ValueObject/Bridge.php | 2 + src/Reporting/MissConfigurationReporter.php | 4 +- src/Reporting/NeededSkipVerifier.php | 8 ++- src/ValueObject/ProcessResult.php | 19 +++++- .../ParallelResultCollectorTest.php | 59 +++++++++++++++++++ tests/Reporting/NeededSkipVerifierTest.php | 32 +++++++++- 10 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 tests/Parallel/Application/ParallelResultCollectorTest.php diff --git a/src/Application/ApplicationFileProcessor.php b/src/Application/ApplicationFileProcessor.php index b14914ff269..9467568f523 100644 --- a/src/Application/ApplicationFileProcessor.php +++ b/src/Application/ApplicationFileProcessor.php @@ -181,7 +181,13 @@ public function processFiles( } } - return new ProcessResult($systemErrors, $fileDiffs, $totalChanged, $this->skipper->provideUsedSkips()); + return new ProcessResult( + $systemErrors, + $fileDiffs, + $totalChanged, + $this->skipper->provideUsedSkips(), + $this->skipper->provideSkippedFilePathsByRule() + ); } private function processFile(File $file, Configuration $configuration): FileProcessResult diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 851825921d6..00a5c1b52f6 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -193,7 +193,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->missConfigurationReporter->reportUnusedSkips($processResult); // the verification re-run is forced to a dry run, so it is safe on an apply run too - $this->missConfigurationReporter->reportUnnecessarySkips(); + $this->missConfigurationReporter->reportUnnecessarySkips($processResult); return $this->resolveReturnCode($processResult, $configuration); } diff --git a/src/Console/Command/WorkerCommand.php b/src/Console/Command/WorkerCommand.php index 8b7d4c1061c..cf5cf822fea 100644 --- a/src/Console/Command/WorkerCommand.php +++ b/src/Console/Command/WorkerCommand.php @@ -168,6 +168,7 @@ private function runWorker( Bridge::SYSTEM_ERRORS_COUNT => count($processResult->getSystemErrors()), Bridge::TOTAL_CHANGED => $processResult->getTotalChanged(), Bridge::USED_SKIPS => $processResult->getUsedSkips(), + Bridge::SKIPPED_FILE_PATHS_BY_RULE => $processResult->getSkippedFilePathsByRule(), ], ]); }); diff --git a/src/Parallel/Application/ParallelResultCollector.php b/src/Parallel/Application/ParallelResultCollector.php index 8598d534879..5a583efdf34 100644 --- a/src/Parallel/Application/ParallelResultCollector.php +++ b/src/Parallel/Application/ParallelResultCollector.php @@ -14,6 +14,7 @@ * who gets which files. * * Holds mutable per-run state - create one per process() call, never share it. + * @see \Rector\Tests\Parallel\Application\ParallelResultCollectorTest */ final class ParallelResultCollector { @@ -34,6 +35,13 @@ final class ParallelResultCollector */ private array $usedSkips = []; + /** + * Paths are kept as keys while collecting, to deduplicate them per rule + * + * @var array> + */ + private array $skippedFilePathsByRule = []; + private int $totalChanged = 0; /** @@ -43,7 +51,8 @@ final class ParallelResultCollector * file_diffs: array, * files_count: int, * system_errors_count: int, - * used_skips: array + * used_skips: array, + * skipped_file_paths_by_rule: array * } $json * @return int files covered by this result, to advance the progress bar */ @@ -58,6 +67,13 @@ public function collectWorkerResult(array $json): int } } + foreach ($json[Bridge::SKIPPED_FILE_PATHS_BY_RULE] as $rectorClass => $filePaths) { + $this->skippedFilePathsByRule[$rectorClass] ??= []; + foreach ($filePaths as $filePath) { + $this->skippedFilePathsByRule[$rectorClass][$filePath] = true; + } + } + // decode arrays to objects foreach ($json[Bridge::SYSTEM_ERRORS] as $jsonError) { if (is_string($jsonError)) { @@ -87,6 +103,17 @@ public function createProcessResult(): ProcessResult $mergedUsedSkips[$skip] = array_keys($paths); } - return new ProcessResult($this->systemErrors, $this->fileDiffs, $this->totalChanged, $mergedUsedSkips); + $mergedSkippedFilePathsByRule = []; + foreach ($this->skippedFilePathsByRule as $rectorClass => $filePaths) { + $mergedSkippedFilePathsByRule[$rectorClass] = array_keys($filePaths); + } + + return new ProcessResult( + $this->systemErrors, + $this->fileDiffs, + $this->totalChanged, + $mergedUsedSkips, + $mergedSkippedFilePathsByRule + ); } } diff --git a/src/Parallel/ValueObject/Bridge.php b/src/Parallel/ValueObject/Bridge.php index 55bda4fba95..8edc01d5165 100644 --- a/src/Parallel/ValueObject/Bridge.php +++ b/src/Parallel/ValueObject/Bridge.php @@ -22,4 +22,6 @@ final class Bridge public const string TOTAL_CHANGED = 'total_changed'; public const string USED_SKIPS = 'used_skips'; + + public const string SKIPPED_FILE_PATHS_BY_RULE = 'skipped_file_paths_by_rule'; } diff --git a/src/Reporting/MissConfigurationReporter.php b/src/Reporting/MissConfigurationReporter.php index 7fd09c02cd3..a4cb810438c 100644 --- a/src/Reporting/MissConfigurationReporter.php +++ b/src/Reporting/MissConfigurationReporter.php @@ -53,9 +53,9 @@ public function reportUnusedSkips(ProcessResult $processResult): void * skip is unnecessary. Enabled with "->reportUnusedSkips()". The re-run is forced to a dry run, * so it is safe on an apply run too. */ - public function reportUnnecessarySkips(): void + public function reportUnnecessarySkips(ProcessResult $processResult): void { - $unnecessarySkips = $this->neededSkipVerifier->verify(); + $unnecessarySkips = $this->neededSkipVerifier->verify($processResult); if ($unnecessarySkips === []) { return; } diff --git a/src/Reporting/NeededSkipVerifier.php b/src/Reporting/NeededSkipVerifier.php index 0234c291faf..976c1cfa011 100644 --- a/src/Reporting/NeededSkipVerifier.php +++ b/src/Reporting/NeededSkipVerifier.php @@ -11,6 +11,7 @@ use Rector\Skipper\Skipper\Skipper; use Rector\ValueObject\Application\File; use Rector\ValueObject\Configuration; +use Rector\ValueObject\ProcessResult; /** * Re-runs each rule-scoped skip on its exact file with skipping off. A skip whose rule still does @@ -34,9 +35,10 @@ public function __construct( * * @return string[] */ - public function verify(): array + public function verify(ProcessResult $processResult): array { - $skippedFilePathsByRule = $this->skipper->provideSkippedFilePathsByRule(); + // collected in the process that handled the files - the workers in a parallel run + $skippedFilePathsByRule = $processResult->getSkippedFilePathsByRule(); if ($skippedFilePathsByRule === []) { return []; } @@ -72,7 +74,7 @@ public function verify(): array } /** - * @param array $skippedFilePathsByRule + * @param array $skippedFilePathsByRule * @return array real file path => rule classes that changed it */ private function resolveChangedRulesByFilePath(array $skippedFilePathsByRule): array diff --git a/src/ValueObject/ProcessResult.php b/src/ValueObject/ProcessResult.php index d15bc4ce559..c27d02fd62c 100644 --- a/src/ValueObject/ProcessResult.php +++ b/src/ValueObject/ProcessResult.php @@ -15,12 +15,14 @@ final class ProcessResult * @param SystemError[] $systemErrors * @param FileDiff[] $fileDiffs * @param array $usedSkips + * @param array $skippedFilePathsByRule */ public function __construct( private array $systemErrors, private readonly array $fileDiffs, private readonly int $totalChanged, - private array $usedSkips = [] + private array $usedSkips = [], + private readonly array $skippedFilePathsByRule = [] ) { Assert::allIsInstanceOf($systemErrors, SystemError::class); Assert::allIsInstanceOf($fileDiffs, FileDiff::class); @@ -29,6 +31,11 @@ public function __construct( foreach ($usedSkips as $usedSkip) { Assert::allString($usedSkip); } + + Assert::allString(array_keys($skippedFilePathsByRule)); + foreach ($skippedFilePathsByRule as $skippedFilePaths) { + Assert::allString($skippedFilePaths); + } } /** @@ -91,6 +98,16 @@ public function getUsedSkips(): array return $this->usedSkips; } + /** + * Real file paths skipped per rule-scoped path skip, to verify later whether the skip is needed + * + * @return array + */ + public function getSkippedFilePathsByRule(): array + { + return $this->skippedFilePathsByRule; + } + /** * @return array, int> */ diff --git a/tests/Parallel/Application/ParallelResultCollectorTest.php b/tests/Parallel/Application/ParallelResultCollectorTest.php new file mode 100644 index 00000000000..c63423f3355 --- /dev/null +++ b/tests/Parallel/Application/ParallelResultCollectorTest.php @@ -0,0 +1,59 @@ +collectWorkerResult($this->createWorkerResult([ + 'SomeRector' => ['/src/first.php', '/src/second.php'], + ])); + + // a worker reports its whole cumulative state, so the same path can arrive twice + $parallelResultCollector->collectWorkerResult($this->createWorkerResult([ + 'SomeRector' => ['/src/second.php', '/src/third.php'], + 'AnotherRector' => ['/src/first.php'], + ])); + + $processResult = $parallelResultCollector->createProcessResult(); + + $this->assertSame([ + 'SomeRector' => ['/src/first.php', '/src/second.php', '/src/third.php'], + 'AnotherRector' => ['/src/first.php'], + ], $processResult->getSkippedFilePathsByRule()); + } + + /** + * @param array $skippedFilePathsByRule + * @return array{ + * total_changed: int, + * system_errors: mixed[], + * file_diffs: array, + * files_count: int, + * system_errors_count: int, + * used_skips: array, + * skipped_file_paths_by_rule: array + * } + */ + private function createWorkerResult(array $skippedFilePathsByRule): array + { + return [ + Bridge::TOTAL_CHANGED => 0, + Bridge::SYSTEM_ERRORS => [], + Bridge::FILE_DIFFS => [], + Bridge::FILES_COUNT => 0, + Bridge::SYSTEM_ERRORS_COUNT => 0, + Bridge::USED_SKIPS => [], + Bridge::SKIPPED_FILE_PATHS_BY_RULE => $skippedFilePathsByRule, + ]; + } +} diff --git a/tests/Reporting/NeededSkipVerifierTest.php b/tests/Reporting/NeededSkipVerifierTest.php index 6649800135b..f7bf1fb71fd 100644 --- a/tests/Reporting/NeededSkipVerifierTest.php +++ b/tests/Reporting/NeededSkipVerifierTest.php @@ -13,6 +13,7 @@ use Rector\Skipper\Skipper\Skipper; use Rector\Testing\PHPUnit\AbstractLazyTestCase; use Rector\Tests\Reporting\Source\MakeClassFinalRector; +use Rector\ValueObject\ProcessResult; final class NeededSkipVerifierTest extends AbstractLazyTestCase { @@ -56,8 +57,10 @@ public function testReportsSkipThatRuleWouldNotHaveChanged(): void $this->assertTrue($skipper->shouldSkipRectorAndFile($rule, $neededFilePath)); $this->assertTrue($skipper->shouldSkipRectorAndFile($rule, $unnecessaryFilePath)); + $processResult = new ProcessResult([], [], 0, [], $skipper->provideSkippedFilePathsByRule()); + $neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class); - $unnecessarySkips = $neededSkipVerifier->verify(); + $unnecessarySkips = $neededSkipVerifier->verify($processResult); // one rule group, listing only the unnecessary file $this->assertCount(1, $unnecessarySkips); @@ -69,12 +72,37 @@ public function testReportsSkipThatRuleWouldNotHaveChanged(): void $this->assertSame('tempDirectory . '/unnecessary_skip.php'; + FileSystem::write($unnecessaryFilePath, ' [$unnecessaryFilePath], + ]); + + $rectorConfig = $this->createContainerWithRule(); + + // in a parallel run, the skipped files are captured by workers and only reach the main process via the result + $processResult = new ProcessResult([], [], 0, [], [ + MakeClassFinalRector::class => [$unnecessaryFilePath], + ]); + + $neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class); + $unnecessarySkips = $neededSkipVerifier->verify($processResult); + + $this->assertCount(1, $unnecessarySkips); + $this->assertStringContainsString('unnecessary_skip.php', $unnecessarySkips[0]); + } + public function testReportsNothingWithoutCapturedSkips(): void { $rectorConfig = $this->createContainerWithRule(); $neededSkipVerifier = $rectorConfig->make(NeededSkipVerifier::class); - $this->assertSame([], $neededSkipVerifier->verify()); + $this->assertSame([], $neededSkipVerifier->verify(new ProcessResult([], [], 0))); } private function createContainerWithRule(): RectorConfig From 210fe1c1dc0bb65d41f68b64295f8244db678740 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 16 Nov 2026 22:04:52 +0100 Subject: [PATCH 2/2] Add skipped file paths to worker result shape --- src/Parallel/Application/ParallelFileProcessor.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Parallel/Application/ParallelFileProcessor.php b/src/Parallel/Application/ParallelFileProcessor.php index ddb7d1fe232..1758b6df3c3 100644 --- a/src/Parallel/Application/ParallelFileProcessor.php +++ b/src/Parallel/Application/ParallelFileProcessor.php @@ -202,7 +202,8 @@ function (array $json) use ( * file_diffs: array, * files_count: int, * system_errors_count: int, - * used_skips: array + * used_skips: array, + * skipped_file_paths_by_rule: array * } $json */ $filesCount = $parallelResultCollector->collectWorkerResult($json); $postFileCallback($filesCount);