Skip to content

Commit a9c7644

Browse files
authored
[Configuration] Skip deprecated rules from active set, warn only instead of crashing (#8358)
1 parent 60ee400 commit a9c7644

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/Configuration/ConfigurationRuleFilter.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\Configuration;
66

7+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
78
use Rector\Configuration\Parameter\SimpleParameterProvider;
89
use Rector\Contract\Rector\RectorInterface;
910
use Rector\ValueObject\Configuration;
@@ -30,6 +31,12 @@ public function setConfiguration(Configuration $configuration): void
3031
*/
3132
public function filter(array $rectors): array
3233
{
34+
// deprecated rules only warn via DeprecatedRulesReporter; they must never run, as their
35+
// refactor() throws to signal the deprecation - keep them out of the active set
36+
$rectors = array_values(
37+
array_filter($rectors, static fn (RectorInterface $rector): bool => ! $rector instanceof DeprecatedInterface)
38+
);
39+
3340
if (! $this->configuration instanceof Configuration) {
3441
return $rectors;
3542
}

‎tests/Configuration/ConfigurationRuleFilterTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
99
use Rector\Php80\Rector\Class_\StringableForToStringRector;
1010
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
11+
use Rector\Tests\Configuration\Source\DeprecatedRectorStub;
1112
use Rector\ValueObject\Configuration;
1213

1314
final class ConfigurationRuleFilterTest extends AbstractLazyTestCase
@@ -49,6 +50,20 @@ public function testWithoutPhpOnlyKeepsAllRules(): void
4950
$this->assertSame([$stringableForToStringRector, $removeDeadInstanceOfRector], $filteredRectors);
5051
}
5152

53+
public function testFiltersOutDeprecatedRules(): void
54+
{
55+
$removeDeadInstanceOfRector = $this->make(RemoveDeadInstanceOfRector::class);
56+
$deprecatedRectorStub = $this->make(DeprecatedRectorStub::class);
57+
58+
$this->configurationRuleFilter->setConfiguration($this->createConfiguration(false));
59+
60+
$filteredRectors = $this->configurationRuleFilter->filter(
61+
[$removeDeadInstanceOfRector, $deprecatedRectorStub]
62+
);
63+
64+
$this->assertSame([$removeDeadInstanceOfRector], $filteredRectors);
65+
}
66+
5267
private function createConfiguration(bool $isPhpOnly): Configuration
5368
{
5469
return new Configuration(isPhpOnly: $isPhpOnly);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Configuration\Source;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
final class DeprecatedRectorStub extends AbstractRector implements DeprecatedInterface
15+
{
16+
public function getRuleDefinition(): RuleDefinition
17+
{
18+
return new RuleDefinition('Deprecated stub rule for tests', [new CodeSample('before', 'after')]);
19+
}
20+
21+
public function getNodeTypes(): array
22+
{
23+
return [Class_::class];
24+
}
25+
26+
public function refactor(Node $node): ?Node
27+
{
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)