Exemplo n.º 1
0
 /**
  * Find function calls and extract
  *
  * @param string $filePath
  * @param array $functions
  * @return array
  */
 public function processFile($filePath, $functions)
 {
     $result = array('problems' => array(), 'fixes' => array());
     $phpTokens = new PhpFileParser($filePath);
     $changes = 0;
     foreach ($phpTokens as $key => $row) {
         // get non trivial tokens
         if (is_array($row)) {
             list($token, $functionName, $lineNumber) = $row;
             $originalFunctionName = $functionName;
             // prepare normalized version of function name for matching
             $functionName = strtolower($functionName);
             //				if ($token == T_CONSTANT_ENCAPSED_STRING && function_exists(trim($functionName, '\'""'))) {
             //					$functionName = trim($functionName, '\'""');
             //					if (!in_array($functionName, $this->calledFunctions)) {
             //						$this->calledFunctions[] = $functionName;
             //					}
             //				}
             // check for function call
             if ($token == T_STRING && !$phpTokens->isEqualToToken(T_OBJECT_OPERATOR, $key - 1) && !$phpTokens->isEqualToToken(T_DOUBLE_COLON, $key - 1) && !$phpTokens->isEqualToToken(T_FUNCTION, $key - 2)) {
                 // mark function as called
                 if (function_exists($functionName) && !in_array($functionName, $this->calledFunctions)) {
                     $this->calledFunctions[] = $functionName;
                 }
                 // is it function we're looking for
                 if (isset($functions[$functionName])) {
                     $definingFunctionName = $phpTokens->getDefiningFunctionName($key);
                     //we're skipping deprecated calls that are in deprecated function itself
                     if (!$definingFunctionName || !isset($functions[strtolower($definingFunctionName)])) {
                         $result['problems'][] = array($functions[$functionName], $originalFunctionName, $lineNumber);
                     }
                     //do instant replacement
                     if ($this->fixProblems && isset($this->instantReplacements[$functionName])) {
                         $phpTokens[$key] = array(T_STRING, $this->instantReplacements[$functionName]);
                         $result['fixes'][] = array($originalFunctionName, $this->instantReplacements[$functionName], $lineNumber);
                         $changes++;
                     }
                 }
             }
         }
     }
     if ($changes) {
         try {
             $phpTokens->exportPhp($filePath);
         } catch (CodeReview_IOException $e) {
             echo '*** Error: ' . $e->getMessage() . " ***\n";
         }
     }
     unset($phpTokens);
     return $result;
 }
 public function testSourcePreserve()
 {
     $tests = $this->getTestsPhpFiles();
     foreach ($tests as $test) {
         list($inPath, ) = $test;
         $tokens = new PhpFileParser($inPath);
         $actual = $tokens->exportPhp();
         $this->assertTrue(is_string($actual));
         $this->assertTrue(strlen($actual) > 0);
         $expected = file_get_contents($inPath);
         $this->assertEquals($expected, $actual);
     }
 }