File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -30,6 +30,19 @@ $file = (new Extractor(<<<'XX'
3030Assert::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
You can’t perform that action at this time.
0 commit comments