2020-07-28 23:41:20 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-07-28 23:41:20 +00:00
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocManipulator;
|
|
|
|
|
2021-01-30 15:06:43 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Node;
|
2020-07-28 23:41:20 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
2021-04-04 09:01:11 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
2020-07-28 23:41:20 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2021-08-08 00:26:40 +00:00
|
|
|
use RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser;
|
2021-01-19 19:45:30 +00:00
|
|
|
final class PhpDocTagRemover
|
2020-07-28 23:41:20 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
public function removeByName(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo, string $name) : void
|
2020-07-28 23:41:20 +00:00
|
|
|
{
|
2021-03-20 15:27:18 +00:00
|
|
|
$phpDocNode = $phpDocInfo->getPhpDocNode();
|
|
|
|
foreach ($phpDocNode->children as $key => $phpDocChildNode) {
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$phpDocChildNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode) {
|
2020-07-28 23:41:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-04 09:01:11 +00:00
|
|
|
if ($this->areAnnotationNamesEqual($name, $phpDocChildNode->name)) {
|
|
|
|
unset($phpDocNode->children[$key]);
|
|
|
|
$phpDocInfo->markAsChanged();
|
2020-07-28 23:41:20 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($phpDocChildNode->value instanceof \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode) {
|
2021-06-22 10:43:16 +00:00
|
|
|
$doctrineAnnotationTagValueNode = $phpDocChildNode->value;
|
|
|
|
if ($doctrineAnnotationTagValueNode->hasClassName($name)) {
|
2021-04-04 09:01:11 +00:00
|
|
|
unset($phpDocNode->children[$key]);
|
|
|
|
$phpDocInfo->markAsChanged();
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 23:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function removeTagValueFromNode(\Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo, \PHPStan\PhpDocParser\Ast\Node $desiredNode) : void
|
2020-07-28 23:41:20 +00:00
|
|
|
{
|
2021-03-20 15:27:18 +00:00
|
|
|
$phpDocNode = $phpDocInfo->getPhpDocNode();
|
2021-08-08 00:26:40 +00:00
|
|
|
$phpDocNodeTraverser = new \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser();
|
2021-07-08 15:36:45 +00:00
|
|
|
$phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function ($node) use($desiredNode, $phpDocInfo) : ?int {
|
|
|
|
if ($node instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode && $node->value === $desiredNode) {
|
|
|
|
$phpDocInfo->markAsChanged();
|
2021-08-08 00:26:40 +00:00
|
|
|
return \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser::NODE_REMOVE;
|
2021-07-08 15:36:45 +00:00
|
|
|
}
|
2021-07-01 23:23:13 +00:00
|
|
|
if ($node !== $desiredNode) {
|
2021-07-08 15:36:45 +00:00
|
|
|
return null;
|
2020-07-28 23:41:20 +00:00
|
|
|
}
|
2021-01-19 15:03:26 +00:00
|
|
|
$phpDocInfo->markAsChanged();
|
2021-08-08 00:26:40 +00:00
|
|
|
return \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser::NODE_REMOVE;
|
2021-07-01 23:23:13 +00:00
|
|
|
});
|
2020-07-28 23:41:20 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function areAnnotationNamesEqual(string $firstAnnotationName, string $secondAnnotationName) : bool
|
2020-07-28 23:41:20 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$firstAnnotationName = \trim($firstAnnotationName, '@');
|
|
|
|
$secondAnnotationName = \trim($secondAnnotationName, '@');
|
2020-07-28 23:41:20 +00:00
|
|
|
return $firstAnnotationName === $secondAnnotationName;
|
|
|
|
}
|
|
|
|
}
|