rector/rules/Removing/Rector/Class_/RemoveInterfacesRector.php

73 lines
1.9 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare (strict_types=1);
namespace Rector\Removing\Rector\Class_;
2019-01-22 15:54:25 +00:00
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202403\Webmozart\Assert\Assert;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\RemoveInterfacesRectorTest
2019-09-03 09:11:45 +00:00
*/
final class RemoveInterfacesRector extends AbstractRector implements ConfigurableRectorInterface
2019-01-22 15:54:25 +00:00
{
/**
* @var string[]
2019-01-22 15:54:25 +00:00
*/
2020-07-29 23:39:41 +00:00
private $interfacesToRemove = [];
public function getRuleDefinition() : RuleDefinition
2019-01-22 15:54:25 +00:00
{
return new RuleDefinition('Removes interfaces usage from class.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
2019-01-22 15:54:25 +00:00
class SomeClass implements SomeInterface
{
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
2019-01-22 15:54:25 +00:00
class SomeClass
{
}
CODE_SAMPLE
, ['SomeInterface'])]);
2019-01-22 15:54:25 +00:00
}
/**
* @return array<class-string<Node>>
2019-01-22 15:54:25 +00:00
*/
public function getNodeTypes() : array
2019-01-22 15:54:25 +00:00
{
return [Class_::class];
2019-01-22 15:54:25 +00:00
}
/**
* @param Class_ $node
2019-01-22 15:54:25 +00:00
*/
public function refactor(Node $node) : ?Node
2019-01-22 15:54:25 +00:00
{
2019-02-17 14:12:47 +00:00
if ($node->implements === []) {
2019-01-22 15:54:25 +00:00
return null;
}
$isInterfacesRemoved = \false;
2019-01-22 15:54:25 +00:00
foreach ($node->implements as $key => $implement) {
if ($this->isNames($implement, $this->interfacesToRemove)) {
unset($node->implements[$key]);
$isInterfacesRemoved = \true;
2019-01-22 15:54:25 +00:00
}
}
if (!$isInterfacesRemoved) {
return null;
}
2019-01-22 15:54:25 +00:00
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
2020-07-29 23:39:41 +00:00
{
Assert::allString($configuration);
/** @var string[] $configuration */
$this->interfacesToRemove = $configuration;
2020-07-29 23:39:41 +00:00
}
2019-01-22 15:54:25 +00:00
}