2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\Removing\Rector\Class_ ;
2019-01-22 15:54:25 +00:00
2022-06-06 17:12:56 +00:00
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 ;
2022-06-07 06:40:10 +00:00
use RectorPrefix20220607\Webmozart\Assert\Assert ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\RemoveInterfacesRectorTest
2019-09-03 09:11:45 +00:00
*/
2022-06-06 17:12:56 +00:00
final class RemoveInterfacesRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
2019-01-22 15:54:25 +00:00
{
/**
2021-11-27 13:43:28 +00:00
* @ var string []
2019-01-22 15:54:25 +00:00
*/
2020-07-29 23:39:41 +00:00
private $interfacesToRemove = [];
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-01-22 15:54:25 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Removes interfaces usage from class.' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample ( <<< 'CODE_SAMPLE'
2019-01-22 15:54:25 +00:00
class SomeClass implements SomeInterface
{
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-01-22 15:54:25 +00:00
class SomeClass
{
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-12-06 21:12:03 +00:00
, [ 'SomeInterface' ])]);
2019-01-22 15:54:25 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-01-22 15:54:25 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-01-22 15:54:25 +00:00
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Stmt\Class_ :: class ];
2019-01-22 15:54:25 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param Class_ $node
2019-01-22 15:54:25 +00:00
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\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 ;
}
2022-02-24 18:47:53 +00:00
$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 ]);
2022-02-24 18:47:53 +00:00
$isInterfacesRemoved = \true ;
2019-01-22 15:54:25 +00:00
}
}
2022-02-24 18:47:53 +00:00
if ( ! $isInterfacesRemoved ) {
return null ;
}
2019-01-22 15:54:25 +00:00
return $node ;
}
2021-05-02 10:46:55 +00:00
/**
2021-11-27 13:43:28 +00:00
* @ param mixed [] $configuration
2021-05-02 10:46:55 +00:00
*/
2021-12-10 10:22:23 +00:00
public function configure ( array $configuration ) : void
2020-07-29 23:39:41 +00:00
{
2022-06-07 06:40:10 +00:00
\RectorPrefix20220607\Webmozart\Assert\Assert :: allString ( $configuration );
2022-02-18 01:46:17 +00:00
/** @var string[] $configuration */
$this -> interfacesToRemove = $configuration ;
2020-07-29 23:39:41 +00:00
}
2019-01-22 15:54:25 +00:00
}