Skip to content

Instantly share code, notes, and snippets.

Created June 28, 2020 04:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
require_once __DIR__ . '/../vendor/autoload.php';
use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\PrettyPrinter;
class VarDumpConvertPrintVisitor extends NodeVisitorAbstract
public function leaveNode(Node $node)
if ($node instanceof Node\Expr\FuncCall && $node->name->getLast() == 'var_dump') {
$node->name->parts = ['print'];
$code = <<<'CODE'
function test()
$lexer = new Lexer\Emulative([
'usedAttributes' => [
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
$parser = new Parser\Php7($lexer);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
$printer = new PrettyPrinter\Standard();
try {
$oldStmts = $parser->parse($code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
$oldTokens = $lexer->getTokens();
$newStmts = $traverser->traverse($oldStmts);
$traverser = new NodeTraverser;
$traverser->addVisitor(new VarDumpConvertPrintVisitor);
$stmts = $traverser->traverse($newStmts);
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment