Skip to content

Instantly share code, notes, and snippets.

@nikic nikic/demo.php
Created Dec 26, 2016

Embed
What would you like to do?
Demo for format preserving AST transformation
<?php
/* This demo replaces all unqualified function calls and namespaced lookups
* with fully qualified ones. Apart from this change all formatting should
* be preserved. */
use PhpParser\Lexer;
use PhpParser\Parser;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use PhpParser\PrettyPrinter;
use PhpParser\Error;
use PhpParser\Node;
$dir = /* directory to operate on */;
require __DIR__ . '/vendor/autoload.php';
class NameFullyQualifier extends NodeVisitorAbstract {
public function leaveNode(Node $node) {
if ($node instanceof Node\Name
&& $node->hasAttribute('namespacedName')
&& !in_array(strtolower($node), ['true', 'false', 'null'])
) {
return new Node\Name\FullyQualified($node);
}
return null;
}
}
$lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$parser = new Parser\Php7($lexer, [
'useIdentifierNodes' => true,
'useConsistentVariableNodes' => true,
'useExpressionStatements' => true,
'useNopStatements' => false,
]);
$nameResolver = new NodeVisitor\NameResolver(null, [
'replaceNodes' => false
]);
$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
$traverser->addVisitor($nameResolver);
$traverser->addVisitor(new NameFullyQualifier());
$printer = new PrettyPrinter\Standard();
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($it as $file) {
if (!preg_match('/\.php$/', $file)) {
continue;
}
try {
$code = file_get_contents($file);
$origStmts = $parser->parse($code);
$newCode = $printer->printFormatPreserving(
$traverser->traverse($origStmts),
$origStmts,
$lexer->getTokens()
);
file_put_contents($file, $newCode);
} catch (Error $e) {
echo "Parser error in $file: {$e->getMessage()}\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.