Skip to content

Commit ad7d0a7

Browse files
committed
Extractor: preserves the first comment in the file [Closes #100]
1 parent 2a09a7a commit ad7d0a7

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ public function enterNode(Node $node)
208208
}
209209
};
210210

211+
if ($this->statements) {
212+
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
213+
}
214+
211215
$traverser = new PhpParser\NodeTraverser;
212216
$traverser->addVisitor($visitor);
213217
$traverser->traverse($this->statements);
@@ -367,6 +371,7 @@ private function addCommentAndAttributes($element, Node $node): void
367371
$comment = $node->getDocComment()->getReformattedText();
368372
$comment = Helpers::unformatDocComment($comment);
369373
$element->setComment($comment);
374+
$node->setDocComment(new PhpParser\Comment\Doc(''));
370375
}
371376

372377
foreach ($node->attrGroups ?? [] as $group) {

tests/PhpGenerator/expected/Extractor.classes.expect

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
/**
4+
* First comment
5+
*/
6+
37
declare(strict_types=1);
48

59
namespace Abc;

tests/PhpGenerator/fixtures/classes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
22

3+
/**
4+
* First comment
5+
*/
6+
37
declare(strict_types=1);
48

9+
/**
10+
* Second comment
11+
*/
12+
513
namespace Abc;
614

715
/**

0 commit comments

Comments
 (0)