/**
  * Makes sure a variable is formatted namespace.namespace.etc
  *
  * @param shell
  *    Called shell because it might be missing some data
  */
 public static function normalizeVariableName($shell, $source, $start)
 {
     if (strpos($shell, '[') !== false) {
         $source_line = array_shift(Text::chop($source, $start[0], $start[1], $start[0] + 1));
         preg_match('%^\\s*([a-zA-Z_.$][\\w.$]*(?:\\.[a-zA-Z_.$][\\w.$]|\\["[^"]+"\\])*)\\s*=\\s*function%', $source_line, $match);
         $shell = preg_replace('%\\["([^"]+)"\\]%', '.$1', $match[1]);
     }
     return $shell;
 }
Ejemplo n.º 2
0
 public function getName()
 {
     if ($this->name) {
         return $this->name;
     }
     $line = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->start[0]);
     $line = $line[$this->start[0]];
     return $this->name = trim(substr($line, 0, strpos($line, '(')));
 }
Ejemplo n.º 3
0
 public function build()
 {
     if (!$this->start) {
         die("DojoObject->build() used before setting a start position");
     }
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], false, false, true);
     $end = array($this->start[0], $this->start[1]);
     do {
         $lines = Text::chop($this->package->getCode(), $end[0], $end[1], false, false, true);
         foreach ($lines as $line_number => $line) {
             if (preg_match('%^\\s*}%', $line)) {
                 break;
             }
             if (preg_match('%^(\\s*)([a-zA-Z0-9_$]+|"\\s+")\\s*:%', $line, $match)) {
                 if ($end[0] != $this->start[0] && $end[1] != $this->start[1]) {
                     $between_lines = Text::chop($this->package->getSource(), $end[0], $end[1], $line_number, strlen($match[1]), true);
                     $between_started = true;
                     foreach ($between_lines as $between_line) {
                         if ($between_started && empty($between_line)) {
                             break;
                         }
                         if (!empty($between_line)) {
                             $between_started = true;
                         }
                         $this->body->addBlockCommentLine($between_line);
                     }
                     $between_started = false;
                 }
                 $end = array($line_number, strlen($match[0]));
                 if ($match[2][0] == '"' || $match[2][0] == "'") {
                     $key = trim(implode(Text::chop($this->package->getSource(), $line_number, strpos($line, '"') + 1, $line_number, strlen($match[0]) - 3, false)));
                 } else {
                     $key = $match[2];
                 }
                 break;
             }
         }
         if (!$key) {
             $end = Text::findTermination($lines, '}');
         } else {
             $parameter = new DojoParameter($this->package, $end[0], $end[1], '}');
             $end = $parameter->build();
             $this->values[$key] = $parameter;
         }
     } while ($lines[$end[0]][$end[1]] != '}');
     $this->setEnd($end[0], $end[1]);
     return $end;
 }
 public function build()
 {
     if (!$this->start) {
         die("DojoExecutedFunction->build() used before setting a start position");
     }
     if ($this->end) {
         return $this->end;
     }
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1]);
     $line = $lines[$this->start[0]];
     $this->start = array($this->start[0], strpos($line, 'function'));
     $this->end = parent::build();
     // Basically, the end array here will hold the position of the final } in the function declaration.
     $lines = Text::chop($this->package->getCode(), $this->end[0], $this->end[1], false, false, true);
     $closed = false;
     foreach ($lines as $line_number => $line) {
         $offset = 0;
         if ($line_number == $this->end[0]) {
             $offset = $this->end[1];
         }
         if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) {
             if (!$closed) {
                 if ($match[0][0] != ')') {
                     return false;
                 } else {
                     $closed = true;
                     $offset = $match[0][1] + 1;
                 }
             }
         }
         if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) {
             if ($closed) {
                 if ($match[0][0] != '(') {
                     return false;
                 } else {
                     $parameters = new DojoParameters($this->package, $line_number, $match[0][1]);
                     $end = $parameters->build();
                     break;
                 }
             }
         }
     }
     return $end;
 }
