Example #1
0
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     $tknsLen = sizeof($this->tkns);
     $touchedDoubleColon = false;
     for ($ptr = 0; $ptr < $tknsLen; ++$ptr) {
         $token = $this->tkns[$ptr];
         list($id) = $this->getToken($token);
         if (T_DOUBLE_COLON == $id) {
             $touchedDoubleColon = true;
         }
         if ($touchedDoubleColon && T_CLASS == $id) {
             $touchedDoubleColon = false;
             break;
         }
         if (T_CLASS == $id || T_INTERFACE == $id || T_TRAIT == $id) {
             $this->refWalkUsefulUntil($this->tkns, $ptr, T_STRING);
             list(, $name) = $this->getToken($this->tkns[$ptr]);
             $this->refWalkUsefulUntil($this->tkns, $ptr, ST_CURLY_OPEN);
             $start = $ptr;
             $this->refWalkCurlyBlock($this->tkns, $ptr);
             $end = ++$ptr;
             $this->convertToPlaceholder($name, $start, $end);
             break;
         }
     }
     return $this->render();
 }
function find_methods($source_code, $file)
{
    $class_name = "";
    $tokens = token_get_all($source_code);
    $in_token = false;
    foreach ($tokens as $key => $token) {
        switch ($token[0]) {
            case T_FUNCTION:
                $in_token = T_FUNCTION;
                continue;
                break;
            case T_STRING:
                switch ($in_token) {
                    case T_FUNCTION:
                        $line_num = $token[2];
                        $value = $token[1];
                        $token_type = $token[0];
                        $class_name .= $line_num . " " . $value . " " . $dir . '/' . $ENV['TM_FILEPATH'] . "\n";
                        $in_token = false;
                        break;
                }
                break;
            default:
                if (count($token) > 2) {
                    //$class_name.= "type " . token_name ($token[0]) . " line: ". $token[2] . " class " . $token[1];
                }
                break;
        }
    }
    echo $class_name;
}
 protected function loadFile($file)
 {
     $striped = "";
     // remove all whitespaces and comments
     // replace short tags
     $t = token_get_all(str_replace('<?=', '<?php echo ', php_strip_whitespace($file)));
     $blacklist = array(T_AS, T_CASE, T_INSTANCEOF, T_USE);
     foreach ($t as $i => $token) {
         if (is_string($token)) {
             $striped .= $token;
         } elseif (T_WHITESPACE === $token[0]) {
             if (isset($t[$i + 1]) && is_array($t[$i + 1])) {
                 if (in_array($t[$i + 1][0], $blacklist)) {
                     $striped .= $t[$i][1];
                     continue;
                 }
                 if (isset($t[$i - 1]) && is_array($t[$i - 1])) {
                     if (in_array($t[$i - 1][0], $blacklist)) {
                         $striped .= $t[$i][1];
                         continue;
                     }
                     if ($t[$i - 1][0] == T_ECHO || $t[$i - 1][0] == T_PRINT) {
                         $striped .= $t[$i][1];
                         continue;
                     }
                 }
             }
             $striped .= str_replace(' ', '', $token[1]);
         } else {
             $striped .= $token[1];
         }
     }
     $this->buffer = $striped;
 }
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         if (T_WHITESPACE == $id || T_VARIABLE == $id || T_INLINE_HTML == $id || T_COMMENT == $id || T_DOC_COMMENT == $id || T_CONSTANT_ENCAPSED_STRING == $id) {
             $this->appendCode($text);
             continue;
         }
         if (T_STRING == $id && $this->leftUsefulTokenIs([T_DOUBLE_COLON, T_OBJECT_OPERATOR])) {
             $this->appendCode($text);
             continue;
         }
         if (T_START_HEREDOC == $id) {
             $this->appendCode($text);
             $this->printUntil(ST_SEMI_COLON);
             continue;
         }
         if (ST_QUOTE == $id) {
             $this->appendCode($text);
             $this->printUntilTheEndOfString();
             continue;
         }
         $lcText = strtolower($text);
         if (('true' === $lcText || 'false' === $lcText || 'null' === $lcText) && !$this->leftUsefulTokenIs([T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST]) && !$this->rightUsefulTokenIs([T_NS_SEPARATOR, T_AS, T_CLASS, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_INTERFACE, T_NEW, T_NS_SEPARATOR, T_PAAMAYIM_NEKUDOTAYIM, T_USE, T_TRAIT, T_INSTEADOF, T_CONST]) || isset(static::$reservedWords[$lcText])) {
             $text = $lcText;
         }
         $this->appendCode($text);
     }
     return $this->code;
 }
