Skip to content

Commit 4893a6a

Browse files
authored
Warn on skipped classes that are not Rector rules (#8323)
1 parent e37111c commit 4893a6a

5 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎src/Configuration/Option.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ final class Option
240240
*/
241241
public const string SKIPPED_RECTOR_RULES = 'skipped_rector_rules';
242242

243+
/**
244+
* @internal For reporting skipped classes that are not Rector rules
245+
*/
246+
public const string SKIPPED_NON_RECTOR_CLASSES = 'skipped_non_rector_classes';
247+
243248
/**
244249
* @internal For reporting deprecated cache meta extensions
245250
*/

‎src/Console/Command/ProcessCommand.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
189189
$this->deprecatedRulesReporter->reportDeprecatedAttributesSetsArgs();
190190

191191
$this->missConfigurationReporter->reportSkippedNeverRegisteredRules();
192+
$this->missConfigurationReporter->reportSkippedNonRectorClasses();
192193
$this->missConfigurationReporter->reportUnusedSkips($processResult);
193194

194195
return $this->resolveReturnCode($processResult, $configuration);

‎src/Reporting/MissConfigurationReporter.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\Configuration\Option;
88
use Rector\Configuration\Parameter\SimpleParameterProvider;
99
use Rector\Configuration\VendorMissAnalyseGuard;
10+
use Rector\Contract\Rector\RectorInterface;
1011
use Rector\PostRector\Contract\Rector\PostRectorInterface;
1112
use Rector\ValueObject\ProcessResult;
1213
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -72,6 +73,26 @@ public function reportSkippedNeverRegisteredRules(): void
7273
$this->symfonyStyle->listing($neverRegisteredSkippedRules);
7374
}
7475

76+
public function reportSkippedNonRectorClasses(): void
77+
{
78+
$skippedNonRectorClasses = SimpleParameterProvider::provideArrayParameter(
79+
Option::SKIPPED_NON_RECTOR_CLASSES
80+
);
81+
82+
if ($skippedNonRectorClasses === []) {
83+
return;
84+
}
85+
86+
$this->symfonyStyle->warning(sprintf(
87+
'%s not a Rector rule, so %s never be skipped. Only classes that implement "%s" can be used in "->withSkip()"',
88+
count($skippedNonRectorClasses) > 1 ? 'These skipped classes are' : 'This skipped class is',
89+
count($skippedNonRectorClasses) > 1 ? 'they can' : 'it can',
90+
RectorInterface::class
91+
));
92+
93+
$this->symfonyStyle->listing($skippedNonRectorClasses);
94+
}
95+
7596
/**
7697
* @param string[] $filePaths
7798
*/

‎src/Validation/RectorConfigValidator.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Rector\Configuration\Option;
88
use Rector\Configuration\Parameter\SimpleParameterProvider;
9+
use Rector\Contract\Rector\RectorInterface;
910
use Rector\Exception\ShouldNotHappenException;
11+
use Rector\PostRector\Contract\Rector\PostRectorInterface;
1012

1113
final class RectorConfigValidator
1214
{
@@ -33,8 +35,14 @@ public static function ensureRectorRulesExist(array $skip): void
3335
{
3436
$nonExistingRules = [];
3537
$skippedRectorRules = [];
38+
$skippedNonRectorClasses = [];
3639

3740
foreach ($skip as $key => $value) {
41+
if (is_string($key) && self::isNonRectorClass($key)) {
42+
$skippedNonRectorClasses[] = $key;
43+
continue;
44+
}
45+
3846
if (self::isRectorClassValue($key)) {
3947
if (class_exists($key)) {
4048
$skippedRectorRules[] = $key;
@@ -58,6 +66,7 @@ public static function ensureRectorRulesExist(array $skip): void
5866
}
5967

6068
SimpleParameterProvider::addParameter(Option::SKIPPED_RECTOR_RULES, $skippedRectorRules);
69+
SimpleParameterProvider::addParameter(Option::SKIPPED_NON_RECTOR_CLASSES, $skippedNonRectorClasses);
6170

6271
if ($nonExistingRules === []) {
6372
return;
@@ -73,6 +82,23 @@ public static function ensureRectorRulesExist(array $skip): void
7382
);
7483
}
7584

85+
/**
86+
* Only Rector rules are matched against skipped classes, so any other class can never be skipped
87+
*/
88+
private static function isNonRectorClass(string $key): bool
89+
{
90+
// interfaces are allowed, as they can mark a group of Rector rules
91+
if (! class_exists($key)) {
92+
return false;
93+
}
94+
95+
if (is_a($key, RectorInterface::class, true)) {
96+
return false;
97+
}
98+
99+
return ! is_a($key, PostRectorInterface::class, true);
100+
}
101+
76102
private static function isRectorClassValue(mixed $value): bool
77103
{
78104
// only validate string

‎tests/Reporting/MissConfigurationReporterTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
use Rector\Configuration\Option;
88
use Rector\Configuration\Parameter\SimpleParameterProvider;
99
use Rector\Configuration\VendorMissAnalyseGuard;
10+
use Rector\Php85\Rector\FuncCall\OrdSingleByteRector;
11+
use Rector\PostRector\Rector\NameImportingPostRector;
1012
use Rector\Reporting\MissConfigurationReporter;
1113
use Rector\Reporting\UnusedSkipResolver;
1214
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
1315
use Rector\Tests\Skipper\Skipper\Fixture\Element\FifthElement;
1416
use Rector\Tests\Skipper\Skipper\Fixture\Element\ThreeMan;
1517
use Rector\Tests\Skipper\Skipper\Source\AnotherClassToSkip;
18+
use Rector\Validation\RectorConfigValidator;
1619
use Rector\ValueObject\ProcessResult;
1720
use Symfony\Component\Console\Input\ArrayInput;
1821
use Symfony\Component\Console\Output\BufferedOutput;
@@ -59,6 +62,38 @@ protected function tearDown(): void
5962
{
6063
SimpleParameterProvider::setParameter(Option::SKIP, []);
6164
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, false);
65+
SimpleParameterProvider::setParameter(Option::SKIPPED_NON_RECTOR_CLASSES, []);
66+
}
67+
68+
public function testReportsSkippedNonRectorClass(): void
69+
{
70+
RectorConfigValidator::ensureRectorRulesExist([
71+
// not a Rector rule, can never be skipped
72+
AnotherClassToSkip::class => ['some/path'],
73+
// Rector rule, must not be reported
74+
OrdSingleByteRector::class => ['some/path'],
75+
// post rector rule, must not be reported
76+
NameImportingPostRector::class => ['some/path'],
77+
]);
78+
79+
$this->missConfigurationReporter->reportSkippedNonRectorClasses();
80+
81+
$output = $this->bufferedOutput->fetch();
82+
83+
$this->assertStringContainsString('AnotherClassToSkip', $output);
84+
$this->assertStringNotContainsString('OrdSingleByteRector', $output);
85+
$this->assertStringNotContainsString('NameImportingPostRector', $output);
86+
}
87+
88+
public function testReportsNothingOnRectorClassesOnly(): void
89+
{
90+
RectorConfigValidator::ensureRectorRulesExist([
91+
OrdSingleByteRector::class => ['some/path'],
92+
]);
93+
94+
$this->missConfigurationReporter->reportSkippedNonRectorClasses();
95+
96+
$this->assertSame('', $this->bufferedOutput->fetch());
6297
}
6398

6499
public function testReportsOnlyTrackableUnusedSkips(): void

0 commit comments

Comments
 (0)