Ejemplo n.º 5
0
 public function removeCodeFrom($lines)
 {
     $keys = array_keys($lines);
     $first = array_shift($keys);
     $last = array_pop($keys);
     for ($i = $first; $i <= $last; $i++) {
         $line = $lines[$i];
         if (preg_match('%function\\s*\\([^)]*\\)\\s*{%', $line, $match, PREG_OFFSET_CAPTURE)) {
             $declaration = new DojoFunctionDeclare($this, $i, $match[0][1]);
             list($i, ) = $declaration->build();
             $lines = $declaration->removeCodeFrom($lines);
         } elseif (preg_match('%^.*(with|switch)\\s*\\([^(]*\\)\\s*{%', $line, $match)) {
             $with_lines = Text::chop($lines, $i, strlen($match[0]) - 1, null, null, true);
             list($end_line, $end_pos) = Text::findTermination($with_lines, '}', '{}()[]');
             for ($j = $i; $j <= $end_line; $j++) {
                 $line = $lines[$j];
                 if ($j == $i) {
                     $lines[$j] = Text::blankOutAt($line, strlen($match[0]) - 1);
                 } elseif ($j == $end_line) {
                     $lines[$j] = Text::blankOutAt($line, 0, $end_pos);
                 } else {
                     $lines[$j] = Text::blankOut($line, $line);
                 }
             }
         }
     }
     return $lines;
 }
 public function build()
 {
     if (!$this->start) {
         die("DojoFunctionDeclare->build() used before setting a start position");
     }
     if ($this->end) {
         return $this->end;
     }
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1]);
     $line = trim($lines[$this->start[0]]);
     if (strpos($line, 'function') === 0) {
         $line = substr($line, 8);
         preg_match('%[^\\s]%', $line, $match);
         if ($match[0] != '(') {
             $this->function_name = trim(substr($line, 0, strpos($line, '(')));
         }
     } else {
         $name = trim(substr($line, 0, strpos($line, '=')));
         $extra = substr($line, strpos($line, '=') + 1);
         if (preg_match('%^\\s+new\\s+%', $name, $match) || preg_match('%^\\s*new\\s+%', $extra, $match)) {
             $this->anonymous = true;
             $name = str_replace($match[0], '', $name);
         }
         if (($pos = strpos($name, '.prototype.')) !== false) {
             $this->prototype = substr($name, 0, $pos);
             $name = str_replace('.prototype', '', $name);
         }
         if (($pos = strpos($name, 'this.')) === 0) {
             $this->instance = $this->getFunctionName();
             $name = $this->getFunctionName() . "." . preg_replace('%^this\\.%', '', $name);
         }
         if (!$this->isAnonymous()) {
             $full_lines = Text::chop($this->package->getCode(), $this->start[0], 0);
             $full_line = substr($full_lines[$this->start[0]], 0, $this->start[1]);
             if (preg_match('%(?:[a-zA-Z0-9._$]+\\s*=\\s*)+$%', $full_line, $matches)) {
                 $aliases = preg_split('%\\s*=\\s*%', $matches[0]);
                 foreach ($aliases as $alias) {
                     $alias = trim($alias);
                     if ($alias) {
                         if (strpos($alias, 'this.') === 0) {
                             $alias = $this->getFunctionName() . "." . preg_replace('%^this\\.%', '', $alias);
                         }
                         $this->aliases[] = $alias;
                     }
                 }
             }
         }
         if (strpos($name, '[') !== false) {
             $source_lines = Text::chop($this->package->getSource(), $this->start[0], $this->start[1]);
             $source_line = trim($source_lines[$this->start[0]]);
             preg_match('%^\\s*([a-zA-Z_.$][\\w.$]*(?:\\.[a-zA-Z_.$][\\w.$]|\\["[^"]+"\\])*)\\s*=\\s*function%', $source_line, $match);
             $name = preg_replace('%\\["([^"]+)"\\]%', '.$1', $match[1]);
         }
         $this->function_name = $name;
     }
     $this->parameters->setStart($this->start[0], strpos($lines[$this->start[0]], '('));
     $end = $this->parameters->build();
     $lines = Text::chop($this->package->getCode(), $end[0], $end[1]);
     foreach ($lines as $line_number => $line) {
         if (($pos = strpos($line, '{')) !== false) {
             $this->body->setStart($line_number, $pos);
             return $this->end = $this->body->build();
         }
     }
 }
 public function getThisInheritanceCalls()
 {
     if ($this->this_inheritance_calls) {
         return $this->this_inheritance_calls;
     }
     $internalized = $this->getLocalVariableNames();
     $this->build();
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
     foreach ($lines as $line) {
         if (preg_match('%\\b([a-zA-Z0-9_.$]+)\\.(?:apply|call)\\s*\\(%', $line, $match) && !array_key_exists($match[1], $internalized)) {
             $this->this_inheritance_calls[] = $match[1];
         }
     }
     return $this->this_inheritance_calls;
 }
