comparison vendor/nikic/php-parser/doc/component/AST_builders.markdown @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 5fb285c0d0e3
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
26 use PhpParser\PrettyPrinter; 26 use PhpParser\PrettyPrinter;
27 use PhpParser\Node; 27 use PhpParser\Node;
28 28
29 $factory = new BuilderFactory; 29 $factory = new BuilderFactory;
30 $node = $factory->namespace('Name\Space') 30 $node = $factory->namespace('Name\Space')
31 ->addStmt($factory->use('Some\Other\Thingy')->as('SomeOtherClass')) 31 ->addStmt($factory->use('Some\Other\Thingy')->as('SomeClass'))
32 ->addStmt($factory->useFunction('strlen'))
33 ->addStmt($factory->useConst('PHP_VERSION'))
32 ->addStmt($factory->class('SomeOtherClass') 34 ->addStmt($factory->class('SomeOtherClass')
33 ->extend('SomeClass') 35 ->extend('SomeClass')
34 ->implement('A\Few', '\Interfaces') 36 ->implement('A\Few', '\Interfaces')
35 ->makeAbstract() // ->makeFinal() 37 ->makeAbstract() // ->makeFinal()
36 38
39 ->addStmt($factory->useTrait('FirstTrait'))
40
41 ->addStmt($factory->useTrait('SecondTrait', 'ThirdTrait')
42 ->and('AnotherTrait')
43 ->with($factory->traitUseAdaptation('foo')->as('bar'))
44 ->with($factory->traitUseAdaptation('AnotherTrait', 'baz')->as('test'))
45 ->with($factory->traitUseAdaptation('AnotherTrait', 'func')->insteadof('SecondTrait')))
46
37 ->addStmt($factory->method('someMethod') 47 ->addStmt($factory->method('someMethod')
38 ->makePublic() 48 ->makePublic()
39 ->makeAbstract() // ->makeFinal() 49 ->makeAbstract() // ->makeFinal()
40 ->setReturnType('bool') 50 ->setReturnType('bool') // ->makeReturnByRef()
41 ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) 51 ->addParam($factory->param('someParam')->setType('SomeClass'))
42 ->setDocComment('/** 52 ->setDocComment('/**
43 * This method does something. 53 * This method does something.
44 * 54 *
45 * @param SomeClass And takes a parameter 55 * @param SomeClass And takes a parameter
46 */') 56 */')
72 <?php 82 <?php
73 83
74 namespace Name\Space; 84 namespace Name\Space;
75 85
76 use Some\Other\Thingy as SomeClass; 86 use Some\Other\Thingy as SomeClass;
87 use function strlen;
88 use const PHP_VERSION;
77 abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces 89 abstract class SomeOtherClass extends SomeClass implements A\Few, \Interfaces
78 { 90 {
91 use FirstTrait;
92 use SecondTrait, ThirdTrait, AnotherTrait {
93 foo as bar;
94 AnotherTrait::baz as test;
95 AnotherTrait::func insteadof SecondTrait;
96 }
79 protected $someProperty; 97 protected $someProperty;
80 private $anotherProperty = array(1, 2, 3); 98 private $anotherProperty = array(1, 2, 3);
81 /** 99 /**
82 * This method does something. 100 * This method does something.
83 * 101 *
96 114
97 The `BuilderFactory` also provides a number of additional helper methods, which directly return 115 The `BuilderFactory` also provides a number of additional helper methods, which directly return
98 nodes. The following methods are currently available: 116 nodes. The following methods are currently available:
99 117
100 * `val($value)`: Creates an AST node for a literal value like `42` or `[1, 2, 3]`. 118 * `val($value)`: Creates an AST node for a literal value like `42` or `[1, 2, 3]`.
119 * `var($name)`: Creates variable node.
101 * `args(array $args)`: Creates an array of function/method arguments, including the required `Arg` 120 * `args(array $args)`: Creates an array of function/method arguments, including the required `Arg`
102 wrappers. Also converts literals to AST nodes. 121 wrappers. Also converts literals to AST nodes.
103 * `funcCall($name, array $args = [])`: Create a function call node. Converts `$name` to a `Name` 122 * `funcCall($name, array $args = [])`: Create a function call node. Converts `$name` to a `Name`
104 node and normalizes arguments. 123 node and normalizes arguments.
105 * `methodCall(Expr $var, $name, array $args = [])`: Create a method call node. Converts `$name` to 124 * `methodCall(Expr $var, $name, array $args = [])`: Create a method call node. Converts `$name` to
109 * `new($class, array $args = [])`: Create a "new" (object creation) node. Converts `$class` to a 128 * `new($class, array $args = [])`: Create a "new" (object creation) node. Converts `$class` to a
110 `Name` node. 129 `Name` node.
111 * `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node. 130 * `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node.
112 * `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a 131 * `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a
113 `Name` node and `$name` to an `Identifier` node. 132 `Name` node and `$name` to an `Identifier` node.
133 * `propertyFetch($var, $name)`: Creates a property fetch node. Converts `$name` to an `Identifier`
134 node.
114 * `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions. 135 * `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions.
115 136
116 These methods may be expanded on an as-needed basis. Please open an issue or PR if a common 137 These methods may be expanded on an as-needed basis. Please open an issue or PR if a common
117 operation is missing. 138 operation is missing.