Fix __halt_compiler() pretty printing edge case

We can't strip the <?php at the end of a __halt_compiler() segment
in file mode.

Fixed by being a bit more explicit in prettyPrintFile() about what
we want to do...
This commit is contained in:
Nikita Popov 2016-02-20 18:23:52 +01:00
parent 47509cf927
commit 1fe8f09caa
2 changed files with 38 additions and 4 deletions

View File

@ -125,15 +125,22 @@ abstract class PrettyPrinterAbstract
* @return string Pretty printed statements
*/
public function prettyPrintFile(array $stmts) {
$p = rtrim($this->prettyPrint($stmts));
if (!$stmts) {
return "<?php\n\n";
}
$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
$p = preg_replace('/<\?php$/', '', $p);
$p = $this->prettyPrint($stmts);
if (!$count) {
if ($stmts[0] instanceof Stmt\InlineHTML) {
$p = preg_replace('/^\?>\n?/', '', $p);
} else {
$p = "<?php\n\n" . $p;
}
if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
$p = preg_replace('/<\?php$/', '', rtrim($p));
}
return $p;
}

View File

@ -0,0 +1,27 @@
__halt_compiler
-----
<?php
echo 'foo';
__halt_compiler();
!!!
???
-----
<?php
echo 'foo';
__halt_compiler();
!!!
???
-----
<?php
echo 'foo';
__halt_compiler();
<?php
-----
<?php
echo 'foo';
__halt_compiler();
<?php