Example #5
0
 private function parse($src, $interestedClass = null)
 {
     $this->tokens = token_get_all($src);
     $classes = $uses = array();
     $namespace = '';
     while ($token = $this->next()) {
         if (T_NAMESPACE === $token[0]) {
             $namespace = $this->parseNamespace();
             $uses = array();
         } elseif (T_CLASS === $token[0] || T_INTERFACE === $token[0]) {
             if ('' !== $namespace) {
                 $class = $namespace . '\\' . $this->nextValue();
             } else {
                 $class = $this->nextValue();
             }
             $classes[$class] = $uses;
             if (null !== $interestedClass && $interestedClass === $class) {
                 return $classes;
             }
         } elseif (T_USE === $token[0]) {
             foreach ($this->parseUseStatement() as $useStatement) {
                 list($alias, $class) = $useStatement;
                 $uses[strtolower($alias)] = $class;
             }
         }
     }
     return $classes;
 }
Example #6
0
 /**
  * Strips whitespace from source. Taken from composer
  * @param $source
  * @return string
  */
 private function stripWhitespace($source)
 {
     if (!function_exists('token_get_all')) {
         return $source;
     }
     $output = '';
     foreach (token_get_all($source) as $token) {
         if (is_string($token)) {
             $output .= $token;
         } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
             // $output .= $token[1];
             $output .= str_repeat("\n", substr_count($token[1], "\n"));
         } elseif (T_WHITESPACE === $token[0]) {
             // reduce wide spaces
             $whitespace = preg_replace('{[ \\t]+}', ' ', $token[1]);
             // normalize newlines to \n
             $whitespace = preg_replace('{(?:\\r\\n|\\r|\\n)}', "\n", $whitespace);
             // trim leading spaces
             $whitespace = preg_replace('{\\n +}', "\n", $whitespace);
             $output .= $whitespace;
         } else {
             $output .= $token[1];
         }
     }
     return $output;
 }
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         switch ($id) {
             case T_WHILE:
                 $str = $text;
                 while (list($index, $token) = each($this->tkns)) {
                     list($id, $text) = $this->getToken($token);
                     $this->ptr = $index;
                     $str .= $text;
                     if (ST_CURLY_OPEN == $id || ST_COLON == $id || ST_SEMI_COLON == $id && (ST_SEMI_COLON == $ptId || ST_CURLY_OPEN == $ptId || T_COMMENT == $ptId || T_DOC_COMMENT == $ptId)) {
                         $this->appendCode($str);
                         break;
                     } elseif (ST_SEMI_COLON == $id && !(ST_SEMI_COLON == $ptId || ST_CURLY_OPEN == $ptId || T_COMMENT == $ptId || T_DOC_COMMENT == $ptId)) {
                         $this->rtrimAndAppendCode($str);
                         break;
                     }
                 }
                 break;
             case T_WHITESPACE:
                 $this->appendCode($text);
                 break;
             default:
                 $ptId = $id;
                 $this->appendCode($text);
                 break;
         }
     }
     return $this->code;
 }
 /**
  * @param cfhCompile_CodeWriter $codeWriter
  * @param cfhCompile_Class_Interface $class
  * @param unknown_type $sourceCode
  * @param cfhCompile_ClassRegistry $classRegistry
  * @return String
  */
 public function preWrite(cfhCompile_CodeWriter $codeWriter, cfhCompile_Class_Interface $class, $sourceCode, cfhCompile_ClassRegistry $classRegistry)
 {
     if (is_null($sourceCode)) {
         return NULL;
     }
     $tokens = token_get_all('<?php ' . $sourceCode);
     $sourceCode = '';
     $lastWasString = FALSE;
     while ($token = current($tokens)) {
         $nextIsString = is_string(next($tokens));
         prev($tokens);
         if (is_string($token)) {
             $sourceCode .= $token;
             $lastWasString = TRUE;
         } else {
             list($token, $text) = $token;
             if ($token == T_WHITESPACE) {
                 if ($lastWasString === FALSE && $nextIsString === FALSE) {
                     $sourceCode .= ' ';
                 }
             } else {
                 $sourceCode .= $text;
             }
             $lastWasString = FALSE;
         }
         next($tokens);
     }
     return trim(substr($sourceCode, 5));
 }
 /**
  * Returns an array of tokens this test wants to listen for.
  *
  * @return array
  */
 public function register()
 {
     // Everyone has had a chance to figure out what forbidden functions
     // they want to check for, so now we can cache out the list.
     $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
     if ($this->patternMatch === true) {
         foreach ($this->forbiddenFunctionNames as $i => $name) {
             $this->forbiddenFunctionNames[$i] = '/' . $name . '/i';
         }
         return array(T_STRING);
     }
     // If we are not pattern matching, we need to work out what
     // tokens to listen for.
     $string = '<?php ';
     foreach ($this->forbiddenFunctionNames as $name) {
         $string .= $name . '();';
     }
     $register = array();
     $tokens = token_get_all($string);
     array_shift($tokens);
     foreach ($tokens as $token) {
         if (is_array($token) === true) {
             $register[] = $token[0];
         }
     }
     return array_unique($register);
 }
