You are here

protected function JSqueeze::renameVars in Advanced CSS/JS Aggregation 8.2

Same name and namespace in other branches
  1. 8.4 advagg_js_minify/jsqueeze.inc \Patchwork\JSqueeze::renameVars()
  2. 8.3 advagg_js_minify/jsqueeze.inc \Patchwork\JSqueeze::renameVars()
  3. 7.2 advagg_js_compress/jsqueeze.inc \Patchwork\JSqueeze::renameVars()
1 call to JSqueeze::renameVars()
JSqueeze::squeeze in advagg_js_minify/jsqueeze.inc
Squeezes a JavaScript source code.

File

advagg_js_minify/jsqueeze.inc, line 808

Class

JSqueeze

Namespace

Patchwork

Code

protected function renameVars(&$tree, $root) {
  if ($root) {
    $tree['local'] += $tree['used'];
    $tree['used'] = array();
    foreach ($tree['local'] as $k => $v) {
      if ('.' == $k[0]) {
        $k = substr($k, 1);
      }
      if ('true' === $k) {
        $this->charFreq[48] += $v;
      }
      else {
        if ('false' === $k) {
          $this->charFreq[49] += $v;
        }
        else {
          if (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}\$#", $k)) {
            foreach (count_chars($k, 1) as $k => $w) {
              $this->charFreq[$k] += $w * $v;
            }
          }
          else {
            if (2 == strlen($k)) {
              $tree['used'][] = $k[1];
            }
          }
        }
      }
    }
    arsort($this->charFreq);
    $this->str0 = '';
    $this->str1 = '';
    foreach ($this->charFreq as $k => $v) {
      if (!$v) {
        break;
      }
      $v = chr($k);
      if (64 < $k && $k < 91 || 96 < $k && $k < 123) {

        // A-Z a-z
        $this->str0 .= $v;
        $this->str1 .= $v;
      }
      else {
        if (47 < $k && $k < 58) {

          // 0-9
          $this->str1 .= $v;
        }
      }
    }
    if ('' === $this->str0) {
      $this->str0 = 'claspemitdbfrugnjvhowkxqyzCLASPEMITDBFRUGNJVHOWKXQYZ';
      $this->str1 = $this->str0 . '0123456789';
    }
    foreach ($tree['local'] as $var => $root) {
      if ('.' != substr($var, 0, 1) && isset($tree['local'][".{$var}"])) {
        $tree['local'][$var] += $tree['local'][".{$var}"];
      }
    }
    foreach ($tree['local'] as $var => $root) {
      if ('.' == substr($var, 0, 1) && isset($tree['local'][substr($var, 1)])) {
        $tree['local'][$var] = $tree['local'][substr($var, 1)];
      }
    }
    arsort($tree['local']);
    foreach ($tree['local'] as $var => $root) {
      switch (substr($var, 0, 1)) {
        case '.':
          if (!isset($tree['local'][substr($var, 1)])) {
            $tree['local'][$var] = '#' . ($this->specialVarRx && 3 < strlen($var) && preg_match("'^\\.{$this->specialVarRx}\$'", $var) ? $this
              ->getNextName($tree) . '$' : substr($var, 1));
          }
          break;
        case ';':
          $tree['local'][$var] = 0 === $root ? '' : $this
            ->getNextName($tree);
        case '#':
          break;
        default:
          $root = $this->specialVarRx && 2 < strlen($var) && preg_match("'^{$this->specialVarRx}\$'", $var) ? $this
            ->getNextName($tree) . '$' : $var;
          $tree['local'][$var] = $root;
          if (isset($tree['local'][".{$var}"])) {
            $tree['local'][".{$var}"] = '#' . $root;
          }
      }
    }
    foreach ($tree['local'] as $var => $root) {
      $tree['local'][$var] = preg_replace("'^#'", '.', $tree['local'][$var]);
    }
  }
  else {
    arsort($tree['local']);
    if (false !== $tree['nfe']) {
      $tree['used'][] = $tree['local'][$tree['nfe']];
    }
    foreach ($tree['local'] as $var => $root) {
      if ($tree['nfe'] !== $var) {
        $tree['local'][$var] = 0 === $root ? '' : $this
          ->getNextName($tree);
      }
    }
  }
  $this->local_tree =& $tree['local'];
  $this->used_tree =& $tree['used'];
  $tree['code'] = preg_replace_callback("#[.,{ ]?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array(
    &$this,
    'getNewName',
  ), $tree['code']);
  $this->specialVarRx && ($tree['code'] = preg_replace_callback("#//''\"\"[0-9]+'#", array(
    &$this,
    'renameInString',
  ), $tree['code']));
  foreach ($tree['childs'] as $a => &$b) {
    $this
      ->renameVars($b, false);
    $tree['code'] = str_replace($a, $b['code'], $tree['code']);
    unset($tree['childs'][$a]);
  }
}