/** * @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; }
/** * @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); }
/** * 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(); } }
/** * @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)); }
/** 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; }
if (preg_match('~--EXPECT(?:F|REGEX)?--\\s*(?:Parse|Fatal) error~', $code)) { continue; } $code = $matches[1]; } set_time_limit(10); ++$count; if ($SHOW_PROGRESS) { 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;
private function dump($node) { $pp = new PHPParser_PrettyPrinter_Default(); return $pp->prettyPrint(array($node)); }
public function __toString() { $prettyPrinter = new \PHPParser_PrettyPrinter_Default(); return $prettyPrinter->prettyPrint($this->statement); }
/** * 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); }