Example #10
0
 public function handleTraitFile($basename, $pathname, $depth)
 {
     $traits = null;
     // The results of individual file parses are cached, since only a few
     // files will have changed and TokenisedRegularExpression is quite
     // slow. A combination of the file name and file contents hash are used,
     // since just using the datetime lead to problems with upgrading.
     $file = file_get_contents($pathname);
     $key = preg_replace('/[^a-zA-Z0-9_]/', '_', $basename) . '_' . md5($file);
     if ($data = $this->cache->load($key)) {
         $valid = isset($data['traits']) && isset($data['namespace']) && is_array($data['traits']) && is_string($data['namespace']);
         if ($valid) {
             $traits = $data['traits'];
             $namespace = $data['namespace'];
         }
     }
     if (!$traits) {
         $tokens = token_get_all($file);
         $namespace = self::get_namespace_parser()->findAll($tokens);
         if ($namespace) {
             $namespace = implode('', $namespace[0]['namespaceName']) . '\\';
         } else {
             $namespace = '';
         }
         $traits = self::get_trait_parser()->findAll($tokens);
         $cache = array('traits' => $traits, 'namespace' => $namespace);
         $this->cache->save($cache, $key);
     }
     foreach ($traits as $trait) {
         $this->traits[strtolower($namespace . $trait['traitName'])] = $pathname;
     }
 }
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     $touchedDoubleArrow = false;
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         if (T_DOUBLE_ARROW == $id) {
             $touchedDoubleArrow = true;
             $this->appendCode($text);
             continue;
         }
         if ($touchedDoubleArrow) {
             if (T_WHITESPACE == $id || T_DOC_COMMENT == $id || T_COMMENT == $id) {
                 $this->appendCode($text);
                 continue;
             }
             if (T_ARRAY === $id) {
                 $this->rtrimAndAppendCode($text);
                 $touchedDoubleArrow = false;
                 continue;
             }
             $touchedDoubleArrow = false;
         }
         $this->appendCode($text);
     }
     return $this->code;
 }
