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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

66 changes: 8 additions & 58 deletions rules/Transform/Rector/Class_/MergeInterfacesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,19 @@
namespace Rector\Transform\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* Covers cases like
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/a1cdb4d2dd8f45d731244eed406e1d537218cc66
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/614d2e6f7af5a5b0be5363ff536aed2b7ee5a31d
*
* @see \Rector\Tests\Transform\Rector\Class_\MergeInterfacesRector\MergeInterfacesRectorTest
* @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.
*/
final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface
final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, string>
*/
private array $oldToNewInterfaces = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Merge old interface to a new one, that already has its methods', [
Expand Down Expand Up @@ -64,56 +54,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->implements === []) {
return null;
}

$hasChanged = false;
foreach ($node->implements as $key => $implement) {
$oldInterfaces = array_keys($this->oldToNewInterfaces);
if (! $this->isNames($implement, $oldInterfaces)) {
continue;
}

$interface = $this->getName($implement);
$node->implements[$key] = new FullyQualified($this->oldToNewInterfaces[$interface]);
$hasChanged = true;
}

if (! $hasChanged) {
return null;
}

$this->makeImplementsUnique($node);

return $node;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and never used; use a custom rule scoped to your exact case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allString(array_keys($configuration));
Assert::allString($configuration);

$this->oldToNewInterfaces = $configuration;
}

private function makeImplementsUnique(Class_ $class): void
{
$alreadyAddedNames = [];
/** @var array<int, Interface_> $implements */
$implements = $class->implements;

foreach ($implements as $key => $name) {
$fqnName = $this->getName($name);
if (in_array($fqnName, $alreadyAddedNames, true)) {
unset($class->implements[$key]);
continue;
}

$alreadyAddedNames[] = $fqnName;
}
}
}
Loading