mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
Merge pull request #975 from rectorphp/remove-interface-rector
Added new rector - RemoveInterfacesRector
This commit is contained in:
commit
3f3b1d6a24
@ -71,11 +71,12 @@ CODE_SAMPLE
|
||||
}
|
||||
|
||||
foreach ($node->implements as $key => $implement) {
|
||||
$interface = (string) $implement->getAttribute(Attribute::RESOLVED_NAME);
|
||||
|
||||
if (array_key_exists($interface, $this->oldToNewInterfaces)) {
|
||||
$node->implements[$key] = new Name($this->oldToNewInterfaces[$interface]);
|
||||
if (! $this->isNames($implement, array_keys($this->oldToNewInterfaces))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$interface = $this->getName($implement);
|
||||
$node->implements[$key] = new Name($this->oldToNewInterfaces[$interface]);
|
||||
}
|
||||
|
||||
$this->makeImplementsUnique($node);
|
||||
|
74
src/Rector/Interface_/RemoveInterfacesRector.php
Normal file
74
src/Rector/Interface_/RemoveInterfacesRector.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Interface_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class RemoveInterfacesRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $interfacesToRemove = [];
|
||||
|
||||
/**
|
||||
* @param string[] $interfacesToRemove
|
||||
*/
|
||||
public function __construct(array $interfacesToRemove)
|
||||
{
|
||||
$this->interfacesToRemove = $interfacesToRemove;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Removes interfaces usage from class.', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass implements SomeInterface
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
['SomeInterface']
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->implements) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($node->implements as $key => $implement) {
|
||||
if ($this->isNames($implement, $this->interfacesToRemove)) {
|
||||
unset($node->implements[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$node->implements = array_values($node->implements);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\SomeInterface;
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\AnotherInterface;
|
||||
|
||||
class SomeClass implements SomeInterface, AnotherInterface
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\SomeInterface;
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\AnotherInterface;
|
||||
|
||||
class SomeClass implements AnotherInterface
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\SomeInterface;
|
||||
|
||||
class SomeClass2 implements SomeInterface
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\SomeInterface;
|
||||
|
||||
class SomeClass2
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,28 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector;
|
||||
|
||||
use Rector\Rector\Interface_\RemoveInterfacesRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source\SomeInterface;
|
||||
|
||||
final class RemoveInterfacesRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture2.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return RemoveInterfacesRector::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
protected function getRectorConfiguration(): array
|
||||
{
|
||||
return [SomeInterface::class];
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source;
|
||||
|
||||
interface AnotherInterface
|
||||
{
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source;
|
||||
|
||||
interface SomeInterface
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user