Skip to content

Commit 3e22610

Browse files
committed
fix extractor erasing comments when they precede strict_types
1 parent 67bab43 commit 3e22610

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

src/PhpGenerator/Extractor.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,29 @@ public function extractAll(): PhpFile
243243
{
244244
$phpFile = new PhpFile;
245245

246+
$comments = [];
247+
$firstStmt = $this->statements[0] ?? null;
248+
if ($firstStmt instanceof Node\Stmt\Declare_) {
249+
$comments = $firstStmt->getComments();
250+
if (!$firstStmt->getDocComment()) {
251+
$comments = [];
252+
$firstStmt = $this->statements[1] ?? null;
253+
}
254+
}
255+
246256
if (
247-
($firstStmt = $this->statements[0] ?? null)
248-
&& ($firstStmt = $firstStmt instanceof Node\Stmt\Declare_ ? $this->statements[1] ?? null : $firstStmt)
257+
!$comments
258+
&& $firstStmt
249259
&& !$firstStmt instanceof Node\Stmt\ClassLike
250260
&& !$firstStmt instanceof Node\Stmt\Function_
251261
) {
252262
$comments = $firstStmt->getComments();
253-
foreach ($comments as $i => $comment) {
254-
if ($comment instanceof PhpParser\Comment\Doc) {
255-
$phpFile->setComment(Helpers::unformatDocComment($comment->getReformattedText()));
256-
break;
257-
}
263+
}
264+
265+
foreach ($comments as $comment) {
266+
if ($comment instanceof PhpParser\Comment\Doc) {
267+
$phpFile->setComment(Helpers::unformatDocComment($comment->getReformattedText()));
268+
break;
258269
}
259270
}
260271

tests/PhpGenerator/Extractor.extractAll.file.comments.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ $file = (new Extractor(<<<'XX'
3030
Assert::same('doc comment', $file->getComment());
3131

3232

33+
$file = (new Extractor(<<<'XX'
34+
<?php
35+
36+
/** doc comment */
37+
38+
declare(strict_types=1);
39+
40+
namespace Abc;
41+
XX))->extractAll();
42+
43+
Assert::same('doc comment', $file->getComment());
44+
45+
3346
$file = (new Extractor(<<<'XX'
3447
<?php
3548

0 commit comments

Comments
 (0)