Fix __halt_compiler() usage in namespace

This fixes the only left bug that was reported by parsing the PHP testsuite :)
This commit is contained in:
nikic 2011-12-07 18:36:38 +01:00
parent 8bdb478785
commit df691065a6
3 changed files with 31 additions and 4 deletions

View File

@ -132,7 +132,7 @@ class PHPParser_Lexer
$this->pos = count($this->tokens);
// return with (); removed
return substr($textAfter, strlen($matches[0]));
return (string) substr($textAfter, strlen($matches[0])); // (string) converts false to ''
}
/**

View File

@ -101,17 +101,20 @@ class PHPParser_Node_Stmt_Namespace extends PHPParser_Node_Stmt
// iterate over all following namespaces
for ($i = 0, $c = count($nsOffsets); $i < $c; ++$i) {
$nsStmt = $stmts[$nsOffsets[$i]];
$newStmts[] = $nsStmt = $stmts[$nsOffsets[$i]];
// the last namespace takes all statements after it
if ($c === $i + 1) {
$nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1);
// if the last statement is __halt_compiler() put it outside the namespace
if (end($nsStmt->stmts) instanceof PHPParser_Node_Stmt_HaltCompiler) {
$newStmts[] = array_pop($nsStmt->stmts);
}
// and all the others take all statements between the current and the following one
} else {
$nsStmt->stmts = array_slice($stmts, $nsOffsets[$i] + 1, $nsOffsets[$i + 1] - $nsOffsets[$i] - 1);
}
$newStmts[] = $nsStmt;
}
return $newStmts;

View File

@ -28,4 +28,28 @@ array(
1: Stmt_HaltCompiler(
remaining: Hallo World!
)
)
-----
<?php
namespace A;
$a;
__halt_compiler();
-----
array(
0: Stmt_Namespace(
name: Name(
parts: array(
0: A
)
)
stmts: array(
0: Expr_Variable(
name: a
)
)
)
1: Stmt_HaltCompiler(
remaining:
)
)