Ejemplo n.º 8
0
 public function getType()
 {
     if ($this->parameter_type) {
         return $this->parameter_type;
     }
     $type = array();
     $lines = Text::chop($this->package->getSource(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
     foreach ($lines as $line) {
         list($first, $middle, $last, $data, $multiline) = Text::findComments($line, $multiline);
         $type = array_merge($type, array($first, $middle, $last));
     }
     return $this->parameter_type = implode(' ', array_diff($type, array('')));
 }
Ejemplo n.º 9
0
 public function build()
 {
     if (!$this->start) {
         die("DojoExecutedFunction->build() used before setting a start position");
     }
     if ($this->end) {
         return $this->end;
     }
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1]);
     $line = $lines[$this->start[0]];
     $this->start = array($this->start[0], strpos($line, 'function'));
     $this->end = parent::build();
     // Basically, the end array here will hold the position of the final } in the function declaration.
     $parameters = $this->getParameters();
     if (count($parameters) && ($pos = strpos($lines[$this->end[0]], '(')) !== false) {
         $arguments = new DojoParameters($this->package);
         $arguments->start = array($this->end[0], $pos);
         $arguments->build();
         $arguments = $arguments->getParameters();
         foreach ($parameters as $pos => $parameter) {
             if ($arguments[$pos]) {
                 $argument = $arguments[$pos];
                 if ($argument->isA(DojoVariable) && $parameter->isA(DojoVariable)) {
                     if (preg_match('%(^|\\|\\|)([a-zA-Z0-9_.$]+)(\\|\\||$)%', $argument->getVariable(), $match)) {
                         $this->body->addResolvedParameter($parameter->getVariable(), $match[2]);
                     }
                 }
             }
         }
     }
     $lines = Text::chop($this->package->getCode(), $this->end[0], $this->end[1], false, false, true);
     $closed = false;
     foreach ($lines as $line_number => $line) {
         $offset = 0;
         if ($line_number == $this->end[0]) {
             $offset = $this->end[1];
         }
         if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) {
             if (!$closed) {
                 if ($match[0][0] != ')') {
                     return false;
                 } else {
                     $closed = true;
                     $offset = $match[0][1] + 1;
                 }
             }
         }
         if (preg_match('%\\S%', $line, $match, PREG_OFFSET_CAPTURE, $offset)) {
             if ($closed) {
                 if ($match[0][0] != '(') {
                     return false;
                 } else {
                     $parameters = new DojoParameters($this->package, $line_number, $match[0][1]);
                     $end = $parameters->build();
                     break;
                 }
             }
         }
     }
     return $end;
 }
Ejemplo n.º 10
0
 public function getThisInheritanceCalls()
 {
     if ($this->this_inheritance_calls) {
         return $this->this_inheritance_calls;
     }
     $this->build();
     $lines = Text::chop($this->package->getCode(), $this->start[0], $this->start[1], $this->end[0], $this->end[1], true);
     foreach ($lines as $line) {
         if (preg_match('%\\b([a-zA-Z0-9_.$]+)\\.(?:apply|call)\\s*\\(%', $line, $match)) {
             $this->this_inheritance_calls[] = $match[1];
         }
     }
     return $this->this_inheritance_calls;
 }