Skip to content

Commit 63c657c

Browse files
authored
[Configuration] Report explicit error when --only rule exists but is not registered (#8300)
1 parent a87065d commit 63c657c

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎src/Configuration/OnlyRuleResolver.php‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\Configuration;
66

7+
use PHPStan\Reflection\ReflectionProvider;
78
use Rector\Contract\Rector\RectorInterface;
89
use Rector\Exception\Configuration\RectorRuleNameAmbiguousException;
910
use Rector\Exception\Configuration\RectorRuleNotFoundException;
@@ -17,7 +18,8 @@
1718
* @param RectorInterface[] $rectors
1819
*/
1920
public function __construct(
20-
private array $rectors
21+
private array $rectors,
22+
private ReflectionProvider $reflectionProvider
2123
) {
2224
}
2325

@@ -82,6 +84,11 @@ public function resolve(string $rule): string
8284
PHP_EOL
8385
);
8486
} else {
87+
// the rule class exists, it is just missing in the config
88+
if ($this->isRectorRuleClass($rule)) {
89+
throw new RectorRuleNotFoundException($this->createUnregisteredMessage($rule));
90+
}
91+
8592
$message = sprintf(
8693
'Rule "%s" was not found.%sMake sure it is registered in your config or in one of the sets',
8794
$rule,
@@ -91,4 +98,34 @@ public function resolve(string $rule): string
9198

9299
throw new RectorRuleNotFoundException($message);
93100
}
101+
102+
/**
103+
* Is this an existing rule class, that is just not registered in the config?
104+
*/
105+
private function isRectorRuleClass(string $className): bool
106+
{
107+
if (! $this->reflectionProvider->hasClass($className)) {
108+
return false;
109+
}
110+
111+
$classReflection = $this->reflectionProvider->getClass($className);
112+
if ($classReflection->isAbstract()) {
113+
return false;
114+
}
115+
116+
return $classReflection->implementsInterface(RectorInterface::class);
117+
}
118+
119+
private function createUnregisteredMessage(string $ruleClass): string
120+
{
121+
$shortRuleClass = substr((string) strrchr($ruleClass, '\\'), 1);
122+
123+
return sprintf(
124+
'Rule "%s" exists, but is not registered in your Rector config.%sRegister it in your rector.php:'
125+
. PHP_EOL . PHP_EOL . ' ->withRules([%s::class])',
126+
$ruleClass,
127+
PHP_EOL,
128+
$shortRuleClass
129+
);
130+
}
94131
}

‎tests/Configuration/OnlyRuleResolverTest.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Rector\Tests\Configuration;
66

7+
use PHPStan\Reflection\ReflectionProvider;
78
use Rector\Configuration\OnlyRuleResolver;
89
use Rector\Contract\Rector\RectorInterface;
910
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
1011
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
1112
use Rector\Exception\Configuration\RectorRuleNameAmbiguousException;
1213
use Rector\Exception\Configuration\RectorRuleNotFoundException;
1314
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
15+
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;
1416

1517
final class OnlyRuleResolverTest extends AbstractLazyTestCase
1618
{
@@ -23,9 +25,10 @@ protected function setUp(): void
2325
$this->bootFromConfigFiles([__DIR__ . '/config/only_rule_resolver_config.php']);
2426
$rectorConfig = self::getContainer();
2527

26-
$this->onlyRuleResolver = new OnlyRuleResolver(iterator_to_array(
27-
$rectorConfig->tagged(RectorInterface::class)
28-
));
28+
$this->onlyRuleResolver = new OnlyRuleResolver(
29+
iterator_to_array($rectorConfig->tagged(RectorInterface::class)),
30+
$this->make(ReflectionProvider::class)
31+
);
2932
}
3033

3134
public function testResolveOk(): void
@@ -109,6 +112,18 @@ public function testResolveShortOkTwoLevels(): void
109112
);
110113
}
111114

115+
public function testResolveExistingButNotRegistered(): void
116+
{
117+
$this->expectExceptionMessageIsOrContains(
118+
'Rule "Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector" exists, but is not registered in your Rector config.'
119+
. PHP_EOL . 'Register it in your rector.php:' . PHP_EOL . PHP_EOL
120+
. ' ->withRules([SafeDeclareStrictTypesRector::class])'
121+
);
122+
$this->expectException(RectorRuleNotFoundException::class);
123+
124+
$this->onlyRuleResolver->resolve(SafeDeclareStrictTypesRector::class);
125+
}
126+
112127
public function testResolveShortAmbiguous(): void
113128
{
114129
$this->expectExceptionMessageIsOrContains(

0 commit comments

Comments
 (0)