]> git.joonet.de Git - adminer.git/commitdiff
Compile: Add more php_shrink tests
authorJakub Vrana <jakub@vrana.cz>
Wed, 12 Mar 2025 22:32:34 +0000 (23:32 +0100)
committerJakub Vrana <jakub@vrana.cz>
Thu, 13 Mar 2025 05:10:58 +0000 (06:10 +0100)
php_shrink.inc.php
tests/php_shrink.php

index f13c2f95078d09ce0cf4cd4dbce9fa91f37ef8d9..f127493df4f8831d94fad4bd0319d8715dc04775 100644 (file)
@@ -1,7 +1,18 @@
 <?php
 
-// based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
+/** Minify PHP code with these operations:
+* remove extra {}
+* minify variables
+* strip comments, preserve only the first doc-comment
+* join consecutive echo
+* change ?>HTML<?php to echo 'HTML' if it saves space
+* strip public visibility or change it to var
+*
+* @param string PHP code including <?php
+* @return string
+*/
 function php_shrink($input) {
+       // based on http://latrine.dgx.cz/jak-zredukovat-php-skripty
        $special_variables = array_flip(array('$this', '$GLOBALS', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_SERVER', '$http_response_header', '$php_errormsg'));
        $short_variables = array();
        $shortening = true;
index 5ec303e1b18f5f4d5d1fce0e6bd82c0fc4e1c6b8..38faff32b311e10c5f6dbd0060e198ad5fd20954 100644 (file)
@@ -4,13 +4,15 @@ include __DIR__ . "/../php_shrink.inc.php";
 
 function check($code, $expected) {
        $shrinked = php_shrink("<?php\n$code");
-       if ("<?php\n" . str_replace(" ", "\n", $expected) . "" != $shrinked) {
+       if ("<?php\n" . preg_replace('~([^*]) ~', "\\1\n", $expected) . "" != $shrinked) {
                $backtrace = reset(debug_backtrace());
                echo "$backtrace[file]:$backtrace[line]:" . str_replace("\n", " ", substr($shrinked, 6)) . "\n";
        }
 }
 
-check('$ab = 1;', '$a=1;');
+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);');
 check('function f($ab, $cd = 1) { return $ab; }', 'function f($a,$b=1){return$a;}');
 check('class C { var $ab = 1; }', 'class C{var$ab=1;}');
 check('class C { public $ab = 1; }', 'class C{var$ab=1;}');
@@ -20,4 +22,14 @@ check('class C { private $ab = 1; }', 'class C{private$ab=1;}');
 check('class C { private function f($ab) { return $ab; }}', 'class C{private function f($a){return$a;}}');
 check('class C { public function f($ab) { return $ab; }}', 'class C{function f($a){return$a;}}');
 check('class C { private static $ab; }', 'class C{private static$ab;}');
+check('class C { const AB = 1; }', 'class C{const AB=1;}');
+check('class C { private const AB = 1; }', 'class C{private const AB=1;}');
+check('class C { public $ab; function f($cd) { return $cd . $this->ab; }}', 'class C{var$ab;function f($b){return$b.$this->ab;}}');
+check('namespace NS { class C { public $ab = 1; } } new NS\C; $ab = 2;', 'namespace NS{class C{var$ab=1;}}new NS\C;$a=2;');
 check('new \stdClass;', 'new \stdClass;');
+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; ?>2<?php echo 3;', "echo 1,'2',3;");
+check('/** preserve*/ $a; /** ignore */ /* also ignore */ // ignore too', '/** preserve*/$a;');