|
5 | 5 | namespace Rector\Transform\Rector\Class_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Name\FullyQualified; |
9 | 8 | use PhpParser\Node\Stmt\Class_; |
10 | | -use PhpParser\Node\Stmt\Interface_; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
11 | 10 | use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 11 | +use Rector\Exception\ShouldNotHappenException; |
12 | 12 | use Rector\Rector\AbstractRector; |
13 | 13 | use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; |
14 | 14 | use Rector\RuleDoc\RuleDefinition; |
15 | | -use Webmozart\Assert\Assert; |
16 | 15 |
|
17 | 16 | /** |
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. |
23 | 18 | */ |
24 | | -final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface |
| 19 | +final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface |
25 | 20 | { |
26 | | - /** |
27 | | - * @var array<string, string> |
28 | | - */ |
29 | | - private array $oldToNewInterfaces = []; |
30 | | - |
31 | 21 | public function getRuleDefinition(): RuleDefinition |
32 | 22 | { |
33 | 23 | return new RuleDefinition('Merge old interface to a new one, that already has its methods', [ |
@@ -64,56 +54,16 @@ public function getNodeTypes(): array |
64 | 54 | */ |
65 | 55 | public function refactor(Node $node): ?Node |
66 | 56 | { |
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 | + )); |
90 | 61 | } |
91 | 62 |
|
92 | 63 | /** |
93 | 64 | * @param mixed[] $configuration |
94 | 65 | */ |
95 | 66 | public function configure(array $configuration): void |
96 | 67 | { |
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 | | - } |
118 | 68 | } |
119 | 69 | } |
0 commit comments