Merge pull request #975 from rectorphp/remove-interface-rector

Added new rector - RemoveInterfacesRector
This commit is contained in:
Tomáš Votruba 2019-01-22 09:49:53 -08:00 committed by GitHub
commit 3f3b1d6a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 169 additions and 4 deletions

View File

@ -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);

View 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;
}
}

View File

@ -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
{
}
?>

View File

@ -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
{
}
?>

View File

@ -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];
}
}

View File

@ -0,0 +1,7 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source;
interface AnotherInterface
{
}

View File

@ -0,0 +1,7 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Interface_\RemoveInterfacesRector\Source;
interface SomeInterface
{
}