1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-06 14:46:36 +02:00

php_shrink: Join echos interleaved with comments

This commit is contained in:
Jakub Vrana
2025-03-15 07:20:12 +01:00
parent 1fd8aa885b
commit 9c1d5484a2
2 changed files with 10 additions and 6 deletions

View File

@@ -98,11 +98,11 @@ function php_shrink($input) {
} elseif ($token[0] == T_ECHO) {
$in_echo = true;
} elseif ($token[1] == ';' && $in_echo) {
if ($tokens[$i+1][0] === T_WHITESPACE && $tokens[$i+2][0] === T_ECHO) {
$next_echo = next_token($tokens, $i, T_ECHO, array(T_WHITESPACE, T_COMMENT));
for (; $i < $next_echo - 1; $i++) {
next($tokens);
$i++;
}
if ($tokens[$i+1][0] === T_ECHO) {
if ($next_echo) {
// join two consecutive echos
next($tokens);
$token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var","
@@ -122,6 +122,12 @@ function php_shrink($input) {
return $output;
}
function next_token($tokens, $i, $search, $allowed = array()) {
for ($i += 1; in_array($tokens[$i][0], $allowed); $i++) {
}
return ($tokens[$i][0] === $search ? $i : 0);
}
function short_identifier($number, $chars) {
$return = '';
while ($number >= 0) {

View File

@@ -13,9 +13,6 @@ function check($code, $expected) {
//! bugs:
check('{if (true) {} echo 1;}', '{if(true);echo 1;}');
//! inefficiencies:
check("echo 1; //\necho 2;", 'echo 1,2;');
check('$ab = 1; echo $ab;', '$a=1;echo$a;');
check('$ab = 1; $cd = 2;', '$a=1;$b=2;');
check('define("AB", 1);', 'define("AB",1);');
@@ -37,6 +34,7 @@ check('if (true) { echo "a"; } else { echo "b"; }', 'if(true)echo"a";else echo"b
check('echo $_GET["a"];', 'echo$_GET["a"];');
check('$ab = 1; echo "$ab";', '$a=1;echo"$a";');
check('echo 1; echo 3;', 'echo 1,3;');
check('echo 1; /**/ echo 2;', 'echo 1,2;');
check('echo 1; ?>2<?php echo 3;', "echo 1,'2',3;");
check('/** preserve */ $a; /** ignore */ /* also ignore */ // ignore too', '/** preserve */$a;');
check('$a = 1; ?><?php ?><?php $a = 2;', '$a=1;$a=2;');