Example #12
0
function compress_code($content)
{
    $strip_str = "";
    $tokens = token_get_all($content);
    $last_space = FALSE;
    $i = 0;
    $j = count($tokens);
    for (; $i < $j; ++$i) {
        if (is_string($tokens[$i])) {
            $last_space = FALSE;
            $strip_str .= $tokens[$i];
        } else {
            switch ($tokens[$i][0]) {
                case T_COMMENT:
                case T_DOC_COMMENT:
                case T_WHITESPACE:
                    if ($last_space) {
                        break;
                    }
                    $strip_str .= " ";
                    $last_space = TRUE;
                    break;
                default:
                    $last_space = FALSE;
                    $strip_str .= $tokens[$i][1];
            }
        }
    }
    return $strip_str;
}
Example #13
0
 /**
  * Returns an array that contains namespace and name of the class defined in the file
  * 
  * Code losely based on http://stackoverflow.com/questions/7153000/get-class-name-from-file
  * by user http://stackoverflow.com/users/492901/netcoder
  * 
  * @param string file to anaylse
  * @return array
  */
 public static function getClassInfo($file)
 {
     $buffer = file_get_contents($file);
     $tokens = @token_get_all($buffer);
     $class = $namespace = $buffer = '';
     for ($i = 0; $i < count($tokens); $i++) {
         if ($tokens[$i][0] === T_NAMESPACE) {
             for ($j = $i + 1; $j < count($tokens); $j++) {
                 if ($tokens[$j][0] === T_STRING) {
                     $namespace .= '\\' . $tokens[$j][1];
                 } else {
                     if ($tokens[$j] === '{' || $tokens[$j] === ';') {
                         break;
                     }
                 }
             }
         }
         if ($tokens[$i][0] === T_CLASS) {
             for ($j = $i + 1; $j < count($tokens); $j++) {
                 if ($tokens[$j] === '{') {
                     if (!isset($tokens[$i + 2][1])) {
                         error_log($file . ' does not contain a valid class definition');
                         break;
                     } else {
                         $class = $tokens[$i + 2][1];
                     }
                 }
             }
         }
     }
     return array('ns' => $namespace, 'class' => $class);
 }
Example #14
0
 /**
  * Gets the class and adds into the map
  */
 private static function get_class_name($FileName)
 {
     $Tokens = token_get_all(file_get_contents($FileName));
     $IsTestable = false;
     $IsClass = false;
     foreach ($Tokens as $Token) {
         if (is_array($Token)) {
             if (!$IsTestable && $Token[0] == T_DOC_COMMENT && strpos($Token[1], "@TestClass")) {
                 $IsTestable = true;
             }
             if ($IsTestable && $Token[0] == T_CLASS) {
                 $IsClass = true;
             } else {
                 if ($IsClass && $Token[0] == T_STRING) {
                     $ReflectionClass = new ReflectionClass($Token[1]);
                     if (count(self::get_testable_methods($ReflectionClass))) {
                         self::$Classes[$Token[1]] = new ReflectionClass($Token[1]);
                     }
                     $IsTestable = false;
                     $IsClass = false;
                 }
             }
         }
     }
 }
 /**
  * Returns the full class name for the first class in the file.
  *
  * @param string $file A PHP file path
  *
  * @return string|false Full class name if found, false otherwise
  */
 protected function findClass($file)
 {
     $class = false;
     $namespace = false;
     $tokens = token_get_all(file_get_contents($file));
     for ($i = 0, $count = count($tokens); $i < $count; $i++) {
         $token = $tokens[$i];
         if (!is_array($token)) {
             continue;
         }
         if (true === $class && T_STRING === $token[0]) {
             return $namespace . '\\' . $token[1];
         }
         if (true === $namespace && T_STRING === $token[0]) {
             $namespace = '';
             do {
                 $namespace .= $token[1];
                 $token = $tokens[++$i];
             } while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
         }
         if (T_CLASS === $token[0]) {
             $class = true;
         }
         if (T_NAMESPACE === $token[0]) {
             $namespace = true;
         }
     }
     return false;
 }
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         switch ($id) {
             case T_NAMESPACE:
                 if ($this->rightUsefulTokenIs(T_NS_SEPARATOR)) {
                     break;
                 }
                 $this->appendCode($text);
                 list($foundId, $foundText) = $this->printAndStopAt([ST_CURLY_OPEN, ST_SEMI_COLON]);
                 if (ST_CURLY_OPEN == $foundId) {
                     $this->appendCode($foundText);
                     $this->printCurlyBlock();
                 } elseif (ST_SEMI_COLON == $foundId) {
                     $this->appendCode(ST_CURLY_OPEN);
                     list($foundId, $foundText) = $this->printAndStopAt([T_NAMESPACE, T_CLOSE_TAG]);
                     if (T_CLOSE_TAG == $foundId) {
                         return $source;
                     }
                     $this->appendCode($this->getCrlf() . ST_CURLY_CLOSE . $this->getCrlf());
                     prev($this->tkns);
                     continue;
                 }
                 break;
             default:
                 $this->appendCode($text);
         }
     }
     return $this->code;
 }
