2020-08-12 09:44:34 +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\DeadCode\Rector\ClassLike;
|
2020-08-12 09:44:34 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
2023-09-11 07:37:02 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2023-09-20 12:41:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
2023-09-11 09:08:21 +00:00
|
|
|
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Webmozart\Assert\Assert;
|
2020-08-12 09:44:34 +00:00
|
|
|
/**
|
2021-03-21 18:41:17 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveAnnotationRector\RemoveAnnotationRectorTest
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveAnnotationRector extends AbstractRector implements ConfigurableRectorInterface
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2021-01-19 19:45:30 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
|
2021-01-19 19:45:30 +00:00
|
|
|
*/
|
|
|
|
private $phpDocTagRemover;
|
2023-09-11 09:08:21 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
|
|
|
*/
|
|
|
|
private $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
2023-06-08 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $annotationsToRemove = [];
|
2023-09-20 12:41:26 +00:00
|
|
|
public function __construct(PhpDocTagRemover $phpDocTagRemover, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory)
|
2021-01-19 19:45:30 +00:00
|
|
|
{
|
|
|
|
$this->phpDocTagRemover = $phpDocTagRemover;
|
2023-09-11 09:08:21 +00:00
|
|
|
$this->docBlockUpdater = $docBlockUpdater;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2021-01-19 19:45:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove annotation by names', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-08-12 09:44:34 +00:00
|
|
|
/**
|
|
|
|
* @method getName()
|
|
|
|
*/
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-08-12 09:44:34 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['method'])]);
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassLike::class, FunctionLike::class, Property::class, ClassConst::class];
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassLike|FunctionLike|Property|ClassConst $node
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2023-09-11 07:37:02 +00:00
|
|
|
Assert::notEmpty($this->annotationsToRemove);
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
|
|
|
|
if (!$phpDocInfo instanceof PhpDocInfo) {
|
2021-01-18 01:56:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-09-11 07:37:02 +00:00
|
|
|
$hasChanged = \false;
|
2020-08-12 09:44:34 +00:00
|
|
|
foreach ($this->annotationsToRemove as $annotationToRemove) {
|
2023-09-11 07:37:02 +00:00
|
|
|
$namedHasChanged = $this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToRemove);
|
|
|
|
if ($namedHasChanged) {
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!\is_a($annotationToRemove, PhpDocTagValueNode::class, \true)) {
|
2021-07-01 16:34:59 +00:00
|
|
|
continue;
|
2021-01-25 16:20:05 +00:00
|
|
|
}
|
2023-09-11 07:37:02 +00:00
|
|
|
$typedHasChanged = $phpDocInfo->removeByType($annotationToRemove);
|
|
|
|
if ($typedHasChanged) {
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
2021-01-18 01:56:12 +00:00
|
|
|
}
|
2023-09-11 07:37:02 +00:00
|
|
|
if ($hasChanged) {
|
2023-09-11 09:08:21 +00:00
|
|
|
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
|
2021-05-01 10:59:25 +00:00
|
|
|
return $node;
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
2021-05-01 10:59:25 +00:00
|
|
|
return null;
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2020-08-12 09:44:34 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-08-12 09:44:34 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString($configuration);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->annotationsToRemove = $configuration;
|
2020-08-12 09:44:34 +00:00
|
|
|
}
|
|
|
|
}
|