mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 16:02:23 +02:00
Add MergeInterfaceRector [wip]
This commit is contained in:
parent
d48681b0e3
commit
3e72db8fc1
90
src/Rector/Interface_/MergeInterfacesRector.php
Normal file
90
src/Rector/Interface_/MergeInterfacesRector.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Interface_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* Covers cases like
|
||||
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/a1cdb4d2dd8f45d731244eed406e1d537218cc66
|
||||
* - https://github.com/FriendsOfPHP/PHP-CS-Fixer/commit/614d2e6f7af5a5b0be5363ff536aed2b7ee5a31d
|
||||
*/
|
||||
final class MergeInterfacesRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $oldToNewInterfaces = [];
|
||||
|
||||
/**
|
||||
* @param string[] $oldToNewInterfaces
|
||||
*/
|
||||
public function __construct(array $oldToNewInterfaces)
|
||||
{
|
||||
$this->oldToNewInterfaces = $oldToNewInterfaces;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Merges old interface to a new one, that already has its methods', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass implements SomeInterface, SomeOldInterface
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass implements SomeInterface
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Class_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->implements) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($node->implements as $implement) {
|
||||
$interface = (string) $implement->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
if (in_array($interface, array_keys($this->oldToNewInterfaces))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $classNode
|
||||
*/
|
||||
public function refactor(Node $classNode): ?Node
|
||||
{
|
||||
foreach ($classNode->implements as $key => $implement) {
|
||||
$interface = (string) $implement->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
if (in_array($interface, array_keys($this->oldToNewInterfaces))) {
|
||||
$classNode->implements[$key] = new Node\Name($this->oldToNewInterfaces[$interface]);
|
||||
}
|
||||
}
|
||||
|
||||
// @todo array unique
|
||||
// $classNode->implements = array_unique($classNode->implements);
|
||||
|
||||
return $classNode;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeInterface;
|
||||
use Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeOldInterface;
|
||||
|
||||
class SomeClass implements SomeInterface
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\MagicDisclosure\MergeInterfacesRectorTest;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @covers \Rector\Rector\Interface_\MergeInterfacesRector
|
||||
*/
|
||||
final class MergeInterfacesRectorTestTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideWrongToFixedFiles()
|
||||
*/
|
||||
public function test(string $wrong, string $fixed): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
|
||||
}
|
||||
|
||||
public function provideWrongToFixedFiles(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config.yml';
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source;
|
||||
|
||||
interface SomeOldInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeInterface;
|
||||
use Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeOldInterface;
|
||||
|
||||
class SomeClass implements SomeInterface, SomeOldInterface
|
||||
{
|
||||
|
||||
}
|
4
tests/Rector/Interface_/MergeInterfacesRector/config.yml
Normal file
4
tests/Rector/Interface_/MergeInterfacesRector/config.yml
Normal file
@ -0,0 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\Interface_\MergeInterfacesRector:
|
||||
$oldToNewInterfaces:
|
||||
'Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeOldInterface': 'Rector\Tests\Rector\MagicDisclosure\GetAndSetToMethodCallRector\Source\SomeInterface'
|
Loading…
x
Reference in New Issue
Block a user