public static function php_check_syntax($code, &$error = null)
{
$b = 0;
foreach (token_get_all($code) as $token) {
if (is_array($token)) {
$token = token_name($token[0]);
}
switch ($token) {
case 'T_CURLY_OPEN':
case 'T_DOLLAR_OPEN_CURLY_BRACES':
case 'T_CURLY_OPENT_VARIABLE':
// This is not documented in the manual. (11.05.07)
// This is not documented in the manual. (11.05.07)
case '{':
++$b;
break;
case '}':
--$b;
break;
}
}
if ($b) {
$error = _t('Unbalanced braces.');
return false;
// Unbalanced braces would break the eval below
} else {
ob_start();
// Catch potential parse error messages
if (function_exists('xdebug_disable')) {
xdebug_disable();
}
$display_errors = ini_set('display_errors', 'on');
// Make sure we have something to catch
$html_errors = ini_set('html_errors', 'off');
$error_reporting = error_reporting(E_ALL ^ E_NOTICE);
$code = eval("namespace HabariSandBox; return true; \n" . $code . "\n");
// Put $code in a dead code sandbox to prevent its execution
ini_set('display_errors', $display_errors);
// be a good citizen
ini_set('html_errors', $html_errors);
error_reporting($error_reporting);
$error = ob_get_clean();
return false !== $code;
}
}