@@ -12,7 +12,7 @@ To bootstrap the library, include the autoloader generated by composer:
12
12
require 'path/to/vendor/autoload.php';
13
13
```
14
14
15
- Additionally you may want to set the ` xdebug.max_nesting_level ` ini option to a higher value:
15
+ Additionally, you may want to set the ` xdebug.max_nesting_level ` ini option to a higher value:
16
16
17
17
``` php
18
18
ini_set('xdebug.max_nesting_level', 3000);
@@ -29,25 +29,29 @@ In order to parse code, you first have to create a parser instance:
29
29
30
30
``` php
31
31
use PhpParser\ParserFactory;
32
- $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
33
- ```
32
+ use PhpParser\PhpVersion;
33
+
34
+ // Parser for the version you are running on.
35
+ $parser = (new ParserFactory())->createForHostVersion();
34
36
35
- The factory accepts a kind argument, that determines how different PHP versions are treated:
37
+ // Parser for the newest PHP version supported by the PHP-Parser library.
38
+ $parser = (new ParserFactory())->createForNewestSupportedVersion();
36
39
37
- Kind | Behavior
38
- -----|---------
39
- ` ParserFactory::PREFER_PHP7 ` | Try to parse code as PHP 7. If this fails, try to parse it as PHP 5.
40
- ` ParserFactory::PREFER_PHP5 ` | Try to parse code as PHP 5. If this fails, try to parse it as PHP 7.
41
- ` ParserFactory::ONLY_PHP7 ` | Parse code as PHP 7.
42
- ` ParserFactory::ONLY_PHP5 ` | Parse code as PHP 5.
40
+ // Parser for a specific PHP version.
41
+ $parser = (new ParserFactory())->createForVersion(PhpVersion::fromString('8.1'));
42
+ ```
43
43
44
- Unless you have a strong reason to use something else, ` PREFER_PHP7 ` is a reasonable default.
44
+ Which version you should target depends on your use case. In many cases you will want to use the
45
+ host version, as people typically analyze code for the version they are running on. However, when
46
+ analyzing arbitrary code you are usually best off using the newest supported version, which tends
47
+ to accept the widest range of code (unless there are breaking changes in PHP).
45
48
46
- The ` create ()` method optionally accepts a ` Lexer ` instance as the second argument . Some use cases
47
- that require customized lexers are discussed in the [ lexer documentation] ( component/Lexer.markdown ) .
49
+ The ` createXYZ ()` methods optionally accept an array of lexer options . Some use cases that require
50
+ customized lexer options are discussed in the [ lexer documentation] ( component/Lexer.markdown ) .
48
51
49
- Subsequently you can pass PHP code (including the opening ` <?php ` tag) to the ` parse ` method in order to
50
- create a syntax tree. If a syntax error is encountered, an ` PhpParser\Error ` exception will be thrown:
52
+ Subsequently, you can pass PHP code (including the opening ` <?php ` tag) to the ` parse() ` method in
53
+ order to create a syntax tree. If a syntax error is encountered, an ` PhpParser\Error ` exception will
54
+ be thrown by default:
51
55
52
56
``` php
53
57
<?php
@@ -62,7 +66,7 @@ function printLine($msg) {
62
66
printLine('Hello World!!!');
63
67
CODE;
64
68
65
- $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7 );
69
+ $parser = (new ParserFactory())->createForHostVersion( );
66
70
67
71
try {
68
72
$stmts = $parser->parse($code);
@@ -77,7 +81,7 @@ A parser instance can be reused to parse multiple files.
77
81
Node dumping
78
82
------------
79
83
80
- To dump the abstract syntax tree in human readable form, a ` NodeDumper ` can be used:
84
+ To dump the abstract syntax tree in human- readable form, a ` NodeDumper ` can be used:
81
85
82
86
``` php
83
87
<?php
@@ -171,7 +175,7 @@ with them easier they are grouped into three categories:
171
175
172
176
* ` PhpParser\Node\Stmt ` s are statement nodes, i.e. language constructs that do not return
173
177
a value and can not occur in an expression. For example a class definition is a statement.
174
- It doesn't return a value and you can't write something like ` func(class A {}); ` .
178
+ It doesn't return a value, and you can't write something like ` func(class A {}); ` .
175
179
* ` PhpParser\Node\Expr ` s are expression nodes, i.e. language constructs that return a value
176
180
and thus can occur in other expressions. Examples of expressions are ` $var `
177
181
(` PhpParser\Node\Expr\Variable ` ) and ` func() ` (` PhpParser\Node\Expr\FuncCall ` ).
@@ -201,15 +205,14 @@ can then be retrieved using `hasAttribute()`, `getAttribute()` and `getAttribute
201
205
By default the lexer adds the ` startLine ` , ` endLine ` and ` comments ` attributes. ` comments ` is an array
202
206
of ` PhpParser\Comment[\Doc] ` instances.
203
207
204
- The start line can also be accessed using ` getLine() ` / ` setLine ()` (instead of ` getAttribute('startLine') ` ).
208
+ The start line can also be accessed using ` getStartLine ()` (instead of ` getAttribute('startLine') ` ).
205
209
The last doc comment from the ` comments ` attribute can be obtained using ` getDocComment() ` .
206
210
207
211
Pretty printer
208
212
--------------
209
213
210
- The pretty printer component compiles the AST back to PHP code. As the parser does not retain formatting
211
- information the formatting is done using a specified scheme. Currently there is only one scheme available,
212
- namely ` PhpParser\PrettyPrinter\Standard ` .
214
+ The pretty printer component compiles the AST back to PHP code according to a specified scheme.
215
+ Currently, there is only one scheme available, namely ` PhpParser\PrettyPrinter\Standard ` .
213
216
214
217
``` php
215
218
use PhpParser\Error;
@@ -218,8 +221,8 @@ use PhpParser\PrettyPrinter;
218
221
219
222
$code = "<?php echo 'Hi ', hi\\getTarget();";
220
223
221
- $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7 );
222
- $prettyPrinter = new PrettyPrinter\Standard;
224
+ $parser = (new ParserFactory())->createForHostVersion( );
225
+ $prettyPrinter = new PrettyPrinter\Standard() ;
223
226
224
227
try {
225
228
// parse
@@ -254,10 +257,13 @@ single expression using `prettyPrintExpr()`.
254
257
The ` prettyPrintFile() ` method can be used to print an entire file. This will include the opening ` <?php ` tag
255
258
and handle inline HTML as the first/last statement more gracefully.
256
259
260
+ There is also a pretty-printing mode which retains formatting for parts of the AST that have not
261
+ been changed, which requires additional setup.
262
+
257
263
> Read more: [ Pretty printing documentation] ( component/Pretty_printing.markdown )
258
264
259
- Node traversation
260
- -----------------
265
+ Node traversal
266
+ --------------
261
267
262
268
The above pretty printing example used the fact that the source code was known and thus it was easy to
263
269
write code that accesses a certain part of a node tree and changes it. Normally this is not the case.
@@ -272,7 +278,7 @@ use PhpParser\NodeTraverser;
272
278
use PhpParser\ParserFactory;
273
279
use PhpParser\PrettyPrinter;
274
280
275
- $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7 );
281
+ $parser = (new ParserFactory())->createForHostVersion( );
276
282
$traverser = new NodeTraverser;
277
283
$prettyPrinter = new PrettyPrinter\Standard;
278
284
@@ -303,8 +309,7 @@ The corresponding node visitor might look like this:
303
309
use PhpParser\Node;
304
310
use PhpParser\NodeVisitorAbstract;
305
311
306
- class MyNodeVisitor extends NodeVisitorAbstract
307
- {
312
+ class MyNodeVisitor extends NodeVisitorAbstract {
308
313
public function leaveNode(Node $node) {
309
314
if ($node instanceof Node\Scalar\String_) {
310
315
$node->value = 'foo';
@@ -326,7 +331,7 @@ public function afterTraverse(array $nodes);
326
331
```
327
332
328
333
The ` beforeTraverse() ` method is called once before the traversal begins and is passed the nodes the
329
- traverser was called with. This method can be used for resetting values before traversation or
334
+ traverser was called with. This method can be used for resetting values before traversal or
330
335
preparing the tree for traversal.
331
336
332
337
The ` afterTraverse() ` method is similar to the ` beforeTraverse() ` method, with the only difference that
@@ -342,8 +347,8 @@ The `enterNode()` method can additionally return the value `NodeTraverser::DONT_
342
347
which instructs the traverser to skip all children of the current node. To furthermore prevent subsequent
343
348
visitors from visiting the current node, ` NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN ` can be used instead.
344
349
345
- The ` leaveNode() ` method can additionally return the value ` NodeTraverser::REMOVE_NODE ` , in which
346
- case the current node will be removed from the parent array. Furthermore it is possible to return
350
+ Both methods can additionally return the value ` NodeTraverser::REMOVE_NODE ` , in which
351
+ case the current node will be removed from the parent array. Furthermore, it is possible to return
347
352
an array of nodes, which will be merged into the parent array at the offset of the current node.
348
353
I.e. if in ` array(A, B, C) ` the node ` B ` should be replaced with ` array(X, Y, Z) ` the result will
349
354
be ` array(A, X, Y, Z, C) ` .
@@ -372,8 +377,9 @@ unqualified function and constant names. These are resolved at runtime and thus
372
377
know which function they are referring to. In most cases this is a non-issue as the global functions
373
378
are meant.
374
379
375
- Also the ` NameResolver ` adds a ` namespacedName ` subnode to class, function and constant declarations
376
- that contains the namespaced name instead of only the shortname that is available via ` name ` .
380
+ Additionally, the ` NameResolver ` adds a ` namespacedName ` subnode to class, function and constant
381
+ declarations that contains the namespaced name instead of only the shortname that is available via
382
+ ` name ` .
377
383
378
384
> Read more: [ Name resolution documentation] ( component/Name_resolution.markdown )
379
385
@@ -396,7 +402,7 @@ use PhpParser\NodeVisitor\NameResolver;
396
402
$inDir = '/some/path';
397
403
$outDir = '/some/other/path';
398
404
399
- $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7 );
405
+ $parser = (new ParserFactory())->createForNewestSupportedVersion( );
400
406
$traverser = new NodeTraverser;
401
407
$prettyPrinter = new PrettyPrinter\Standard;
402
408
0 commit comments