|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Reporting; |
| 6 | + |
| 7 | +use Nette\Utils\FileSystem; |
| 8 | +use Rector\Application\FileProcessor; |
| 9 | +use Rector\Application\Provider\CurrentFileProvider; |
| 10 | +use Rector\FileSystem\FilePathHelper; |
| 11 | +use Rector\Skipper\Skipper\Skipper; |
| 12 | +use Rector\ValueObject\Application\File; |
| 13 | +use Rector\ValueObject\Configuration; |
| 14 | + |
| 15 | +/** |
| 16 | + * Re-runs each rule-scoped skip on its exact file with skipping off. A skip whose rule still does |
| 17 | + * not change the file never prevented anything and can be removed. The re-processing is forced to a |
| 18 | + * dry run, so it never writes, no matter the mode of the real run. |
| 19 | + * |
| 20 | + * @see \Rector\Tests\Reporting\NeededSkipVerifierTest |
| 21 | + */ |
| 22 | +final readonly class NeededSkipVerifier |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private Skipper $skipper, |
| 26 | + private FileProcessor $fileProcessor, |
| 27 | + private CurrentFileProvider $currentFileProvider, |
| 28 | + private FilePathHelper $filePathHelper, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Unnecessary skips grouped under their rule, ready for "->listing()". |
| 34 | + * |
| 35 | + * @return string[] |
| 36 | + */ |
| 37 | + public function verify(): array |
| 38 | + { |
| 39 | + $skippedFilePathsByRule = $this->skipper->provideSkippedFilePathsByRule(); |
| 40 | + if ($skippedFilePathsByRule === []) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + |
| 44 | + $changedRulesByFilePath = $this->resolveChangedRulesByFilePath($skippedFilePathsByRule); |
| 45 | + |
| 46 | + $unnecessarySkips = []; |
| 47 | + foreach ($skippedFilePathsByRule as $rectorClass => $filePaths) { |
| 48 | + $unnecessaryRelativePaths = []; |
| 49 | + foreach ($filePaths as $filePath) { |
| 50 | + // file could not be re-processed (e.g. parse error), keep the skip to stay safe |
| 51 | + if (! array_key_exists($filePath, $changedRulesByFilePath)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + // the rule changed the file, so the skip really prevents a change - keep it |
| 56 | + if (in_array($rectorClass, $changedRulesByFilePath[$filePath], true)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $unnecessaryRelativePaths[] = $this->filePathHelper->relativePath($filePath); |
| 61 | + } |
| 62 | + |
| 63 | + if ($unnecessaryRelativePaths === []) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // rule on its own line, with each unnecessary path nested below it as a sub-item |
| 68 | + $unnecessarySkips[] = $rectorClass . ':' . "\n * " . implode("\n * ", $unnecessaryRelativePaths); |
| 69 | + } |
| 70 | + |
| 71 | + return $unnecessarySkips; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param array<class-string, string[]> $skippedFilePathsByRule |
| 76 | + * @return array<string, string[]> real file path => rule classes that changed it |
| 77 | + */ |
| 78 | + private function resolveChangedRulesByFilePath(array $skippedFilePathsByRule): array |
| 79 | + { |
| 80 | + $uniqueFilePaths = []; |
| 81 | + foreach ($skippedFilePathsByRule as $filePaths) { |
| 82 | + foreach ($filePaths as $filePath) { |
| 83 | + $uniqueFilePaths[$filePath] = true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // force a dry run, so re-processing detects the change but never writes the skipped file |
| 88 | + $dryRunConfiguration = new Configuration(isDryRun: true); |
| 89 | + |
| 90 | + $this->skipper->startVerifyingSkips(); |
| 91 | + |
| 92 | + $changedRulesByFilePath = []; |
| 93 | + try { |
| 94 | + foreach (array_keys($uniqueFilePaths) as $filePath) { |
| 95 | + $file = new File($filePath, FileSystem::read($filePath)); |
| 96 | + $this->currentFileProvider->setFile($file); |
| 97 | + |
| 98 | + $fileProcessResult = $this->fileProcessor->processFile($file, $dryRunConfiguration); |
| 99 | + |
| 100 | + // a broken file is not verifiable, leave it out so its skips are kept |
| 101 | + if ($fileProcessResult->getSystemErrors() !== []) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + $fileDiff = $fileProcessResult->getFileDiff(); |
| 106 | + $changedRulesByFilePath[$filePath] = $fileDiff?->getRectorClasses() ?? []; |
| 107 | + } |
| 108 | + } finally { |
| 109 | + $this->skipper->stopVerifyingSkips(); |
| 110 | + } |
| 111 | + |
| 112 | + return $changedRulesByFilePath; |
| 113 | + } |
| 114 | +} |
0 commit comments