1
0
mirror of https://github.com/vrana/adminer.git synced 2025-08-07 23:27:17 +02:00

Compile: Add more php_shrink tests

This commit is contained in:
Jakub Vrana
2025-03-12 23:32:34 +01:00
parent 818b9ad903
commit b9b4db0c8e
2 changed files with 26 additions and 3 deletions

View 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;

View 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;');