Skip to content

Commit

Permalink
Fix __halt_compiler() pretty printing edge case
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
nikic committed Feb 20, 2016
1 parent 47509cf commit 1fe8f09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,22 @@ public function prettyPrintExpr(Expr $node) {
* @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;
}

Expand Down
27 changes: 27 additions & 0 deletions test/code/prettyPrinter/stmt/haltCompiler.file-test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1fe8f09

Please sign in to comment.