Chris@13: Pretty printing Chris@13: =============== Chris@13: Chris@13: Pretty printing is the process of converting a syntax tree back to PHP code. In its basic mode of Chris@13: operation the pretty printer provided by this library will print the AST using a certain predefined Chris@13: code style and will discard (nearly) all formatting of the original code. Because programmers tend Chris@13: to be rather picky about their code formatting, this mode of operation is not very suitable for Chris@13: refactoring code, but can be used for automatically generated code, which is usually only read for Chris@13: debugging purposes. Chris@13: Chris@13: Basic usage Chris@13: ----------- Chris@13: Chris@13: ```php Chris@13: $stmts = $parser->parse($code); Chris@13: Chris@13: // MODIFY $stmts here Chris@13: Chris@13: $prettyPrinter = new PhpParser\PrettyPrinter\Standard; Chris@13: $newCode = $prettyPrinter->prettyPrintFile($stmts); Chris@13: ``` Chris@13: Chris@13: The pretty printer has three basic printing methods: `prettyPrint()`, `prettyPrintFile()` and Chris@13: `prettyPrintExpr()`. The one that is most commonly useful is `prettyPrintFile()`, which takes an Chris@13: array of statements and produces a full PHP file, including opening ` **Note:** This functionality is **experimental** and not yet complete. Chris@13: Chris@13: For automated code refactoring, migration and similar, you will usually only want to modify a small Chris@13: portion of the code and leave the remainder alone. The basic pretty printer is not suitable for Chris@13: this, because it will also reformat parts of the code which have not been modified. Chris@13: Chris@13: Since PHP-Parser 4.0, an experimental formatting-preserving pretty-printing mode is available, which Chris@13: attempts to preserve the formatting of code (those AST nodes that have not changed) and only reformat Chris@13: code which has been modified or newly inserted. Chris@13: Chris@13: Use of the formatting-preservation functionality requires some additional preparatory steps: Chris@13: Chris@13: ```php Chris@13: use PhpParser\{Lexer, NodeTraverser, NodeVisitor, Parser, PrettyPrinter}; Chris@13: Chris@13: $lexer = new Lexer\Emulative([ Chris@13: 'usedAttributes' => [ Chris@13: 'comments', Chris@13: 'startLine', 'endLine', Chris@13: 'startTokenPos', 'endTokenPos', Chris@13: ], Chris@13: ]); Chris@13: $parser = new Parser\Php7($lexer); Chris@13: Chris@13: $traverser = new NodeTraverser(); Chris@13: $traverser->addVisitor(new NodeVisitor\CloningVisitor()); Chris@13: Chris@13: $printer = new PrettyPrinter\Standard(); Chris@13: Chris@13: $oldStmts = $parser->parse($code); Chris@13: $oldTokens = $lexer->getTokens(); Chris@13: Chris@13: $newStmts = $traverser->traverse($oldStmts); Chris@13: Chris@13: // MODIFY $newStmts HERE Chris@13: Chris@13: $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); Chris@13: ``` Chris@13: Chris@13: If you make use of the name resolution functionality, you will likely want to disable the Chris@13: `replaceNodes` option. This will add resolved names as attributes, instead of directlying modifying Chris@13: the AST and causing spurious changes to the pretty printed code. For more information, see the Chris@13: [name resolution documentation](Name_resolution.markdown). Chris@13: Chris@13: This functionality is experimental and not yet fully implemented. It should not provide incorrect Chris@13: code, but it may sometimes reformat more code than necessary. Open issues are tracked in Chris@13: [issue #344](https://github.com/nikic/PHP-Parser/issues/344). If you encounter problems while using Chris@13: this functionality, please open an issue, so we know what to prioritize.