Skip to content

Commit df98b3b

Browse files
authored
[skip] display skips only on uncached run (#8071)
* [skip] display skips only on uncached run * newline
1 parent 84ab911 commit df98b3b

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/Caching/UnchangedFilesFilter.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Rector\Caching;
66

77
use Rector\Caching\Detector\ChangedFilesDetector;
8+
use Rector\Configuration\Option;
9+
use Rector\Configuration\Parameter\SimpleParameterProvider;
810

911
final readonly class UnchangedFilesFilter
1012
{
@@ -31,6 +33,12 @@ public function filterFilePaths(array $filePaths): array
3133
$this->changedFilesDetector->invalidateFile($filePath);
3234
}
3335

36+
// some files were served from cache, so rules ran on a subset only - unused skip reporting
37+
// would then flag every cached file's skip as falsely unused
38+
if (count($changedFileInfos) < count($filePaths)) {
39+
SimpleParameterProvider::setParameter(Option::IS_CACHED_RUN, true);
40+
}
41+
3442
return $changedFileInfos;
3543
}
3644
}

‎src/Configuration/Option.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ final class Option
118118
*/
119119
public const string IS_RUN_NARROWED = 'is_run_narrowed';
120120

121+
/**
122+
* True when the unchanged-files cache dropped at least one file from the run, so rules only ran
123+
* on the changed subset. Unused skip reporting is then disabled, as skips on cached files never
124+
* get a chance to match and would all look falsely unused.
125+
*
126+
* @internal
127+
*/
128+
public const string IS_CACHED_RUN = 'is_cached_run';
129+
121130
/**
122131
* @internal Use RectorConfig::fileExtensions() instead
123132
*/

‎src/Reporting/MissConfigurationReporter.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function reportUnusedSkips(ProcessResult $processResult): void
4040
count($unusedSkips) > 1 ? 'them' : 'it'
4141
));
4242

43-
$this->symfonyStyle->listing($unusedSkips);
43+
// add a blank line between items, so grouped rule skips stay visually separated
44+
$spacedUnusedSkips = array_map(static fn (string $unusedSkip): string => $unusedSkip . "\n", $unusedSkips);
45+
46+
$this->symfonyStyle->listing($spacedUnusedSkips);
4447
}
4548

4649
public function reportSkippedNeverRegisteredRules(): void

‎src/Reporting/UnusedSkipResolver.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function resolve(ProcessResult $processResult): array
4343
return [];
4444
}
4545

46+
// a cached run only re-processes changed files, so skips on cached files never get a chance
47+
// to match and would all look falsely unused - reporting them would be noise
48+
if (SimpleParameterProvider::provideBoolParameter(Option::IS_CACHED_RUN, false)) {
49+
return [];
50+
}
51+
4652
// map of rule => (trackable skip path => relative display path); skips are tracked at
4753
// runtime by their path, but rule-scoped ones are printed grouped under their rule so the
4854
// user knows exactly what to remove. Skip-everywhere rule skips (null path) are forgotten

‎tests/Reporting/UnusedSkipResolverTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function tearDown(): void
4646
SimpleParameterProvider::setParameter(Option::SKIP, []);
4747
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, false);
4848
SimpleParameterProvider::setParameter(Option::IS_RUN_NARROWED, false);
49+
SimpleParameterProvider::setParameter(Option::IS_CACHED_RUN, false);
4950
}
5051

5152
public function testResolvesUnusedSkipsAsRuleAndPath(): void
@@ -113,6 +114,18 @@ public function testResolvesNothingWhenRunIsNarrowed(): void
113114
$this->assertSame([], $this->unusedSkipResolver->resolve($processResult));
114115
}
115116

117+
public function testResolvesNothingWhenRunIsCached(): void
118+
{
119+
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, true);
120+
SimpleParameterProvider::setParameter(Option::IS_CACHED_RUN, true);
121+
122+
// a cached run only re-processes changed files, so skips on cached files never match and
123+
// are not false positives
124+
$processResult = new ProcessResult([], [], 0, []);
125+
126+
$this->assertSame([], $this->unusedSkipResolver->resolve($processResult));
127+
}
128+
116129
public function testResolvesNothingWhenDisabled(): void
117130
{
118131
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, false);

0 commit comments

Comments
 (0)