2019-01-22 15:54:25 +00:00
|
|
|
<?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;
|
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\Rector\Interface_\RemoveInterfacesRector\RemoveInterfacesRectorTest
|
|
|
|
*/
|
2019-01-22 15:54:25 +00:00
|
|
|
final class RemoveInterfacesRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $interfacesToRemove = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $interfacesToRemove
|
|
|
|
*/
|
2019-04-26 13:32:24 +00:00
|
|
|
public function __construct(array $interfacesToRemove = [])
|
2019-01-22 15:54:25 +00:00
|
|
|
{
|
|
|
|
$this->interfacesToRemove = $interfacesToRemove;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Removes interfaces usage from class.', [
|
|
|
|
new ConfiguredCodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-01-22 15:54:25 +00:00
|
|
|
class SomeClass implements SomeInterface
|
|
|
|
{
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-01-22 15:54:25 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-01-22 15:54:25 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-01-22 15:54:25 +00:00
|
|
|
,
|
|
|
|
['SomeInterface']
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Class_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Class_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($node->implements === []) {
|
2019-01-22 15:54:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($node->implements as $key => $implement) {
|
|
|
|
if ($this->isNames($implement, $this->interfacesToRemove)) {
|
|
|
|
unset($node->implements[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|