Chris@13
|
1 AST builders
|
Chris@13
|
2 ============
|
Chris@13
|
3
|
Chris@13
|
4 When PHP-Parser is used to generate (or modify) code by first creating an Abstract Syntax Tree and
|
Chris@13
|
5 then using the [pretty printer](Pretty_printing.markdown) to convert it to PHP code, it can often
|
Chris@13
|
6 be tedious to manually construct AST nodes. The project provides a number of utilities to simplify
|
Chris@13
|
7 the construction of common AST nodes.
|
Chris@13
|
8
|
Chris@13
|
9 Fluent builders
|
Chris@13
|
10 ---------------
|
Chris@13
|
11
|
Chris@13
|
12 The library comes with a number of builders, which allow creating node trees using a fluent
|
Chris@13
|
13 interface. Builders are created using the `BuilderFactory` and the final constructed node is
|
Chris@13
|
14 accessed through `getNode()`. Fluent builders are available for
|
Chris@13
|
15 the following syntactic elements:
|
Chris@13
|
16
|
Chris@13
|
17 * namespaces and use statements
|
Chris@13
|
18 * classes, interfaces and traits
|
Chris@13
|
19 * methods, functions and parameters
|
Chris@13
|
20 * properties
|
Chris@13
|
21
|
Chris@13
|
22 Here is an example:
|
Chris@13
|
23
|
Chris@13
|
24 ```php
|
Chris@13
|
25 use PhpParser\BuilderFactory;
|
Chris@13
|
26 use PhpParser\PrettyPrinter;
|
Chris@13
|
27 use PhpParser\Node;
|
Chris@13
|
28
|
Chris@13
|
29 $factory = new BuilderFactory;
|
Chris@13
|
30 $node = $factory->namespace('Name\Space')
|
Chris@13
|
31 ->addStmt($factory->use('Some\Other\Thingy')->as('SomeOtherClass'))
|
Chris@13
|
32 ->addStmt($factory->class('SomeOtherClass')
|
Chris@13
|
33 ->extend('SomeClass')
|
Chris@13
|
34 ->implement('A\Few', '\Interfaces')
|
Chris@13
|
35 ->makeAbstract() // ->makeFinal()
|
Chris@13
|
36
|
Chris@13
|
37 ->addStmt($factory->method('someMethod')
|
Chris@13
|
38 ->makePublic()
|
Chris@13
|
39 ->makeAbstract() // ->makeFinal()
|
Chris@13
|
40 ->setReturnType('bool')
|
Chris@13
|
41 ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
Chris@13
|
42 ->setDocComment('/**
|
Chris@13
|
43 * This method does something.
|
Chris@13
|
44 *
|
Chris@13
|
45 * @param SomeClass And takes a parameter
|
Chris@13
|
46 */')
|
Chris@13
|
47 )
|
Chris@13
|
48
|
Chris@13
|
49 ->addStmt($factory->method('anotherMethod')
|
Chris@13
|
50 ->makeProtected() // ->makePublic() [default], ->makePrivate()
|
Chris@13
|
51 ->addParam($factory->param('someParam')->setDefault('test'))
|
Chris@13
|
52 // it is possible to add manually created nodes
|
Chris@13
|
53 ->addStmt(new Node\Expr\Print_(new Node\Expr\Variable('someParam')))
|
Chris@13
|
54 )
|
Chris@13
|
55
|
Chris@13
|
56 // properties will be correctly reordered above the methods
|
Chris@13
|
57 ->addStmt($factory->property('someProperty')->makeProtected())
|
Chris@13
|
58 ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3)))
|
Chris@13
|
59 )
|
Chris@13
|
60
|
Chris@13
|
61 ->getNode()
|
Chris@13
|
62 ;
|
Chris@13
|
63
|
Chris@13
|
64 $stmts = array($node);
|
Chris@13
|
65 $prettyPrinter = new PrettyPrinter\Standard();
|
Chris@13
|
66 echo $prettyPrinter->prettyPrintFile($stmts);
|
Chris@13
|
67 ```
|
Chris@13
|
68
|
Chris@13
|
69 This will produce the following output with the standard pretty printer:
|
Chris@13
|
70
|
Chris@13
|
71 ```php
|
Chris@13
|
72 <?php
|
Chris@13
|
73
|
Chris@13
|
74 namespace Name\Space;
|
Chris@13
|
75
|
Chris@13
|
76 use Some\Other\Thingy as SomeClass;
|
Chris@13
|
77 abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
|
Chris@13
|
78 {
|
Chris@13
|
79 protected $someProperty;
|
Chris@13
|
80 private $anotherProperty = array(1, 2, 3);
|
Chris@13
|
81 /**
|
Chris@13
|
82 * This method does something.
|
Chris@13
|
83 *
|
Chris@13
|
84 * @param SomeClass And takes a parameter
|
Chris@13
|
85 */
|
Chris@13
|
86 public abstract function someMethod(SomeClass $someParam) : bool;
|
Chris@13
|
87 protected function anotherMethod($someParam = 'test')
|
Chris@13
|
88 {
|
Chris@13
|
89 print $someParam;
|
Chris@13
|
90 }
|
Chris@13
|
91 }
|
Chris@13
|
92 ```
|
Chris@13
|
93
|
Chris@13
|
94 Additional helper methods
|
Chris@13
|
95 -------------------------
|
Chris@13
|
96
|
Chris@13
|
97 The `BuilderFactory` also provides a number of additional helper methods, which directly return
|
Chris@13
|
98 nodes. The following methods are currently available:
|
Chris@13
|
99
|
Chris@13
|
100 * `val($value)`: Creates an AST node for a literal value like `42` or `[1, 2, 3]`.
|
Chris@13
|
101 * `args(array $args)`: Creates an array of function/method arguments, including the required `Arg`
|
Chris@13
|
102 wrappers. Also converts literals to AST nodes.
|
Chris@13
|
103 * `funcCall($name, array $args = [])`: Create a function call node. Converts `$name` to a `Name`
|
Chris@13
|
104 node and normalizes arguments.
|
Chris@13
|
105 * `methodCall(Expr $var, $name, array $args = [])`: Create a method call node. Converts `$name` to
|
Chris@13
|
106 an `Identifier` node and normalizes arguments.
|
Chris@13
|
107 * `staticCall($class, $name, array $args = [])`: Create a static method call node. Converts
|
Chris@13
|
108 `$class` to a `Name` node, `$name` to an `Identifier` node and normalizes arguments.
|
Chris@13
|
109 * `new($class, array $args = [])`: Create a "new" (object creation) node. Converts `$class` to a
|
Chris@13
|
110 `Name` node.
|
Chris@13
|
111 * `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node.
|
Chris@13
|
112 * `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a
|
Chris@13
|
113 `Name` node and `$name` to an `Identifier` node.
|
Chris@13
|
114 * `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions.
|
Chris@13
|
115
|
Chris@13
|
116 These methods may be expanded on an as-needed basis. Please open an issue or PR if a common
|
Chris@13
|
117 operation is missing. |