mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
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:
parent
47509cf927
commit
1fe8f09caa
@ -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;
|
||||
}
|
||||
|
||||
|
27
test/code/prettyPrinter/stmt/haltCompiler.file-test
Normal file
27
test/code/prettyPrinter/stmt/haltCompiler.file-test
Normal 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
|
Loading…
Reference in New Issue
Block a user