Beispiel #1
0
 public static function formatCode($parsedCode)
 {
     // special case - make sure we have code to format
     if ($parsedCode === null) {
         return 'no code' . PHP_EOL;
     }
     // we have the parsed code
     // we're going to need something to turn the parsed code back
     // into source code
     $prettyPrinter = new \PhpParser\PrettyPrinter\Standard();
     // convert the parsed code into a string
     //
     // this string may contain more than just the code we are
     // interested in
     $codeToPrint = $prettyPrinter->prettyPrint([$parsedCode]);
     // let's strip stuff
     $codeToPrint = self::stripComments($codeToPrint);
     // all done
     return $codeToPrint;
 }
Beispiel #2
0
 $code = file_get_contents($file);
 $readTime += microtime(true) - $startTime;
 if (null === ($code = $codeExtractor($file, $code))) {
     continue;
 }
 set_time_limit(10);
 ++$count;
 if ($showProgress) {
     echo substr(str_pad('Testing file ' . $count . ': ' . substr($file, strlen($dir)), 79), 0, 79), "\r";
 }
 try {
     $startTime = microtime(true);
     $stmts = $parser->parse($code);
     $parseTime += microtime(true) - $startTime;
     $startTime = microtime(true);
     $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
     $ppTime += microtime(true) - $startTime;
     try {
         $startTime = microtime(true);
         $ppStmts = $parser->parse($code);
         $reparseTime += microtime(true) - $startTime;
         $startTime = microtime(true);
         $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
         $compareTime += microtime(true) - $startTime;
         if (!$same) {
             echo $file, ":\n    Result of initial parse and parse after pretty print differ\n";
             ++$compareFail;
         }
     } catch (PhpParser\Error $e) {
         echo $file, ":\n    Parse of pretty print failed with message: {$e->getMessage()}\n";
         ++$ppFail;
Beispiel #3
0
 private function dump($node)
 {
     $pp = new \PhpParser\PrettyPrinter\Standard();
     return $pp->prettyPrint(array($node));
 }
Beispiel #4
0
 public function setCache()
 {
     //empty the cache folder
     $this->cleanFolder();
     $phpbb_root_path = get_option('wpphpbbu_path', false);
     $phpEx = 'php';
     $GLOBALS['phpbb_root_path'] = get_option('wpphpbbu_path', false);
     $GLOBALS['phpEx'] = 'php';
     $GLOBALS['phpbb_root_path'] = $phpbb_root_path;
     //fix make_clickable
     $parser = new \PhpParser\Parser(new \PhpParser\Lexer());
     $prettyPrinter = new \PhpParser\PrettyPrinter\Standard();
     try {
         $searched_function[] = "make_clickable";
         $traverser_safety = new \PhpParser\NodeTraverser();
         $traverser_safety->addVisitor(new \SafeFunction($searched_function));
         // parse
         $raw = file_get_contents($phpbb_root_path . 'includes/functions_content.' . $phpEx);
         $stmts = $parser->parse($raw);
         // traverse
         $stmts = $traverser_safety->traverse($stmts);
         // pretty print
         $code = $prettyPrinter->prettyPrint($stmts);
         file_put_contents(__DIR__ . '/cache/functions_content.' . $phpEx, '<?php ' . $code . ' ?>');
     } catch (PhpParser\Error $e) {
         echo 'Parse Error: ', $e->getMessage();
     }
     try {
         $searched_function[] = "validate_username";
         $traverser_safety = new \PhpParser\NodeTraverser();
         $traverser_safety->addVisitor(new \SafeFunction($searched_function));
         // parse
         $raw = file_get_contents($phpbb_root_path . 'includes/functions_user.' . $phpEx);
         $stmts = $parser->parse($raw);
         // traverse
         $stmts = $traverser_safety->traverse($stmts);
         // pretty print
         $code = $prettyPrinter->prettyPrint($stmts);
         file_put_contents(__DIR__ . '/cache/functions_user.' . $phpEx, '<?php ' . $code . ' ?>');
     } catch (\PhpParser\Error $e) {
         echo 'Parse Error: ', $e->getMessage();
     }
     //unicorn code is actually useless, im bored. At least, it was a good exercise
     try {
         $traverser_path = new \PhpParser\NodeTraverser();
         //dont forget to escape the path, = preq_quote?
         $mypath = __DIR__;
         $traverser_path->addVisitor(new \PathFixer("\$phpbb_root_path \\. \\'includes/functions_content.\\' \\. \$phpEx", [new \PhpParser\Node\Scalar\String_($mypath . '/cache/functions_content.'), new \PhpParser\Node\Expr\Variable('phpEx')]));
         //fix path to functions_content
         $raw = file_get_contents($phpbb_root_path . 'common.' . $phpEx);
         // parse
         $stmts = $parser->parse($raw);
         // traverse
         $stmts = $traverser_path->traverse($stmts);
         // pretty print
         $code = $prettyPrinter->prettyPrint($stmts);
         file_put_contents(__DIR__ . '/cache/common.' . $phpEx, '<?php ' . $code . ' ?>');
     } catch (\PhpParser\Error $e) {
         echo 'Parse Error: ', $e->getMessage();
     }
 }
 /**
  * Returns the formatted code of the closure
  *
  * @return string
  */
 public function getCode()
 {
     if (!$this->code) {
         // Use the pretty printer to print the closure code from the AST
         $printer = new \PhpParser\PrettyPrinter\Standard();
         $this->code = $printer->prettyPrint(array($this->getClosureAbstractSyntaxTree()));
     }
     return $this->code;
 }
Beispiel #6
0
function printStmts($stmts)
{
    $prettyPrinter = new PhpParser\PrettyPrinter\Standard();
    $code = $prettyPrinter->prettyPrint($stmts);
    print $code . "\n";
}