You are here

protected function JSqueeze::renameVars in Minify JS 7

1 call to JSqueeze::renameVars()
JSqueeze::squeeze in lib/JSqueeze.php
Squeezes a JavaScript source code.

File

lib/JSqueeze.php, line 831

Class

JSqueeze

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;
      }
      elseif ('false' === $k) {
        $this->charFreq[49] += $v;
      }
      elseif (!$this->specialVarRx || !preg_match("#^{$this->specialVarRx}\$#", $k)) {
        foreach (count_chars($k, 1) as $k => $w) {
          $this->charFreq[$k] += $w * $v;
        }
      }
      elseif (2 == strlen($k)) {
        $tree['used'][] = $k[1];
      }
    }
    $this->charFreq = $this
      ->rsort($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;
      }
      elseif (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)];
      }
    }
    $tree['local'] = $this
      ->rsort($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 {
    $tree['local'] = $this
      ->rsort($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("#[.,{ ]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->varRx}:?#", array(
    $this,
    'getNewName',
  ), $tree['code']);
  if ($this->specialVarRx && preg_match_all("#//''\"\"[0-9]+'#", $tree['code'], $b)) {
    foreach ($b[0] as $a) {
      $this->strings[$a] = preg_replace_callback("#[.,{]?(?:[gs]et )?(?<![a-zA-Z0-9_\$@]){$this->specialVarRx}:?#", array(
        $this,
        'getNewName',
      ), $this->strings[$a]);
    }
  }
  foreach ($tree['childs'] as $a => &$b) {
    $this
      ->renameVars($b, false);
    $tree['code'] = str_replace($a, $b['code'], $tree['code']);
    unset($tree['childs'][$a]);
  }
}