From dfe6f2d43c20b7883ad5354d59c859f3ad85cae4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 29 Dec 2024 03:19:31 +0700 Subject: [PATCH] [DeadCode] Allow remove useless @var on typed class constant as well on RemoveUselessVarTagRector (#6633) * [DeadCode] Allow remove useless @var on typed class constant as well on RemoveUselessVarTagRector * grammar --- .../Fixture/with_constant_type.php.inc | 24 +++++++++++++++++++ .../PhpDoc/DeadVarTagValueNodeAnalyzer.php | 3 ++- .../PhpDoc/TagRemover/VarTagRemover.php | 3 ++- .../Property/RemoveUselessVarTagRector.php | 7 +++--- 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/Property/RemoveUselessVarTagRector/Fixture/with_constant_type.php.inc diff --git a/rules-tests/DeadCode/Rector/Property/RemoveUselessVarTagRector/Fixture/with_constant_type.php.inc b/rules-tests/DeadCode/Rector/Property/RemoveUselessVarTagRector/Fixture/with_constant_type.php.inc new file mode 100644 index 00000000000..eafdcff6a93 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Property/RemoveUselessVarTagRector/Fixture/with_constant_type.php.inc @@ -0,0 +1,24 @@ + +----- + diff --git a/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php b/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php index da6ddde57f1..5a0c2120a7f 100644 --- a/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php +++ b/rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\PhpDoc; use PhpParser\Node; +use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; use PHPStan\Type\IntersectionType; @@ -24,7 +25,7 @@ public function __construct( ) { } - public function isDead(VarTagValueNode $varTagValueNode, Property $property): bool + public function isDead(VarTagValueNode $varTagValueNode, Property|ClassConst $property): bool { if (! $property->type instanceof Node) { return false; diff --git a/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php b/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php index ec31398b8e8..929b496cd77 100644 --- a/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php +++ b/rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Param; +use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; @@ -31,7 +32,7 @@ public function __construct( ) { } - public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property $property): bool + public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassConst $property): bool { $varTagValueNode = $phpDocInfo->getVarTagValueNode(); if (! $varTagValueNode instanceof VarTagValueNode) { diff --git a/rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php b/rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php index 1da68d024c7..3d3110eb7c7 100644 --- a/rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php +++ b/rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\Property; use PhpParser\Node; +use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\Property; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; @@ -25,7 +26,7 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Remove unused @var annotation for properties', [ + return new RuleDefinition('Remove unused @var annotation for properties and class constants', [ new CodeSample( <<<'CODE_SAMPLE' final class SomeClass @@ -53,11 +54,11 @@ final class SomeClass */ public function getNodeTypes(): array { - return [Property::class]; + return [Property::class, ClassConst::class]; } /** - * @param Property $node + * @param Property|ClassConst $node */ public function refactor(Node $node): ?Node {