]> git.joonet.de Git - adminer.git/commitdiff
php_shrink: Join echos interleaved with comments
authorJakub Vrana <jakub@vrana.cz>
Sat, 15 Mar 2025 06:20:12 +0000 (07:20 +0100)
committerJakub Vrana <jakub@vrana.cz>
Sat, 15 Mar 2025 06:21:37 +0000 (07:21 +0100)
php_shrink.inc.php
tests/php_shrink.php

index fe3b1c1834329d32934b7093751aa3752181ae60..a15ffd0f3c5a1e49fd9fd25303e60fb09873ff92 100644 (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) {
index bc917e750ed940bd95764e2d7803156f8327ae77..41f4cb9cc0468dc7cbd63205460e2ce0c5478494 100644 (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;');