Skip to content

Commit 45c7dfa

Browse files
committed
Fix
1 parent b15dcfe commit 45c7dfa

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

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)