Example #17
0
 function token_get_all_nl($source)
 {
     $new_tokens = array();
     // Get the tokens
     $tokens = token_get_all($source);
     // Split newlines into their own tokens
     foreach ($tokens as $token) {
         $token_name = is_array($token) ? $token[0] : null;
         $token_data = is_array($token) ? $token[1] : $token;
         // Do not split encapsed strings or multiline comments
         if ($token_name == T_CONSTANT_ENCAPSED_STRING || substr($token_data, 0, 2) == '/*') {
             $new_tokens[] = array($token_name, $token_data);
             continue;
         }
         // Split the data up by newlines
         $split_data = preg_split('#(\\r\\n|\\n)#', $token_data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
         foreach ($split_data as $data) {
             if ($data == "\r\n" || $data == "\n") {
                 // This is a new line token
                 $new_tokens[] = array(T_NEW_LINE, $data);
             } else {
                 // Add the token under the original token name
                 $new_tokens[] = is_array($token) ? array($token_name, $data) : $data;
             }
         }
     }
     return $new_tokens;
 }
Example #18
0
/**
 * 压缩PHP代码
 *
 * @param string $content 压缩内容
 * @return string
 */
function compress_code($content)
{
    $strip_str = '';
    //分析php源码
    $tokens = token_get_all($content);
    $last_space = false;
    for ($i = 0, $j = count($tokens); $i < $j; $i++) {
        if (is_string($tokens[$i])) {
            $last_space = false;
            $strip_str .= $tokens[$i];
        } else {
            switch ($tokens[$i][0]) {
                //过滤各种PHP注释
                case T_COMMENT:
                case T_DOC_COMMENT:
                    break;
                    //过滤空格
                //过滤空格
                case T_WHITESPACE:
                    if (!$last_space) {
                        $strip_str .= ' ';
                        $last_space = true;
                    }
                    break;
                default:
                    $last_space = false;
                    $strip_str .= $tokens[$i][1];
            }
        }
    }
    return $strip_str;
}
 /**
  * Extracts tokens from a source code.
  *
  * @param string $source Source code
  */
 protected final function processSource($source)
 {
     $stream = @token_get_all(str_replace(array("\r\n", "\r"), "\n", $source));
     static $checkLines = array(T_COMMENT => true, T_WHITESPACE => true, T_DOC_COMMENT => true, T_INLINE_HTML => true, T_ENCAPSED_AND_WHITESPACE => true, T_CONSTANT_ENCAPSED_STRING => true);
     foreach ($stream as $position => $token) {
         if (is_array($token)) {
             if (!NATIVE_TRAITS && T_STRING === $token[0]) {
                 $lValue = strtolower($token[1]);
                 if ('trait' === $lValue) {
                     $token[0] = T_TRAIT;
                 } elseif ('insteadof' === $lValue) {
                     $token[0] = T_INSTEADOF;
                 } elseif ('__TRAIT__' === $token[1]) {
                     $token[0] = T_TRAIT_C;
                 } elseif ('callable' === $lValue) {
                     $token[0] = T_CALLABLE;
                 }
             }
             $this->tokens[] = $token;
         } else {
             $previous = $this->tokens[$position - 1];
             $line = $previous[2];
             if (isset($checkLines[$previous[0]])) {
                 $line += substr_count($previous[1], "\n");
             }
             $this->tokens[] = array($token, $token, $line);
         }
     }
     $this->count = count($this->tokens);
 }
Example #20
0
function get_php_classes($php_code, $onlypublic)
{
    $classes = array();
    $methods = array();
    $tokens = token_get_all($php_code);
    $count = count($tokens);
    for ($i = 2; $i < $count; $i++) {
        if ($tokens[$i - 2][0] == T_CLASS && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
            $class_name = $tokens[$i][1];
            $methods[$class_name] = array();
        }
        if ($tokens[$i - 2][0] == T_FUNCTION && $tokens[$i - 1][0] == T_WHITESPACE && $tokens[$i][0] == T_STRING) {
            if ($onlypublic) {
                if (!in_array($tokens[$i - 4][0], array(T_PROTECTED, T_PRIVATE))) {
                    $method_name = $tokens[$i][1];
                    $methods[$class_name][] = $method_name;
                }
            } else {
                $method_name = $tokens[$i][1];
                $methods[$class_name][] = $method_name;
            }
        }
    }
    return $methods;
}
Example #21
0
 static function scan_php_file($file, &$cache)
 {
     $code = file_get_contents($file);
     $raw_tokens = token_get_all($code);
     unset($code);
     $tokens = array();
     $func_token_list = array("t" => array(), "t2" => array());
     $token_number = 0;
     // Filter out HTML / whitespace, and build a lookup for global function calls.
     foreach ($raw_tokens as $token) {
         if (!is_array($token) || $token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML) {
             if (is_array($token)) {
                 if ($token[0] == T_STRING && in_array($token[1], array("t", "t2"))) {
                     $func_token_list[$token[1]][] = $token_number;
                 }
             }
             $tokens[] = $token;
             $token_number++;
         }
     }
     unset($raw_tokens);
     if (!empty($func_token_list["t"])) {
         $errors = l10n_scanner::_parse_t_calls($tokens, $func_token_list["t"], $cache);
         foreach ($errors as $line => $error) {
             Kohana_Log::add("error", "Translation scanner error.  " . "file: " . substr($file, strlen(DOCROOT)) . ", line: {$line}, context: {$error}");
         }
     }
     if (!empty($func_token_list["t2"])) {
         $errors = l10n_scanner::_parse_plural_calls($tokens, $func_token_list["t2"], $cache);
         foreach ($errors as $line => $error) {
             Kohana_Log::add("error", "Translation scanner error.  " . "file: " . substr($file, strlen(DOCROOT)) . ", line: {$line}, context: {$error}");
         }
     }
 }
 /**
  * Finalizes the processor.
  *
  * @return  string
  */
 public function finalize()
 {
     static $classes = array(T_VARIABLE => 'variable', T_CLASS => 'keyword', T_INTERFACE => 'keyword', T_EXTENDS => 'keyword', T_IMPLEMENTS => 'keyword', T_CATCH => 'keyword', T_THROW => 'keyword', T_TRY => 'keyword', T_NEW => 'keyword', T_FUNCTION => 'keyword', T_FOR => 'keyword', T_IF => 'keyword', T_ELSE => 'keyword', T_SWITCH => 'keyword', T_WHILE => 'keyword', T_FOREACH => 'keyword', T_RETURN => 'keyword', T_STATIC => 'modifier', T_ABSTRACT => 'modifier', T_PUBLIC => 'modifier', T_PRIVATE => 'modifier', T_PROTECTED => 'modifier', T_FINAL => 'modifier', T_DNUMBER => 'number', T_LNUMBER => 'number', T_CONSTANT_ENCAPSED_STRING => 'string', T_COMMENT => 'comment', '{' => 'bracket', '}' => 'bracket', '(' => 'bracket', ')' => 'bracket');
     // Tokenize buffer
     $tokens = token_get_all('<?php ' . trim($this->buffer, "\r\n") . '?>');
     if (!is_array($tokens) || xp::errorAt(__FILE__, __LINE__ - 1)) {
         $e = new FormatException('Cannot parse "' . $this->buffer . '"');
         xp::gc(__FILE__);
         throw $e;
     }
     // Create HTML
     $current = NULL;
     $out = '';
     for ($i = 1, $s = sizeof($tokens) - 1; $i < $s; $i++) {
         $token = $tokens[$i];
         $class = isset($classes[$token[0]]) ? $classes[$token[0]] : 'default';
         // Handle annotations
         if (is_array($token) && T_COMMENT === $token[0] && '#' === $token[1][0]) {
             $class = 'annotation';
         }
         if ($current != $class) {
             $out .= '</span><span class="' . $class . '">';
             $current = $class;
         }
         $out .= strtr(htmlspecialchars(is_array($token) ? $token[1] : $token), array("\n" => '<br/>', "\r" => ''));
     }
     // Skip leading "</span>" (7)
     return '</p><code>' . substr($out, 7) . ($current ? '</span>' : '') . '</code><p>';
 }
Example #23
0
 public static function tokenize($code, $options = array())
 {
     $defaults = array('wrap' => true, 'ignore' => array(), 'include' => array());
     $options += $defaults;
     $tokens = array();
     $line = 1;
     if ($options['wrap']) {
         $code = "<?php {$code}?>";
     }
     foreach (token_get_all($code) as $token) {
         $token = is_array($token) ? $token : array(null, $token, $line);
         list($id, $content, $line) = $token;
         $name = $id ? token_name($id) : $content;
         if (!empty($options['include'])) {
             if (!in_array($name, $options['include']) && !in_array($id, $options['include'])) {
                 continue;
             }
         }
         if (in_array($name, $options['ignore']) || in_array($id, $options['ignore'])) {
             continue;
         }
         $tokens[] = compact('id', 'name', 'content', 'line');
     }
     if ($options['wrap'] && empty($options['include'])) {
         $tokens = array_slice($tokens, 1, count($tokens) - 2);
     }
     return $tokens;
 }
Example #24
0
 public function __construct($filePath)
 {
     if (!file_exists($filePath) || !is_readable($filePath)) {
         throw new FileException('File does not exist, or is not readable; check permissions.');
     }
     $this->tokens = token_get_all(file_get_contents($filePath));
 }
Example #25
0
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         switch ($id) {
             case T_IF:
                 if ($this->leftTokenIs([T_ELSE]) && !$this->leftTokenIs([T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO])) {
                     $this->rtrimAndAppendCode($text);
                     break;
                 }
                 $this->appendCode($text);
                 break;
             case T_ELSEIF:
                 $this->appendCode(str_replace(' ', '', $text));
                 break;
             default:
                 $this->appendCode($text);
                 break;
         }
     }
     return $this->code;
 }
