-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.php
More file actions
31 lines (27 loc) · 733 Bytes
/
example2.php
File metadata and controls
31 lines (27 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
require 'vendor/autoload.php';
class MyNodeVisitor extends NodeVisitorAbstract
{
public function leaveNode(Node $node) {
if ($node instanceof Node\Scalar\String_) {
$node->value = 'World';
}
}
}
$code = <<<'CODE'
<?php
echo 'Hello';
CODE;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$traverser = new NodeTraverser;
// add your visitor
$traverser->addVisitor(new MyNodeVisitor);
$ast = $parser->parse($code);
$traverser->traverse($ast);
$prettyPrinter = new Standard();
echo $prettyPrinter->prettyPrintFile($ast);