2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Transform\Rector\Class_;
|
2018-06-13 13:17:27 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-07-16 14:24:54 +00:00
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Webmozart\Assert\Assert;
|
2018-06-13 13:17:27 +00:00
|
|
|
/**
|
|
|
|
* Covers cases like
|
|
|
|
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/a1cdb4d2dd8f45d731244eed406e1d537218cc66
|
|
|
|
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/614d2e6f7af5a5b0be5363ff536aed2b7ee5a31d
|
2020-07-29 23:39:41 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\Class_\MergeInterfacesRector\MergeInterfacesRectorTest
|
2018-06-13 13:17:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MergeInterfacesRector extends AbstractRector implements ConfigurableRectorInterface
|
2018-06-13 13:17:27 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-05-02 10:46:55 +00:00
|
|
|
* @var array<string, string>
|
2018-06-13 13:17:27 +00:00
|
|
|
*/
|
2020-07-29 23:39:41 +00:00
|
|
|
private $oldToNewInterfaces = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-06-13 13:17:27 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Merges old interface to a new one, that already has its methods', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2018-06-13 13:17:27 +00:00
|
|
|
class SomeClass implements SomeInterface, SomeOldInterface
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-06-13 13:17:27 +00:00
|
|
|
class SomeClass implements SomeInterface
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['SomeOldInterface' => 'SomeInterface'])]);
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|
2018-08-14 22:12:41 +00:00
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-08-14 22:12:41 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-06-13 13:17:27 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2018-06-13 13:17:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-06-13 13:17:27 +00:00
|
|
|
{
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($node->implements === []) {
|
2018-08-14 21:33:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \false;
|
2018-10-15 04:36:58 +00:00
|
|
|
foreach ($node->implements as $key => $implement) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$oldInterfaces = \array_keys($this->oldToNewInterfaces);
|
|
|
|
if (!$this->isNames($implement, $oldInterfaces)) {
|
2019-01-22 15:54:25 +00:00
|
|
|
continue;
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|
2019-01-22 15:54:25 +00:00
|
|
|
$interface = $this->getName($implement);
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->implements[$key] = new Name($this->oldToNewInterfaces[$interface]);
|
2022-02-28 06:43:27 +00:00
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|
2018-10-15 04:36:58 +00:00
|
|
|
$this->makeImplementsUnique($node);
|
|
|
|
return $node;
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString(\array_keys($configuration));
|
|
|
|
Assert::allString($configuration);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->oldToNewInterfaces = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function makeImplementsUnique(Class_ $class) : void
|
2018-06-21 11:53:05 +00:00
|
|
|
{
|
|
|
|
$alreadyAddedNames = [];
|
2022-07-16 14:24:54 +00:00
|
|
|
/** @var array<int, Interface_> $implements */
|
|
|
|
$implements = $class->implements;
|
|
|
|
foreach ($implements as $key => $name) {
|
2019-01-22 21:13:03 +00:00
|
|
|
$fqnName = $this->getName($name);
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\in_array($fqnName, $alreadyAddedNames, \true)) {
|
2022-08-06 21:53:15 +00:00
|
|
|
unset($class->implements[$key]);
|
2018-06-21 11:53:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$alreadyAddedNames[] = $fqnName;
|
|
|
|
}
|
|
|
|
}
|
2018-06-13 13:17:27 +00:00
|
|
|
}
|