rector/rules/Removing/Rector/Class_/RemoveInterfacesRector.php
Tomas Votruba ad10fd18db Updated Rector to commit 96400215b5
96400215b5 [automated] Re-Generate Nodes/Rectors Documentation (#2314)
2022-05-15 09:42:33 +00:00

73 lines
2.2 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Removing\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix20220515\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\RemoveInterfacesRectorTest
*/
final class RemoveInterfacesRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
{
/**
* @var string[]
*/
private $interfacesToRemove = [];
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Removes interfaces usage from class.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass implements SomeInterface
{
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
}
CODE_SAMPLE
, ['SomeInterface'])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node->implements === []) {
return null;
}
$isInterfacesRemoved = \false;
foreach ($node->implements as $key => $implement) {
if ($this->isNames($implement, $this->interfacesToRemove)) {
unset($node->implements[$key]);
$isInterfacesRemoved = \true;
}
}
if (!$isInterfacesRemoved) {
return null;
}
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
\RectorPrefix20220515\Webmozart\Assert\Assert::allString($configuration);
/** @var string[] $configuration */
$this->interfacesToRemove = $configuration;
}
}