Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Configuration/ConfigurationRuleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Configuration/ConfigurationRuleFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/Configuration/Source/DeprecatedRectorStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Configuration\Source;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class DeprecatedRectorStub extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Deprecated stub rule for tests', [new CodeSample('before', 'after')]);
}

public function getNodeTypes(): array
{
return [Class_::class];
}

public function refactor(Node $node): ?Node
{
return null;
}
}
Loading