Skip to content

Commit 8bf6146

Browse files
[Transform] Deprecate MergeInterfacesRector as niche, context-dependent and never used (#8772)
* [Transform] Deprecate MergeInterfacesRector as niche, context-dependent and never used * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 8546172 commit 8bf6146

8 files changed

Lines changed: 8 additions & 195 deletions

File tree

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/Fixture/fixture.php.inc‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/Fixture/fixture2.php.inc‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/Fixture/fqcn.php.inc‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/MergeInterfacesRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/Source/SomeInterface.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/Source/SomeOldInterface.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules-tests/Transform/Rector/Class_/MergeInterfacesRector/config/configured_rule.php‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎rules/Transform/Rector/Class_/MergeInterfacesRector.php‎

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,19 @@
55
namespace Rector\Transform\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Name\FullyQualified;
98
use PhpParser\Node\Stmt\Class_;
10-
use PhpParser\Node\Stmt\Interface_;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
1110
use Rector\Contract\Rector\ConfigurableRectorInterface;
11+
use Rector\Exception\ShouldNotHappenException;
1212
use Rector\Rector\AbstractRector;
1313
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
1414
use Rector\RuleDoc\RuleDefinition;
15-
use Webmozart\Assert\Assert;
1615

1716
/**
18-
* Covers cases like
19-
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/a1cdb4d2dd8f45d731244eed406e1d537218cc66
20-
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/614d2e6f7af5a5b0be5363ff536aed2b7ee5a31d
21-
*
22-
* @see \Rector\Tests\Transform\Rector\Class_\MergeInterfacesRector\MergeInterfacesRectorTest
17+
* @deprecated This rule is deprecated, as too niche and never used. Merging interfaces depends on project-specific context and is safer handled by a custom rule scoped to your exact case.
2318
*/
24-
final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface
19+
final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
2520
{
26-
/**
27-
* @var array<string, string>
28-
*/
29-
private array $oldToNewInterfaces = [];
30-
3121
public function getRuleDefinition(): RuleDefinition
3222
{
3323
return new RuleDefinition('Merge old interface to a new one, that already has its methods', [
@@ -64,56 +54,16 @@ public function getNodeTypes(): array
6454
*/
6555
public function refactor(Node $node): ?Node
6656
{
67-
if ($node->implements === []) {
68-
return null;
69-
}
70-
71-
$hasChanged = false;
72-
foreach ($node->implements as $key => $implement) {
73-
$oldInterfaces = array_keys($this->oldToNewInterfaces);
74-
if (! $this->isNames($implement, $oldInterfaces)) {
75-
continue;
76-
}
77-
78-
$interface = $this->getName($implement);
79-
$node->implements[$key] = new FullyQualified($this->oldToNewInterfaces[$interface]);
80-
$hasChanged = true;
81-
}
82-
83-
if (! $hasChanged) {
84-
return null;
85-
}
86-
87-
$this->makeImplementsUnique($node);
88-
89-
return $node;
57+
throw new ShouldNotHappenException(sprintf(
58+
'"%s" rule is deprecated, as too niche and never used; use a custom rule scoped to your exact case instead',
59+
self::class
60+
));
9061
}
9162

9263
/**
9364
* @param mixed[] $configuration
9465
*/
9566
public function configure(array $configuration): void
9667
{
97-
Assert::allString(array_keys($configuration));
98-
Assert::allString($configuration);
99-
100-
$this->oldToNewInterfaces = $configuration;
101-
}
102-
103-
private function makeImplementsUnique(Class_ $class): void
104-
{
105-
$alreadyAddedNames = [];
106-
/** @var array<int, Interface_> $implements */
107-
$implements = $class->implements;
108-
109-
foreach ($implements as $key => $name) {
110-
$fqnName = $this->getName($name);
111-
if (in_array($fqnName, $alreadyAddedNames, true)) {
112-
unset($class->implements[$key]);
113-
continue;
114-
}
115-
116-
$alreadyAddedNames[] = $fqnName;
117-
}
11868
}
11969
}

0 commit comments

Comments
 (0)