/** * @dataProvider provideTestPrettyPrint * @covers PHPParser_PrettyPrinter_Zend<extended> */ public function testPrettyPrint($name, $code, $dump) { $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative()); $prettyPrinter = new PHPParser_PrettyPrinter_Default(); $stmts = $parser->parse($code); $this->assertEquals($this->canonicalize($dump), $this->canonicalize($prettyPrinter->prettyPrint($stmts)), $name); }
/** * @dataProvider provideTestPlaceholderReplacement * @covers PHPParser_Template */ public function testPlaceholderReplacement($templateCode, $placeholders, $expectedPrettyPrint) { $parser = new PHPParser_Parser(new PHPParser_Lexer()); $prettyPrinter = new PHPParser_PrettyPrinter_Default(); $template = new PHPParser_Template($parser, $templateCode); $this->assertEquals($expectedPrettyPrint, $prettyPrinter->prettyPrint($template->getStmts($placeholders))); }
/** * The main filter method. * Implemented according to \php_user_filter class. Will loop over all stream buckets, buffer them and perform * the needed actions. * * @param resource $in Incoming bucket brigade we need to filter * @param resource $out Outgoing bucket brigade with already filtered content * @param integer $consumed The count of altered characters as buckets pass the filter * @param boolean $closing Is the stream about to close? * * @throws \Exception * @throws \PHPParser_Error * * @return integer * * @link http://www.php.net/manual/en/php-user-filter.filter.php * * TODO The buffering does not work that well, maybe we should implement universal buffering within parent class! */ public function filter($in, $out, &$consumed, $closing) { // Get our buckets from the stream $buffer = ''; while ($bucket = stream_bucket_make_writeable($in)) { $buffer .= $bucket->data; // Tell them how much we already processed, and stuff it back into the output $consumed += $bucket->datalen; // Save a bucket for later reuse $bigBucket = $bucket; } // Beautify all the buckets! $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); try { // parse $stmts = $parser->parse($buffer); $data = '<?php ' . $prettyPrinter->prettyPrint($stmts); } catch (PHPParser_Error $e) { throw $e; } // Refill the bucket with the beautified data // Do not forget to set the length! $bigBucket->data = $data; $bigBucket->datalen = strlen($data); // Only append our big bucket stream_bucket_append($out, $bigBucket); return PSFS_PASS_ON; }
public function leaveNode(\PHPParser_Node $node) { if ($node instanceof \PHPParser_Node_Const) { $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); static::$constants[$node->name] = $prettyPrinter->prettyPrintExpr($node->value); } }
/** * Stub for writes * * @param \Trismegiste\Mondrian\Parser\PhpFile $file */ public function write(PhpFile $file) { $fch = $file->getRealPath(); $stmts = iterator_to_array($file->getIterator()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); $this->storage[basename($fch)] = $this->getMockFile($fch, "<?php\n\n" . $prettyPrinter->prettyPrint($stmts)); $this->invocationMocker->invoke(new \PHPUnit_Framework_MockObject_Invocation_Object('VirtualPhpDumper', 'write', array(basename($fch)), $this)); }
function transform_code($code, callable $transform) { $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); $ast = $parser->parse($code); $ast = $transform($ast); return '<?php' . "\n\n" . $prettyPrinter->prettyPrint($ast); }
/** * As we depend on a fully buffered bucket brigade we will do all the work here. * We will pretty-print the buffer and write the result as one big bucket into the stream * * @return void * @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException */ public function finish() { // Beautify all the buckets! $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); try { // parse $stmts = $parser->parse($this->bucketBuffer); $this->bucketBuffer = '<?php ' . $prettyPrinter->prettyPrint($stmts); } catch (\PHPParser_Error $e) { throw new GeneratorException($e->getMessage()); } }
public function testWithTraverser() { $classNode = new \PHPParser_Node_Stmt_Class('Victory', array('stmts' => array(new \PHPParser_Node_Stmt_ClassMethod('holy', array('stmts' => array(new \PHPParser_Node_Expr_New(new \PHPParser_Node_Name('Holy\\War')), new \PHPParser_Node_Expr_New(new \PHPParser_Node_Name('\\Hangar18')))))))); $file = new \Trismegiste\Mondrian\Parser\PhpFile('/I/Am/Victory.php', array($classNode)); $traverser = new \PHPParser_NodeTraverser(); $traverser->addVisitor($this->visitor); $this->assertFalse($file->isModified()); $traverser->traverse(array($file)); $this->assertTrue($file->isModified()); $this->assertCount(3, $classNode->stmts); $pp = new \PHPParser_PrettyPrinter_Default(); $flat = $pp->prettyPrint(iterator_to_array($file->getIterator())); eval($flat); }
public function cache() { $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); try { $searched_function[] = "validate_username"; $traverser_safety = new \PHPParser_NodeTraverser(); $traverser_safety->addVisitor(new SafeFunction($searched_function)); // parse $raw = file_get_contents($this->path_to_wp . '/wp-includes/user.' . $this->phpEx); $stmts = $parser->parse($raw); // traverse $stmts = $traverser_safety->traverse($stmts); // pretty print $code = $prettyPrinter->prettyPrint($stmts); file_put_contents($this->phpbb_root_path . 'cache/phpbbwpunicorn_user.' . $this->phpEx, '<?php ' . $code . ' ?>'); } catch (PHPParser\Error $e) { echo 'Parse Error: ', $e->getMessage(); } $parser = new \PHPParser_Parser(new \PHPParser_Lexer()); $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); try { $searched_function[] = "make_clickable"; $traverser_safety = new \PHPParser_NodeTraverser(); $traverser_safety->addVisitor(new SafeFunction($searched_function)); // parse $raw = file_get_contents($this->path_to_wp . '/wp-includes/formatting.' . $this->phpEx); $stmts = $parser->parse($raw); // traverse $stmts = $traverser_safety->traverse($stmts); // pretty print $code = $prettyPrinter->prettyPrint($stmts); file_put_contents($this->phpbb_root_path . 'cache/phpbbwpunicorn_formatting.' . $this->phpEx, '<?php ' . $code . ' ?>'); } catch (PHPParser\Error $e) { echo 'Parse Error: ', $e->getMessage(); } }
/** Validate passed callable for execution * * @example $sandbox->validate('<?php echo "Hello World!"; ?>'); * * @param callable|string $code The callable or string of code to validate * * @return PHPSandbox Returns the PHPSandbox instance for chainability */ public function validate($code) { $this->preparsed_code = $this->disassemble($code); $parser = new \PHPParser_Parser(new \PHPParser_Lexer_Emulative()); try { $this->parsed_ast = $parser->parse($this->preparsed_code); } catch (\PHPParser_Error $error) { $this->validation_error("Could not parse sandboxed code!", Error::PARSER_ERROR, null, $this->preparsed_code, $error); } $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); if ($this->allow_functions && $this->auto_whitelist_functions || $this->allow_constants && $this->auto_whitelist_constants || $this->allow_classes && $this->auto_whitelist_classes || $this->allow_interfaces && $this->auto_whitelist_interfaces || $this->allow_traits && $this->auto_whitelist_traits || $this->allow_globals && $this->auto_whitelist_globals) { $traverser = new \PHPParser_NodeTraverser(); $whitelister = new SandboxWhitelistVisitor($this); $traverser->addVisitor($whitelister); $traverser->traverse($this->parsed_ast); } $traverser = new \PHPParser_NodeTraverser(); $validator = new ValidatorVisitor($this); $traverser->addVisitor($validator); $this->prepared_ast = $traverser->traverse($this->parsed_ast); $this->prepared_code = $prettyPrinter->prettyPrint($this->prepared_ast); return $this; }
function filter_func($path) { return preg_match('~\\.php(?:\\.cache)?$~', $path) && false === strpos($path, 'skeleton'); } } elseif ('PHP' === $TEST_TYPE) { function filter_func($path) { return preg_match('~\\.phpt$~', $path); } } else { showHelp('Test type must be either "Symfony" or "PHP"!'); } require_once dirname(__FILE__) . '/../lib/PHPParser/Autoloader.php'; PHPParser_Autoloader::register(); $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative()); $prettyPrinter = new PHPParser_PrettyPrinter_Default(); $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 (!filter_func($file)) { continue; } $startTime = microtime(true); $code = file_get_contents($file); $readTime += microtime(true) - $startTime; if ('PHP' === $TEST_TYPE) { if (preg_match('~(?: # skeleton files ext.gmp.tests.001
private function dump($node) { $pp = new PHPParser_PrettyPrinter_Default(); return $pp->prettyPrint(array($node)); }
/** * 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_Default(); $this->code = $printer->prettyPrint(array($this->getClosureAbstractSyntaxTree())); } return $this->code; }
protected function dump($f) { $stmt = iterator_to_array($f->getIterator()); $pp = new \PHPParser_PrettyPrinter_Default(); echo $pp->prettyPrint($stmt); }
public function getArg(\PHPParser_Node_Arg $arg) { if ($arg->value instanceof \PHPParser_Node_Expr_ClassConstFetch) { $constant = $arg->value; $rightPart = $constant->name; if (array_key_exists($rightPart, MyConstantVisitor::$constants)) { return MyConstantVisitor::$constants[$rightPart]; } } $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); return $prettyPrinter->prettyPrintExpr($arg->value); }
/** * print an associative array */ public function pExpr_Array(\PHPParser_Node_Expr_Array $node) { $multiLine = FALSE; $startLine = $node->getAttribute('startLine'); $endLine = $node->getAttribute('endLine'); if ($startLine != $endLine) { $multiLine = TRUE; } $printedNodes = ''; foreach ($node->items as $itemNode) { $glueToken = ", "; if ($itemNode->getAttribute('startLine') != $startLine) { $glueToken = ',' . LF; $startLine = $itemNode->getAttribute('startLine'); } if (!empty($printedNodes)) { $printedNodes .= $glueToken . $this->p($itemNode); } else { $printedNodes .= $this->p($itemNode); } } if ($multiLine) { $multiLinedItems = $this->indentToken . preg_replace('~\\n(?!$|' . $this->noIndentToken . ')~', LF . $this->indentToken, $printedNodes); return 'array(' . LF . $multiLinedItems . LF . ')'; } else { return parent::pExpr_Array($node); } }
/** * @covers PHPParser_NodeVisitor_NameResolver */ public function testResolveLocations() { $code = <<<EOC <?php namespace NS; class A extends B implements C { use A; } interface A extends C { public function a(A \$a); } A::b(); A::\$b; A::B; new A; \$a instanceof A; namespace\\a(); namespace\\A; try { \$someThing; } catch (A \$a) { \$someThingElse; } EOC; $expectedCode = <<<EOC namespace NS; class A extends \\NS\\B implements \\NS\\C { use \\NS\\A; } interface A extends \\NS\\C { public function a(\\NS\\A \$a); } \\NS\\A::b(); \\NS\\A::\$b; \\NS\\A::B; new \\NS\\A(); \$a instanceof \\NS\\A; \\NS\\a(); \\NS\\A; try { \$someThing; } catch (\\NS\\A \$a) { \$someThingElse; } EOC; $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative()); $prettyPrinter = new PHPParser_PrettyPrinter_Default(); $traverser = new PHPParser_NodeTraverser(); $traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver()); $stmts = $parser->parse($code); $stmts = $traverser->traverse($stmts); $this->assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts)); }
public function __toString() { $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); return $prettyPrinter->prettyPrint($this->statement); }