From a72e0535208b1e47d7c3e8863533d17707b613a8 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sat, 15 Mar 2025 07:35:37 +0100 Subject: [PATCH] php_shrink: Use foreach --- php_shrink.inc.php | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/php_shrink.inc.php b/php_shrink.inc.php index 875c4614..c4e79cfe 100644 --- a/php_shrink.inc.php +++ b/php_shrink.inc.php @@ -50,6 +50,9 @@ function php_shrink($input) { if ($token[0] === T_VARIABLE && !isset($special_variables[$token[1]])) { $short_variables[$token[1]]++; } + } + + foreach ($tokens as $i => $token) { if ( $tokens[$i+2][0] === T_CLOSE_TAG && $tokens[$i+3][0] === T_INLINE_HTML && $tokens[$i+4][0] === T_OPEN_TAG && strlen(add_apo_slashes($tokens[$i+3][1])) < strlen($tokens[$i+3][1]) + 3 @@ -76,7 +79,11 @@ function php_shrink($input) { $output = ''; $in_echo = false; $doc_comment = false; // include only first /** - for (reset($tokens); list($i, $token) = each($tokens);) { + $next_pos = 0; + foreach ($tokens as $i => $token) { + if ($i < $next_pos) { + continue; + } if (!is_array($token)) { $token = array(0, $token); } @@ -99,12 +106,9 @@ function php_shrink($input) { $in_echo = true; } elseif ($token[1] == ';' && $in_echo) { $next_echo = next_token($tokens, $i, T_ECHO, array(T_WHITESPACE, T_COMMENT)); - for (; $i < $next_echo - 1; $i++) { - next($tokens); - } if ($next_echo) { // join two consecutive echos - next($tokens); + $next_pos = $next_echo + 1; $token[1] = ','; // '.' would conflict with "a".1+2 and would use more memory //! remove ',' and "," but not $var"," } else { $in_echo = false; @@ -140,11 +144,3 @@ function short_identifier($number, $chars) { function add_apo_slashes($s) { return addcslashes($s, "\\'"); } - -if (!function_exists("each")) { - function each(&$arr) { - $key = key($arr); - next($arr); - return $key === null ? false : array($key, $arr[$key]); - } -}