Example #26
0
 public function run() : DependencyInterface
 {
     foreach ($this->files as $file) {
         $this->parser->reset();
         foreach (token_get_all(file_get_contents($file[0])) as $token) {
             if (is_array($token)) {
                 $this->parser->tokenArray($token);
             } else {
                 $this->parser->tokenString($token);
             }
         }
         $calls = $this->parser->getCalls();
         foreach ($calls as $call => $count) {
             $call = $this->normalize->normalize($call);
             if (!array_key_exists($call, $this->flipArray)) {
                 continue;
             }
             $key = $this->flipArray[$call];
             if (isset($this->calls[$key][$call])) {
                 $this->calls[$key][$call] = $this->calls[$key][$call] + $count;
             } else {
                 $this->calls[$key][$call] = $count;
             }
         }
     }
     return $this;
 }
Example #27
0
 /**
  * Get a namespaced classname from a string of php
  * 
  * @param string $contents
  * @return string|null
  */
 public static function fromContents($contents)
 {
     $class = null;
     $namespace = null;
     $tokens = token_get_all($contents);
     for ($i = 0, $len = count($tokens); $i < $len; $i++) {
         if ($tokens[$i][0] === T_NAMESPACE) {
             for ($j = $i + 1, $jlen = count($tokens); $j < $jlen; $j++) {
                 if ($tokens[$j][0] === T_STRING) {
                     $namespace .= '\\' . $tokens[$j][1];
                 } else {
                     if ($tokens[$j] === '{' || $tokens[$j] === ';') {
                         break;
                     }
                 }
             }
         } else {
             if ($tokens[$i][0] === T_CLASS) {
                 for ($j = $i + 1; $j < $len; $j++) {
                     if ($tokens[$j] === '{') {
                         $class = $tokens[$i + 2][1];
                     }
                 }
             }
         }
     }
     if ($class === null) {
         return null;
     }
     return $namespace === null ? $class : trim($namespace . '\\' . $class, '\\');
 }
 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the current token
  *                                        in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     // We are only interested in the first token in a multi-line string.
     if ($tokens[$stackPtr]['code'] === $tokens[$stackPtr - 1]['code']) {
         return;
     }
     $workingString = $tokens[$stackPtr]['content'];
     $lastStringToken = $stackPtr;
     $i = $stackPtr + 1;
     if (isset($tokens[$i]) === true) {
         while ($i < $phpcsFile->numTokens && $tokens[$i]['code'] === $tokens[$stackPtr]['code']) {
             $workingString .= $tokens[$i]['content'];
             $lastStringToken = $i;
             $i++;
         }
     }
     // Check if it's a double quoted string.
     if (strpos($workingString, '"') === false) {
         return;
     }
     // Make sure it's not a part of a string started in a previous line.
     // If it is, then we have already checked it.
     if ($workingString[0] !== '"') {
         return;
     }
     // The use of variables in double quoted strings is not allowed.
     if ($tokens[$stackPtr]['code'] === T_DOUBLE_QUOTED_STRING) {
         $stringTokens = token_get_all('<?php ' . $workingString);
         foreach ($stringTokens as $token) {
             if (is_array($token) === true && $token[0] === T_VARIABLE) {
                 $error = 'Variable "%s" not allowed in double quoted string; use concatenation instead';
                 $data = array($token[1]);
                 $phpcsFile->addError($error, $stackPtr, 'ContainsVar', $data);
             }
         }
         return;
     }
     //end if
     $allowedChars = array('\\0', '\\1', '\\2', '\\3', '\\4', '\\5', '\\6', '\\7', '\\n', '\\r', '\\f', '\\t', '\\v', '\\x', '\\b', '\\e', '\\u', '\'');
     foreach ($allowedChars as $testChar) {
         if (strpos($workingString, $testChar) !== false) {
             return;
         }
     }
     $error = 'String %s does not require double quotes; use single quotes instead';
     $data = array(str_replace("\n", '\\n', $workingString));
     $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotRequired', $data);
     if ($fix === true) {
         $phpcsFile->fixer->beginChangeset();
         $innerContent = substr($workingString, 1, -1);
         $innerContent = str_replace('\\"', '"', $innerContent);
         $phpcsFile->fixer->replaceToken($stackPtr, "'{$innerContent}'");
         while ($lastStringToken !== $stackPtr) {
             $phpcsFile->fixer->replaceToken($lastStringToken, '');
             $lastStringToken--;
         }
         $phpcsFile->fixer->endChangeset();
     }
 }
 public function format($source)
 {
     $this->tkns = token_get_all($source);
     $this->code = '';
     while (list($index, $token) = each($this->tkns)) {
         list($id, $text) = $this->getToken($token);
         $this->ptr = $index;
         switch ($id) {
             case T_TRAIT:
             case T_CLASS:
                 if ($this->leftUsefulTokenIs(T_DOUBLE_COLON)) {
                     $this->appendCode($text);
                     break;
                 }
                 $this->appendCode($text);
                 $this->printUntil(ST_CURLY_OPEN);
                 list(, $text) = $this->printAndStopAt(T_WHITESPACE);
                 if ($this->hasLn($text)) {
                     $text = substr(strrchr($text, 10), 0);
                 }
                 $this->appendCode($text);
                 break;
             default:
                 $this->appendCode($text);
                 break;
         }
     }
     return $this->code;
 }
Example #30
0
File: Runner.php Project: klei/phut
 public function getFullyQualifiedClassNameFromFile($fileName)
 {
     $content = file_get_contents($fileName);
     $tokens = token_get_all($content);
     $waitingForNamespace = false;
     $waitingForClass = false;
     $namespace = null;
     $class = null;
     foreach ($tokens as $token) {
         if (!is_array($token)) {
             continue;
         }
         if ($token[0] == T_NAMESPACE) {
             $waitingForNamespace = true;
             $waitingForClass = false;
         } elseif ($token[0] == T_CLASS) {
             $waitingForClass = true;
             $waitingForNamespace = false;
         } elseif ($token[0] == T_STRING && $waitingForNamespace) {
             $namespace = $token[1];
             $waitingForNamespace = false;
         } elseif ($token[0] == T_STRING && $waitingForClass) {
             $class = $token[1];
             $waitingForClass = false;
         }
         if ($class !== null) {
             break;
         }
     }
     return $class !== null ? ($namespace !== null ? $namespace . '\\' : '') . $class : null;
 }