diff --git a/src/Configuration/ConfigurationRuleFilter.php b/src/Configuration/ConfigurationRuleFilter.php index 572bd58a614..7f9ea2cf13b 100644 --- a/src/Configuration/ConfigurationRuleFilter.php +++ b/src/Configuration/ConfigurationRuleFilter.php @@ -4,6 +4,7 @@ namespace Rector\Configuration; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Configuration\Parameter\SimpleParameterProvider; use Rector\Contract\Rector\RectorInterface; use Rector\ValueObject\Configuration; @@ -30,6 +31,12 @@ public function setConfiguration(Configuration $configuration): void */ public function filter(array $rectors): array { + // deprecated rules only warn via DeprecatedRulesReporter; they must never run, as their + // refactor() throws to signal the deprecation - keep them out of the active set + $rectors = array_values( + array_filter($rectors, static fn (RectorInterface $rector): bool => ! $rector instanceof DeprecatedInterface) + ); + if (! $this->configuration instanceof Configuration) { return $rectors; } diff --git a/tests/Configuration/ConfigurationRuleFilterTest.php b/tests/Configuration/ConfigurationRuleFilterTest.php index 32d7839a680..0ac159485c6 100644 --- a/tests/Configuration/ConfigurationRuleFilterTest.php +++ b/tests/Configuration/ConfigurationRuleFilterTest.php @@ -8,6 +8,7 @@ use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector; use Rector\Php80\Rector\Class_\StringableForToStringRector; use Rector\Testing\PHPUnit\AbstractLazyTestCase; +use Rector\Tests\Configuration\Source\DeprecatedRectorStub; use Rector\ValueObject\Configuration; final class ConfigurationRuleFilterTest extends AbstractLazyTestCase @@ -49,6 +50,20 @@ public function testWithoutPhpOnlyKeepsAllRules(): void $this->assertSame([$stringableForToStringRector, $removeDeadInstanceOfRector], $filteredRectors); } + public function testFiltersOutDeprecatedRules(): void + { + $removeDeadInstanceOfRector = $this->make(RemoveDeadInstanceOfRector::class); + $deprecatedRectorStub = $this->make(DeprecatedRectorStub::class); + + $this->configurationRuleFilter->setConfiguration($this->createConfiguration(false)); + + $filteredRectors = $this->configurationRuleFilter->filter( + [$removeDeadInstanceOfRector, $deprecatedRectorStub] + ); + + $this->assertSame([$removeDeadInstanceOfRector], $filteredRectors); + } + private function createConfiguration(bool $isPhpOnly): Configuration { return new Configuration(isPhpOnly: $isPhpOnly); diff --git a/tests/Configuration/Source/DeprecatedRectorStub.php b/tests/Configuration/Source/DeprecatedRectorStub.php new file mode 100644 index 00000000000..51bb072bcba --- /dev/null +++ b/tests/Configuration/Source/DeprecatedRectorStub.php @@ -0,0 +1,30 @@ +