Skip to content

Commit 3da7c72

Browse files
authored
[reporting] Add reportUnusedSkips() to report skips that never matched (#8058)
* [reporting] Add reportUnusedSkips() to report skips that never matched Adds a ->reportUnusedSkips() config option, similar to PHPStan's reportUnusedIgnores. Tracks which ->withSkip() elements actually matched during a run via a shared UsedSkipCollector, then warns about path and rule-with-path skips that never matched and can be removed. Used skips are aggregated across parallel workers through the process bridge and carried on ProcessResult. Skip-everywhere rule skips are forgotten from the container at boot, so they are not trackable at runtime and are excluded from the report. * fixup! [reporting] Add reportUnusedSkips() to report skips that never matched
1 parent 84d949c commit 3da7c72

22 files changed

Lines changed: 363 additions & 9 deletions

File tree

‎.github/workflows/e2e.yaml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
- 'e2e/only-option-quote-single-bsdouble'
4141
- 'e2e/only-option-quote-single-equalnone'
4242
- 'e2e/parallel-custom-config'
43+
- 'e2e/parallel-unused-skips'
4344
- 'e2e/parallel-reflection-resolver'
4445
- 'e2e/parallel with space'
4546
- 'e2e/print-new-node'
@@ -72,6 +73,11 @@ jobs:
7273
working-directory: ${{ matrix.directory }}
7374
if: ${{ matrix.directory == 'e2e/parallel-custom-config' }}
7475

76+
# tests parallel aggregation of used skips for ->reportUnusedSkips()
77+
- run: php ../e2eTestRunner.php --config custom/config/rector.php
78+
working-directory: ${{ matrix.directory }}
79+
if: ${{ matrix.directory == 'e2e/parallel-unused-skips' }}
80+
7581
- run: php ../e2eTestRunner.php
7682
working-directory: ${{ matrix.directory }}
77-
if: ${{ matrix.directory != 'e2e/parallel-custom-config' }}
83+
if: ${{ matrix.directory != 'e2e/parallel-custom-config' && matrix.directory != 'e2e/parallel-unused-skips' }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"php": "^8.1"
4+
}
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->parallel();
10+
11+
$rectorConfig->paths([__DIR__ . '/../../src/']);
12+
13+
$rectorConfig->rule(SimplifyUselessVariableRector::class);
14+
15+
$rectorConfig->reportUnusedSkips();
16+
17+
// matched in the worker - must be excluded from the unused report (proves parallel aggregation)
18+
// plus a glob that never matches - must be reported as unused
19+
$rectorConfig->skip([
20+
SimplifyUselessVariableRector::class => ['*/src/*'],
21+
'*/NonexistentUnused/*',
22+
]);
23+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[OK] Rector is done!
2+
3+
[WARNING] This skip is unused, it never matched any element. You can remove it
4+
from "->withSkip()"
5+
6+
* */NonexistentUnused/*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace E2e\Parallel\Unused\Skips;
6+
7+
final class SomeClass
8+
{
9+
public function run()
10+
{
11+
$value = 1000;
12+
return $value;
13+
}
14+
}

‎src/Application/ApplicationFileProcessor.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Rector\Parallel\Application\ParallelFileProcessor;
1515
use Rector\PhpParser\Parser\ParserErrors;
1616
use Rector\Reporting\MissConfigurationReporter;
17+
use Rector\Skipper\Skipper\UsedSkipCollector;
1718
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
1819
use Rector\Util\ArrayParametersMerger;
1920
use Rector\ValueObject\Application\File;
@@ -49,6 +50,7 @@ public function __construct(
4950
private readonly FileProcessor $fileProcessor,
5051
private readonly ArrayParametersMerger $arrayParametersMerger,
5152
private readonly MissConfigurationReporter $missConfigurationReporter,
53+
private readonly UsedSkipCollector $usedSkipCollector,
5254
) {
5355
}
5456

@@ -161,7 +163,7 @@ public function processFiles(
161163
}
162164
}
163165

164-
return new ProcessResult($systemErrors, $fileDiffs, $totalChanged);
166+
return new ProcessResult($systemErrors, $fileDiffs, $totalChanged, $this->usedSkipCollector->provide());
165167
}
166168

167169
private function processFile(File $file, Configuration $configuration): FileProcessResult

‎src/Config/RectorConfig.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ public function reportingRealPath(bool $absolute = true): void
438438
SimpleParameterProvider::setParameter(Option::ABSOLUTE_FILE_PATH, $absolute);
439439
}
440440

441+
public function reportUnusedSkips(bool $report = true): void
442+
{
443+
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, $report);
444+
}
445+
441446
public function editorUrl(string $editorUrl): void
442447
{
443448
SimpleParameterProvider::setParameter(Option::EDITOR_URL, $editorUrl);

‎src/Configuration/Option.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ final class Option
103103
*/
104104
public const string SKIP = 'skip';
105105

106+
/**
107+
* @internal Use @see \Rector\Config\RectorConfig::reportUnusedSkips() instead
108+
* @var string
109+
*/
110+
public const string REPORT_UNUSED_SKIPS = 'report_unused_skips';
111+
106112
/**
107113
* @internal Use RectorConfig::fileExtensions() instead
108114
*/

‎src/Configuration/RectorConfigBuilder.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ final class RectorConfigBuilder
174174

175175
private ?bool $reportingRealPath = null;
176176

177+
private ?bool $reportUnusedSkips = null;
178+
177179
/**
178180
* @var string[]
179181
*/
@@ -394,6 +396,10 @@ public function __invoke(RectorConfig $rectorConfig): void
394396
$rectorConfig->reportingRealPath($this->reportingRealPath);
395397
}
396398

399+
if ($this->reportUnusedSkips !== null) {
400+
$rectorConfig->reportUnusedSkips($this->reportUnusedSkips);
401+
}
402+
397403
if ($this->editorUrl !== null) {
398404
$rectorConfig->editorUrl($this->editorUrl);
399405
}
@@ -1314,6 +1320,17 @@ public function withRealPathReporting(bool $absolutePath = true): self
13141320
return $this;
13151321
}
13161322

1323+
/**
1324+
* Report skips configured via withSkip() that never matched anything during the run,
1325+
* so they can be safely removed.
1326+
*/
1327+
public function reportUnusedSkips(bool $report = true): self
1328+
{
1329+
$this->reportUnusedSkips = $report;
1330+
1331+
return $this;
1332+
}
1333+
13171334
public function withEditorUrl(string $editorUrl): self
13181335
{
13191336
$this->editorUrl = $editorUrl;

0 commit comments

Comments
 (0)