/** * Return the arguments to this function call * @return string the arguments as passed to this function call */ function get_args() { $pretty_printer = new PhpParser\PrettyPrinter\Standard(); $args = array(); foreach ($this->node->args as $arg) { $args[] = $pretty_printer->pArg($arg); } return $args; }
/** * Return this function's parameters, including types, default values, etc * @return string the parameters, separated by commas and spaces */ function get_params() { $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); $pretty = array(); foreach ($this->node->params as $param) { $pretty[] = $prettyPrinter->pParam($param); } return implode(', ', $pretty); }
/** * @param $cache_file * @param array $source_files */ public function generateMerge($cache_file, array $source_files) { $parser = new \PhpParser\Parser(new \PhpParser\Lexer()); $config = new \PhpParser\Node\Expr\Array_(); foreach ($source_files as $basename => $file) { $stmts = $parser->parse(file_get_contents($file)); if (empty($stmts) || !$stmts[0] instanceof \PhpParser\Node\Stmt\Return_) { throw new \RuntimeException("Unable to locate return statement with config items in {$file}"); } $config->items[] = new \PhpParser\Node\Expr\ArrayItem($stmts[0]->expr, new \PhpParser\Node\Scalar\String_($basename)); } $prettyPrinter = new \PhpParser\PrettyPrinter\Standard(); $out = $prettyPrinter->prettyPrintExpr($config); file_put_contents($cache_file, '<?php' . PHP_EOL . PHP_EOL . '// THIS IS AUTOGENERATED FILE! DO NOT EDIT IT!' . PHP_EOL . PHP_EOL . 'return ' . $out . ';' . PHP_EOL); }
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; }
public function printCFGNode() { $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); print "[Loop Header Node] : "; if ($this->loop_type == CFGNodeLoopHeader::FOR_LOOP) { print "[For Loop] : \n"; print "("; foreach ($this->expr->init as $initExpr) { print $prettyPrinter->prettyPrintExpr($initExpr) . " ; "; } print ")\n"; print "("; foreach ($this->expr->cond as $condExpr) { print $prettyPrinter->prettyPrintExpr($condExpr) . " ; "; } print ")\n"; print "("; foreach ($this->expr->loop as $loopExpr) { print $prettyPrinter->prettyPrintExpr($loopExpr) . " ; "; } print ")\n"; } else { if ($this->loop_type == CFGNodeLoopHeader::FOREACH_LOOP) { print "[Foreach Loop] : \n"; print $prettyPrinter->prettyPrintExpr($this->expr->expr) . " ;\n"; // TODO: Fix null reference. //print ($prettyPrinter->prettyPrintExpr($this->expr->keyVar)) . " ;\n"; //print ($prettyPrinter->prettyPrintExpr($this->expr->valueVar)) . " ;\n"; } else { if ($this->loop_type == CFGNodeLoopHeader::WHILE_LOOP) { print "[While Loop] : \n"; print $prettyPrinter->prettyPrintExpr($this->expr->cond) . " ;\n"; } else { print "Unrecognized Loop.\n"; } } } }
return null; } if (preg_match('~--EXPECT(?:F|REGEX)?--\\s*(?:Parse|Fatal) error~', $code)) { return null; } return $matches[1]; }; break; default: showHelp('Test type must be one of: PHP5, PHP7 or Symfony'); } require_once dirname(__FILE__) . '/../lib/PhpParser/Autoloader.php'; PhpParser\Autoloader::register(); $parserName = 'PhpParser\\Parser\\' . $version; $parser = new $parserName(new PhpParser\Lexer\Emulative()); $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); $nodeDumper = new PhpParser\NodeDumper(); $parseFail = $ppFail = $compareFail = $count = 0; $readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0; $totalStartTime = microtime(true); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) { if (!$fileFilter($file)) { continue; } $startTime = microtime(true); $code = file_get_contents($file); $readTime += microtime(true) - $startTime; if (null === ($code = $codeExtractor($file, $code))) { continue; } set_time_limit(10);
private function dump($node) { $pp = new \PhpParser\PrettyPrinter\Standard(); return $pp->prettyPrint(array($node)); }
ini_set('xdebug.max_nesting_level', 3000); // Disable XDebug var_dump() output truncation ini_set('xdebug.var_display_max_children', -1); ini_set('xdebug.var_display_max_data', -1); ini_set('xdebug.var_display_max_depth', -1); list($operations, $files) = parseArgs($argv); /* Dump nodes by default */ if (empty($operations)) { $operations[] = 'dump'; } if (empty($files)) { showHelp("Must specify at least one file."); } $parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative()); $dumper = new PhpParser\NodeDumper(); $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); $serializer = new PhpParser\Serializer\XML(); $traverser = new PhpParser\NodeTraverser(); $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver()); foreach ($files as $file) { if (strpos($file, '<?php') === 0) { $code = $file; echo "====> Code {$code}\n"; } else { if (!file_exists($file)) { die("File {$file} does not exist.\n"); } $code = file_get_contents($file); echo "====> File {$file}:\n"; } try {
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; }
function printExpr($expr) { $prettyPrinter = new PhpParser\PrettyPrinter\Standard(); $code = $prettyPrinter->prettyPrintExpr($expr); print $code . "\n"; }