2020-11-13 13:58:17 +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\ClassMethod;
|
2020-11-13 13:58:17 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-12-06 10:48:40 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2022-06-06 17:12:56 +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\DeadCode\PhpDoc\TagRemover\ParamTagRemover;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-11-13 13:58:17 +00:00
|
|
|
/**
|
2021-03-21 18:41:17 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\RemoveUselessParamTagRectorTest
|
2020-11-13 13:58:17 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveUselessParamTagRector extends AbstractRector
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-07-08 17:11:02 +00:00
|
|
|
* @var \Rector\DeadCode\PhpDoc\TagRemover\ParamTagRemover
|
2020-11-13 13:58:17 +00:00
|
|
|
*/
|
2021-07-08 17:11:02 +00:00
|
|
|
private $paramTagRemover;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(ParamTagRemover $paramTagRemover, PhpDocInfoFactory $phpDocInfoFactory)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-07-08 17:11:02 +00:00
|
|
|
$this->paramTagRemover = $paramTagRemover;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2020-12-04 23:22:12 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove @param docblock with same type as parameter type', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-11-13 13:58:17 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $a
|
|
|
|
* @param string $b description
|
|
|
|
*/
|
2020-12-05 00:31:05 +00:00
|
|
|
public function foo(string $a, string $b)
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-11-13 13:58:17 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $b description
|
|
|
|
*/
|
2020-12-05 00:31:05 +00:00
|
|
|
public function foo(string $a, string $b)
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-11-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-11-13 13:58:17 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
2023-12-06 10:48:40 +00:00
|
|
|
return [ClassMethod::class, Function_::class];
|
2020-11-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-12-06 10:48:40 +00:00
|
|
|
* @param ClassMethod|Function_ $node
|
2020-11-13 13:58:17 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-11-13 13:58:17 +00:00
|
|
|
{
|
2021-07-08 17:11:02 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$phpDocInfo instanceof PhpDocInfo) {
|
2021-07-08 17:11:02 +00:00
|
|
|
return null;
|
2020-11-13 13:58:17 +00:00
|
|
|
}
|
2022-12-04 13:35:33 +00:00
|
|
|
$hasChanged = $this->paramTagRemover->removeParamTagsIfUseless($phpDocInfo, $node);
|
|
|
|
if ($hasChanged) {
|
2021-01-19 19:45:30 +00:00
|
|
|
return $node;
|
2020-11-18 10:02:10 +00:00
|
|
|
}
|
2021-01-19 19:45:30 +00:00
|
|
|
return null;
|
2020-11-18 10:02:10 +00:00
|
|
|
}
|
2020-11-13 13:58:17 +00:00
|
|
|
}
|