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-03-09 17:36:27 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Trait_;
|
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-05-01 01:36:32 +00:00
|
|
|
use RectorPrefix202305\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-07-05 09:49:56 +00:00
|
|
|
* @see \Rector\Tests\Removing\Rector\Class_\RemoveTraitUseRector\RemoveTraitUseRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveTraitUseRector extends AbstractRector implements ConfigurableRectorInterface
|
2019-03-09 17:36:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $traitsToRemove = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-03-09 17:36:27 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove specific traits from code', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2019-03-09 17:36:27 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
use SomeTrait;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-03-09 17:36:27 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['TraitNameToRemove'])]);
|
2019-03-09 17:36:27 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-03-09 17:36:27 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-03-09 17:36:27 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class, Trait_::class];
|
2019-03-09 17:36:27 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_|Trait_ $node
|
2019-03-09 17:36:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-03-09 17:36:27 +00:00
|
|
|
{
|
2022-11-17 10:44:46 +00:00
|
|
|
$classHasChanged = \false;
|
2021-07-05 09:49:56 +00:00
|
|
|
foreach ($node->getTraitUses() as $traitUse) {
|
|
|
|
foreach ($traitUse->traits as $trait) {
|
|
|
|
if (!$this->isNames($trait, $this->traitsToRemove)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->removeNode($traitUse);
|
2022-11-17 10:44:46 +00:00
|
|
|
$classHasChanged = \true;
|
2021-07-05 09:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 10:44:46 +00:00
|
|
|
if ($classHasChanged) {
|
2021-01-29 08:08:55 +00:00
|
|
|
return $node;
|
2019-03-09 17:36:27 +00:00
|
|
|
}
|
2021-01-29 08:08:55 +00:00
|
|
|
return null;
|
2019-03-09 17:36:27 +00:00
|
|
|
}
|
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 08:22:29 +00:00
|
|
|
Assert::allString($configuration);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->traitsToRemove = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2019-03-09 17:36:27 +00:00
|
|
|
}
|