Skip to content

Commit 8ed5fd8

Browse files
committed
Extractor: supports file comment extraction with singleline declare
1 parent b5d813b commit 8ed5fd8

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/PhpGenerator/Extractor.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,18 @@ public function extractAll(): PhpFile
237237
$phpFile = new PhpFile;
238238

239239
if (
240-
$this->statements
241-
&& !$this->statements[0] instanceof Node\Stmt\ClassLike
242-
&& !$this->statements[0] instanceof Node\Stmt\Function_
240+
($firstStmt = $this->statements[0] ?? null)
241+
&& ($firstStmt = $firstStmt instanceof Node\Stmt\Declare_ ? $this->statements[1] ?? null : $firstStmt)
242+
&& !$firstStmt instanceof Node\Stmt\ClassLike
243+
&& !$firstStmt instanceof Node\Stmt\Function_
243244
) {
244-
$this->addCommentAndAttributes($phpFile, $this->statements[0]);
245+
$comments = $firstStmt->getComments();
246+
foreach ($comments as $i => $comment) {
247+
if ($comment instanceof PhpParser\Comment\Doc) {
248+
$phpFile->setComment(Helpers::unformatDocComment($comment->getReformattedText()));
249+
break;
250+
}
251+
}
245252
}
246253

247254
$namespaces = ['' => $this->statements];
@@ -437,7 +444,7 @@ private function addFunctionToFile(PhpFile $phpFile, Node\Stmt\Function_ $node):
437444

438445

439446
private function addCommentAndAttributes(
440-
PhpFile|ClassLike|Constant|Property|GlobalFunction|Method|Parameter|EnumCase|TraitUse|PropertyHook $element,
447+
ClassLike|Constant|Property|GlobalFunction|Method|Parameter|EnumCase|TraitUse|PropertyHook $element,
441448
Node $node,
442449
): void
443450
{

tests/PhpGenerator/ClassManipulator.implement.84.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ abstract class ParentAbstract
2626
abstract public array $abstractProperty { get; }
2727
public array $concreteProperty;
2828
abstract public function abstractMethod();
29-
public function concreteMethod() {}
29+
public function concreteMethod()
30+
{
31+
}
3032
}
3133

3234
abstract class TestAbstract extends ParentAbstract

tests/PhpGenerator/ClassType.from.85.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
use Nette\PhpGenerator\ClassType;
8-
use Nette\PhpGenerator\InterfaceType;
98

109
require __DIR__ . '/../bootstrap.php';
1110
require __DIR__ . '/fixtures/classes.85.php';

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ Assert::null($file->getComment());
1919
Assert::same('doc comment', $file->getClasses()['Class1']->getComment());
2020

2121

22+
$file = (new Extractor(<<<'XX'
23+
<?php declare(strict_types=1);
24+
25+
/** doc comment */
26+
27+
namespace Abc;
28+
XX))->extractAll();
29+
30+
Assert::same('doc comment', $file->getComment());
31+
32+
2233
$file = (new Extractor(<<<'XX'
2334
<?php
2435

tests/PhpGenerator/fixtures/classes.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/**
44
* First comment
55
*/
66

7-
declare(strict_types=1);
8-
97
/**
108
* Second comment
119
*/

0 commit comments

Comments
 (0)