Skip to content

Commit 1f15110

Browse files
authored
Remove superfluous comments (#222)
1 parent db22bf0 commit 1f15110

File tree

2 files changed

+37
-5533
lines changed

2 files changed

+37
-5533
lines changed

src/Visitor.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public function enterNode(Node $node)
7373
return null;
7474
}
7575

76+
$node = $this->getCleanCommentsNode($node);
77+
7678
$docComment = $node->getDocComment();
7779

7880
if (! ($docComment instanceof Doc)) {
@@ -870,4 +872,39 @@ static function (Node $node): bool {
870872
}
871873
return '';
872874
}
875+
876+
private function getCleanCommentsNode(Node $node): Node
877+
{
878+
if (count($node->getComments()) === 0) {
879+
return $node;
880+
}
881+
882+
// Remove "//" comments.
883+
$comments = [];
884+
foreach ($node->getComments() as $comment) {
885+
if (strpos(trim($comment->getText()), '//') === 0) {
886+
continue;
887+
}
888+
$comments[] = $comment;
889+
}
890+
891+
$node->setAttribute('comments', $comments);
892+
893+
if ($node->getDocComment() === null) {
894+
return $node;
895+
}
896+
897+
// Remove file comments that are bound to the first node in a file.
898+
$comments = $node->getComments();
899+
if (
900+
$comments[0]->getText() !== (string)$node->getDocComment()
901+
&& strpos($comments[0]->getText(), '/**#@') !== 0
902+
) {
903+
array_shift($comments);
904+
}
905+
906+
$node->setAttribute('comments', $comments);
907+
908+
return $node;
909+
}
873910
}

0 commit comments

Comments
 (0)