Skip to content

Commit

Permalink
Force sorting predictability on PHP 5 vs 7
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 19, 2016
1 parent 74203bf commit 5b9df1e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
@@ -1,3 +1,4 @@
sudo: false
language: php

php:
Expand All @@ -8,12 +9,9 @@ php:
- 7.0
- hhvm

matrix:
allow_failures:
- php: 7.0
- php: hhvm

sudo: false
branches:
only:
- master

install:
- composer install
Expand Down
45 changes: 42 additions & 3 deletions src/JSqueeze.php
Expand Up @@ -830,7 +830,7 @@ protected function renameVars(&$tree, $root)
else if (2 == strlen($k)) $tree['used'][] = $k[1];
}

arsort($this->charFreq);
$this->charFreq = $this->rsort($this->charFreq);

$this->str0 = '';
$this->str1 = '';
Expand Down Expand Up @@ -868,7 +868,7 @@ protected function renameVars(&$tree, $root)
if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) $tree['local'][$var] = $tree['local'][substr($var, 1)];
}

arsort($tree['local']);
$tree['local'] = $this->rsort($tree['local']);

foreach ($tree['local'] as $var => $root) switch (substr($var, 0, 1))
{
Expand All @@ -892,7 +892,7 @@ protected function renameVars(&$tree, $root)
}
else
{
arsort($tree['local']);
$tree['local'] = $this->rsort($tree['local']);
if (false !== $tree['nfe']) $tree['used'][] = $tree['local'][$tree['nfe']];

foreach ($tree['local'] as $var => $root)
Expand Down Expand Up @@ -1001,4 +1001,43 @@ protected function restoreCc(&$s, $lf = true)
$s = str_replace('1#@', '/*@', $s);
$s = str_replace('##', '#', $s);
}

private function rsort($array)
{
if (!$array) {
return $array;
}

$i = 0;
$tuples = array();
foreach ($array as $k => &$v)
{
$tuples[] = array(++$i, $k, &$v);
}

usort($tuples, function ($a, $b) {
if ($b[2] > $a[2]) {
return 1;
}
if ($b[2] < $a[2]) {
return -1;
}
if ($b[0] > $a[0]) {
return -1;
}
if ($b[0] < $a[0]) {
return 1;
}

return 0;
});

$array = array();

foreach ($tuples as $t) {
$array[$t[1]] = &$t[2];
}

return $array;
}
}
2 changes: 1 addition & 1 deletion tests/expected/issue372.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/expected/issue50.js
@@ -1 +1 @@
;function bar(t){try{foo()}catch(o){alert('Exception caught (foo not defined) }else{')};alert(t)};bar(10);
;function bar(t){try{foo()}catch(e){alert('Exception caught (foo not defined) }else{')};alert(t)};bar(10);
2 changes: 1 addition & 1 deletion tests/expected/issues222_324.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/expected/mangle.js
@@ -1 +1 @@
(function(){var u=function n(r,n,u){return n};u=fun}());
(function(){var u=function n(u,n,f){return n};u=fun}());
2 changes: 1 addition & 1 deletion tests/expected/tchwork-24.js
@@ -1 +1 @@
;function(){var u,n;u++;n};
;function(){var n,a;n++;a};

0 comments on commit 5b9df1e

Please sign in to comment.