@@ -31,10 +31,16 @@ expression.
31
31
Customizing the formatting
32
32
--------------------------
33
33
34
- Apart from an ` shortArraySyntax ` option, the default pretty printer does not provide any
35
- functionality to customize the formatting of the generated code. The pretty printer does respect a
36
- number of ` kind ` attributes used by some notes (e.g., whether an integer should be printed as
37
- decimal, hexadecimal, etc), but there are no options to control brace placement or similar.
34
+ The pretty printer respects a number of ` kind ` attributes used by some notes (e.g., whether an
35
+ integer should be printed as decimal, hexadecimal, etc). Additionally, it supports two options:
36
+
37
+ * ` phpVersion ` (defaults to 7.0) allows opting into formatting that is not supported by older PHP
38
+ versions.
39
+ * ` shortArraySyntax ` determines the used array syntax if the ` kind ` attribute is not set. This is
40
+ a legacy option, and ` phpVersion ` should be used to control this behavior instead.
41
+
42
+ However, the default pretty printer does not provide any functionality for fine-grained
43
+ customization of code formatting.
38
44
39
45
If you want to make minor changes to the formatting, the easiest way is to extend the pretty printer
40
46
and override the methods responsible for the node types you are interested in.
@@ -46,29 +52,27 @@ default pretty printer with an existing library for code reformatting, such as
46
52
Formatting-preserving pretty printing
47
53
-------------------------------------
48
54
49
- > ** Note:** This functionality is ** experimental** and not yet complete.
50
-
51
55
For automated code refactoring, migration and similar, you will usually only want to modify a small
52
56
portion of the code and leave the remainder alone. The basic pretty printer is not suitable for
53
57
this, because it will also reformat parts of the code which have not been modified.
54
58
55
- Since PHP-Parser 4.0, an experimental formatting-preserving pretty-printing mode is available, which
59
+ Since PHP-Parser 4.0, a formatting-preserving pretty-printing mode is available, which
56
60
attempts to preserve the formatting of code (those AST nodes that have not changed) and only reformat
57
61
code which has been modified or newly inserted.
58
62
59
63
Use of the formatting-preservation functionality requires some additional preparatory steps:
60
64
61
65
``` php
62
- use PhpParser\{Lexer, NodeTraverser, NodeVisitor, Parser , PrettyPrinter};
66
+ use PhpParser\{Lexer, NodeTraverser, NodeVisitor, ParserFactory , PrettyPrinter};
63
67
64
- $lexer = new Lexer\Emulative( [
68
+ $lexerOptions = new [
65
69
'usedAttributes' => [
66
70
'comments',
67
71
'startLine', 'endLine',
68
72
'startTokenPos', 'endTokenPos',
69
73
],
70
- ]) ;
71
- $parser = new Parser\Php7($lexer );
74
+ ];
75
+ $parser = ( new ParserFactory())->createForHostVersion($lexerOptions );
72
76
73
77
$traverser = new NodeTraverser();
74
78
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
@@ -86,11 +90,9 @@ $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
86
90
```
87
91
88
92
If you make use of the name resolution functionality, you will likely want to disable the
89
- ` replaceNodes ` option. This will add resolved names as attributes, instead of directlying modifying
93
+ ` replaceNodes ` option. This will add resolved names as attributes, instead of directly modifying
90
94
the AST and causing spurious changes to the pretty printed code. For more information, see the
91
95
[ name resolution documentation] ( Name_resolution.markdown ) .
92
96
93
- This functionality is experimental and not yet fully implemented. It should not provide incorrect
94
- code, but it may sometimes reformat more code than necessary. Open issues are tracked in
95
- [ issue #344 ] ( https://github.com/nikic/PHP-Parser/issues/344 ) . If you encounter problems while using
96
- this functionality, please open an issue, so we know what to prioritize.
97
+ The formatting-preservation works on a best-effort basis and may sometimes reformat more code tha
98
+ necessary. If you encounter problems while using this functionality, please open an issue.
0 commit comments