Skip to content

Commit

Permalink
[DeadCode] Allow remove useless @var on typed class constant as well …
Browse files Browse the repository at this point in the history
…on RemoveUselessVarTagRector (#6633)

* [DeadCode] Allow remove useless @var on typed class constant as well on RemoveUselessVarTagRector

* grammar
  • Loading branch information
samsonasik authored Dec 28, 2024
1 parent 7bbe9ba commit dfe6f2d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

final class WithConstantType
{
/**
* @var string
*/
public const string NAME = 'value';
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;

final class WithConstantType
{
public const string NAME = 'value';
}

?>
3 changes: 2 additions & 1 deletion rules/DeadCode/PhpDoc/DeadVarTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions rules/DeadCode/Rector/Property/RemoveUselessVarTagRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit dfe6f2d

Please sign in to comment.