This commit is contained in:
Nikita Popov 2016-02-28 20:28:32 +01:00
parent c8282e6e76
commit 9829bf69cd
2 changed files with 11 additions and 5 deletions

View File

@ -129,14 +129,11 @@ abstract class PrettyPrinterAbstract
return "<?php\n\n";
}
$p = $this->prettyPrint($stmts);
$p = "<?php\n\n" . $this->prettyPrint($stmts);
if ($stmts[0] instanceof Stmt\InlineHTML) {
$p = preg_replace('/^\?>\n?/', '', $p);
} else {
$p = "<?php\n\n" . $p;
$p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
}
if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
$p = preg_replace('/<\?php$/', '', rtrim($p));
}

View File

@ -2,6 +2,7 @@
namespace PhpParser;
use PhpParser\Comment;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
@ -87,6 +88,14 @@ class PrettyPrinterTest extends CodeTestAbstract
$this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
}
public function testCommentBeforeInlineHTML() {
$prettyPrinter = new PrettyPrinter\Standard;
$comment = new Comment\Doc("/**\n * This is a comment\n */");
$stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
$expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
$this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
}
private function parseModeLine($modeLine) {
$parts = explode(' ', $modeLine, 2);
$version = isset($parts[0]) ? $parts[0] : 'both';