Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

class MyClxProductNet_Translate {
/**
* @phpstan-pure
*/
public static function create(): self
{
return new self();
}
}

/** @var MyActionController $this */
/** @var MyClxProductNet_Translate $translator */
$translator = MyClxProductNet_Translate::create();
$url = $this->bulkConsumerRegistration ?? "abc";

?>
-----
<?php

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

class MyClxProductNet_Translate {
/**
* @phpstan-pure
*/
public static function create(): self
{
return new self();
}
}

/** @var MyActionController $this */
$translator = MyClxProductNet_Translate::create();
$url = $this->bulkConsumerRegistration ?? "abc";

?>
8 changes: 4 additions & 4 deletions rules/DeadCode/PhpDoc/TagRemover/VarTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function __construct(
) {
}

public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassConst|Expression $property): bool
public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassConst|Expression $node): bool
{
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return false;
}

$isVarTagValueDead = $this->deadVarTagValueNodeAnalyzer->isDead($varTagValueNode, $property);
$isVarTagValueDead = $this->deadVarTagValueNodeAnalyzer->isDead($varTagValueNode, $node);
if (! $isVarTagValueDead) {
return false;
}
Expand All @@ -48,8 +48,8 @@ public function removeVarTagIfUseless(PhpDocInfo $phpDocInfo, Property|ClassCons
return false;
}

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

return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ public function removeByType(string $typeToRemove, ?string $name = null): bool
{
$hasChanged = false;

if ($name === '') {
$name = null;
}

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use (
$typeToRemove,
Expand Down
21 changes: 20 additions & 1 deletion src/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,26 @@ public function updateRefactoredNodeWithPhpDocInfo(Node $node): void

private function setCommentsAttribute(Node $node): void
{
$comments = array_filter($node->getComments(), static fn (Comment $comment): bool => ! $comment instanceof Doc);
$docComment = $node->getDocComment();
$docCommentText = $docComment instanceof Doc ? $docComment->getText() : null;

$comments = array_filter(
$node->getComments(),
static function (Comment $comment) use ($docCommentText): bool {
if (! $comment instanceof Doc) {
return true;
}

// remove only the docblock that belongs to the node itself;
// keep other preceding docblocks (possible with multiple @var docblocks before a statement)
if ($docCommentText !== null && $comment->getText() === $docCommentText) {
return false;
}

return true;
}
);

$node->setAttribute(AttributeKey::COMMENTS, array_values($comments));
}

Expand Down