2021-01-18 01:56:12 +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\PhpDoc\TagRemover;
|
2021-01-18 01:56:12 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\FunctionLike;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Node;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
2023-07-20 12:07:25 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2023-09-11 13:16:45 +00:00
|
|
|
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
|
2022-08-29 20:49:28 +00:00
|
|
|
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
|
2021-01-18 01:56:12 +00:00
|
|
|
final class ParamTagRemover
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer
|
2021-01-18 01:56:12 +00:00
|
|
|
*/
|
|
|
|
private $deadParamTagValueNodeAnalyzer;
|
2023-09-11 13:16:45 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
|
|
|
*/
|
|
|
|
private $docBlockUpdater;
|
|
|
|
public function __construct(DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer, DocBlockUpdater $docBlockUpdater)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-18 01:56:12 +00:00
|
|
|
$this->deadParamTagValueNodeAnalyzer = $deadParamTagValueNodeAnalyzer;
|
2023-09-11 13:16:45 +00:00
|
|
|
$this->docBlockUpdater = $docBlockUpdater;
|
2021-01-18 01:56:12 +00:00
|
|
|
}
|
2022-12-04 13:35:33 +00:00
|
|
|
public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $functionLike) : bool
|
2021-01-18 01:56:12 +00:00
|
|
|
{
|
2022-12-04 13:35:33 +00:00
|
|
|
$hasChanged = \false;
|
2022-06-07 08:22:29 +00:00
|
|
|
$phpDocNodeTraverser = new PhpDocNodeTraverser();
|
2023-09-11 13:16:45 +00:00
|
|
|
$phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function (Node $docNode) use($functionLike, &$hasChanged) : ?int {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$docNode instanceof PhpDocTagNode) {
|
2021-07-08 17:11:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$docNode->value instanceof ParamTagValueNode) {
|
2021-07-08 17:11:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// handle only basic types, keep phpstan/psalm helper ones
|
|
|
|
if ($docNode->name !== '@param') {
|
|
|
|
return null;
|
2021-01-18 01:56:12 +00:00
|
|
|
}
|
2021-07-08 17:11:02 +00:00
|
|
|
if (!$this->deadParamTagValueNodeAnalyzer->isDead($docNode->value, $functionLike)) {
|
|
|
|
return null;
|
2021-01-18 01:56:12 +00:00
|
|
|
}
|
2022-12-04 13:35:33 +00:00
|
|
|
$hasChanged = \true;
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpDocNodeTraverser::NODE_REMOVE;
|
2021-07-08 17:11:02 +00:00
|
|
|
});
|
2023-09-11 13:16:45 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
|
|
|
|
}
|
2022-12-04 13:35:33 +00:00
|
|
|
return $hasChanged;
|
2021-01-18 01:56:12 +00:00
|
|
|
}
|
|
|
|
}
|