Skip to content

Commit

Permalink
Remove superfluous comments (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar committed Sep 14, 2024
1 parent db22bf0 commit 1f15110
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5,533 deletions.
37 changes: 37 additions & 0 deletions src/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function enterNode(Node $node)
return null;
}

$node = $this->getCleanCommentsNode($node);

$docComment = $node->getDocComment();

if (! ($docComment instanceof Doc)) {
Expand Down Expand Up @@ -870,4 +872,39 @@ static function (Node $node): bool {
}
return '';
}

private function getCleanCommentsNode(Node $node): Node
{
if (count($node->getComments()) === 0) {
return $node;
}

// Remove "//" comments.
$comments = [];
foreach ($node->getComments() as $comment) {
if (strpos(trim($comment->getText()), '//') === 0) {
continue;
}
$comments[] = $comment;
}

$node->setAttribute('comments', $comments);

if ($node->getDocComment() === null) {
return $node;
}

// Remove file comments that are bound to the first node in a file.
$comments = $node->getComments();
if (
$comments[0]->getText() !== (string)$node->getDocComment()
&& strpos($comments[0]->getText(), '/**#@') !== 0
) {
array_shift($comments);
}

$node->setAttribute('comments', $comments);

return $node;
}
}
Loading

0 comments on commit 1f15110

Please sign in to comment.