Skip to content

Commit

Permalink
Extractor::extractAll() fixed file comment parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 27, 2024
1 parent 250bf02 commit 83968a1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ public function enterNode(Node $node)
}
};

if ($this->statements) {
if (
$this->statements
&& !$this->statements[0] instanceof Node\Stmt\ClassLike
&& !$this->statements[0] instanceof Node\Stmt\Function_
) {
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/PhpGenerator/Extractor.extractAll.file.comments.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\Extractor;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


$file = (new Extractor(<<<'XX'
<?php
/** doc comment */
class Class1
{
}

XX))->extractAll();

Assert::null($file->getComment());
Assert::same('doc comment', $file->getClasses()['Class1']->getComment());


$file = (new Extractor(<<<'XX'
<?php
/** doc comment */
namespace Abc;
XX))->extractAll();

Assert::same('doc comment', $file->getComment());


$file = (new Extractor(<<<'XX'
<?php
#[ExampleAttribute]
function () {};
XX))->extractAll();

Assert::null($file->getComment());

0 comments on commit 83968a1

Please sign in to comment.