Skip to content

Commit 142f1c8

Browse files
authored
[DeadCode] Handle only remove 1 @var on multi vars on RemoveUselessVarTagRector (#7884)
* [DeadCode] Handle only remove 1 @var on multi vars on RemoveUselessVarTagRector * Fix
1 parent e65ac69 commit 142f1c8

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;
4+
5+
class MyClxProductNet_Translate {
6+
/**
7+
* @phpstan-pure
8+
*/
9+
public static function create(): self
10+
{
11+
return new self();
12+
}
13+
}
14+
15+
/** @var MyActionController $this */
16+
/** @var MyClxProductNet_Translate $translator */
17+
$translator = MyClxProductNet_Translate::create();
18+
$url = $this->bulkConsumerRegistration ?? "abc";
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\Fixture;
25+
26+
class MyClxProductNet_Translate {
27+
/**
28+
* @phpstan-pure
29+
*/
30+
public static function create(): self
31+
{
32+
return new self();
33+
}
34+
}
35+
36+
/** @var MyActionController $this */
37+
$translator = MyClxProductNet_Translate::create();
38+
$url = $this->bulkConsumerRegistration ?? "abc";
39+
40+
?>

rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public function __construct(
3232
) {
3333
}
3434

35-
public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassConst|Expression $property): bool
35+
public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassConst|Expression $node): bool
3636
{
3737
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
3838
if (! $varTagValueNode instanceof VarTagValueNode) {
3939
return false;
4040
}
4141

42-
$isVarTagValueDead = $this->deadVarTagValueNodeAnalyzer->isDead($varTagValueNode, $property);
42+
$isVarTagValueDead = $this->deadVarTagValueNodeAnalyzer->isDead($varTagValueNode, $node);
4343
if (! $isVarTagValueDead) {
4444
return false;
4545
}
@@ -48,8 +48,8 @@ public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassCons
4848
return false;
4949
}
5050

51-
$phpDocInfo->removeByType(VarTagValueNode::class);
52-
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);
51+
$phpDocInfo->removeByType(VarTagValueNode::class, $varTagValueNode->variableName);
52+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
5353

5454
return true;
5555
}

src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ public function removeByType(string $typeToRemove, ?string $name = null): bool
254254
{
255255
$hasChanged = false;
256256

257+
if ($name === '') {
258+
$name = null;
259+
}
260+
257261
$phpDocNodeTraverser = new PhpDocNodeTraverser();
258262
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use (
259263
$typeToRemove,

src/Comments/NodeDocBlock/DocBlockUpdater.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,26 @@ public function updateRefactoredNodeWithPhpDocInfo(Node $node): void
4343

4444
private function setCommentsAttribute(Node $node): void
4545
{
46-
$comments = array_filter($node->getComments(), static fn (Comment $comment): bool => ! $comment instanceof Doc);
46+
$docComment = $node->getDocComment();
47+
$docCommentText = $docComment instanceof Doc ? $docComment->getText() : null;
48+
49+
$comments = array_filter(
50+
$node->getComments(),
51+
static function (Comment $comment) use ($docCommentText): bool {
52+
if (! $comment instanceof Doc) {
53+
return true;
54+
}
55+
56+
// remove only the docblock that belongs to the node itself;
57+
// keep other preceding docblocks (possible with multiple @var docblocks before a statement)
58+
if ($docCommentText !== null && $comment->getText() === $docCommentText) {
59+
return false;
60+
}
61+
62+
return true;
63+
}
64+
);
65+
4766
$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
4867
}
4968

0 commit comments

Comments
 (0)