Skip to content

Commit 83968a1

Browse files
committed
Extractor::extractAll() fixed file comment parsing
1 parent 250bf02 commit 83968a1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ public function enterNode(Node $node)
209209
}
210210
};
211211

212-
if ($this->statements) {
212+
if (
213+
$this->statements
214+
&& !$this->statements[0] instanceof Node\Stmt\ClassLike
215+
&& !$this->statements[0] instanceof Node\Stmt\Function_
216+
) {
213217
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
214218
}
215219

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Extractor;
6+
use Tester\Assert;
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
$file = (new Extractor(<<<'XX'
11+
<?php
12+
13+
/** doc comment */
14+
class Class1
15+
{
16+
}
17+
18+
XX))->extractAll();
19+
20+
Assert::null($file->getComment());
21+
Assert::same('doc comment', $file->getClasses()['Class1']->getComment());
22+
23+
24+
$file = (new Extractor(<<<'XX'
25+
<?php
26+
27+
/** doc comment */
28+
29+
namespace Abc;
30+
XX))->extractAll();
31+
32+
Assert::same('doc comment', $file->getComment());
33+
34+
35+
$file = (new Extractor(<<<'XX'
36+
<?php
37+
38+
#[ExampleAttribute]
39+
40+
function () {};
41+
XX))->extractAll();
42+
43+
Assert::null($file->getComment());

0 commit comments

Comments
 (0)