Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@13
|
5 use PhpParser\Node\Arg;
|
Chris@13
|
6 use PhpParser\Node\Expr;
|
Chris@13
|
7 use PhpParser\Node\Expr\BinaryOp\Concat;
|
Chris@13
|
8 use PhpParser\Node\Identifier;
|
Chris@13
|
9 use PhpParser\Node\Name;
|
Chris@13
|
10 use PhpParser\Node\Scalar\String_;
|
Chris@0
|
11 use PhpParser\Node\Stmt\Use_;
|
Chris@0
|
12
|
Chris@0
|
13 class BuilderFactory
|
Chris@0
|
14 {
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Creates a namespace builder.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @param null|string|Node\Name $name Name of the namespace
|
Chris@0
|
19 *
|
Chris@0
|
20 * @return Builder\Namespace_ The created namespace builder
|
Chris@0
|
21 */
|
Chris@13
|
22 public function namespace($name) : Builder\Namespace_ {
|
Chris@0
|
23 return new Builder\Namespace_($name);
|
Chris@0
|
24 }
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Creates a class builder.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param string $name Name of the class
|
Chris@0
|
30 *
|
Chris@0
|
31 * @return Builder\Class_ The created class builder
|
Chris@0
|
32 */
|
Chris@13
|
33 public function class(string $name) : Builder\Class_ {
|
Chris@0
|
34 return new Builder\Class_($name);
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Creates an interface builder.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param string $name Name of the interface
|
Chris@0
|
41 *
|
Chris@0
|
42 * @return Builder\Interface_ The created interface builder
|
Chris@0
|
43 */
|
Chris@13
|
44 public function interface(string $name) : Builder\Interface_ {
|
Chris@0
|
45 return new Builder\Interface_($name);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Creates a trait builder.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param string $name Name of the trait
|
Chris@0
|
52 *
|
Chris@0
|
53 * @return Builder\Trait_ The created trait builder
|
Chris@0
|
54 */
|
Chris@13
|
55 public function trait(string $name) : Builder\Trait_ {
|
Chris@0
|
56 return new Builder\Trait_($name);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@17
|
60 * Creates a trait use builder.
|
Chris@17
|
61 *
|
Chris@17
|
62 * @param Node\Name|string ...$traits Trait names
|
Chris@17
|
63 *
|
Chris@17
|
64 * @return Builder\TraitUse The create trait use builder
|
Chris@17
|
65 */
|
Chris@17
|
66 public function useTrait(...$traits) : Builder\TraitUse {
|
Chris@17
|
67 return new Builder\TraitUse(...$traits);
|
Chris@17
|
68 }
|
Chris@17
|
69
|
Chris@17
|
70 /**
|
Chris@17
|
71 * Creates a trait use adaptation builder.
|
Chris@17
|
72 *
|
Chris@17
|
73 * @param Node\Name|string|null $trait Trait name
|
Chris@17
|
74 * @param Node\Identifier|string $method Method name
|
Chris@17
|
75 *
|
Chris@17
|
76 * @return Builder\TraitUseAdaptation The create trait use adaptation builder
|
Chris@17
|
77 */
|
Chris@17
|
78 public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation {
|
Chris@17
|
79 if ($method === null) {
|
Chris@17
|
80 $method = $trait;
|
Chris@17
|
81 $trait = null;
|
Chris@17
|
82 }
|
Chris@17
|
83
|
Chris@17
|
84 return new Builder\TraitUseAdaptation($trait, $method);
|
Chris@17
|
85 }
|
Chris@17
|
86
|
Chris@17
|
87 /**
|
Chris@0
|
88 * Creates a method builder.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @param string $name Name of the method
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return Builder\Method The created method builder
|
Chris@0
|
93 */
|
Chris@13
|
94 public function method(string $name) : Builder\Method {
|
Chris@0
|
95 return new Builder\Method($name);
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Creates a parameter builder.
|
Chris@0
|
100 *
|
Chris@0
|
101 * @param string $name Name of the parameter
|
Chris@0
|
102 *
|
Chris@0
|
103 * @return Builder\Param The created parameter builder
|
Chris@0
|
104 */
|
Chris@13
|
105 public function param(string $name) : Builder\Param {
|
Chris@0
|
106 return new Builder\Param($name);
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Creates a property builder.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @param string $name Name of the property
|
Chris@0
|
113 *
|
Chris@0
|
114 * @return Builder\Property The created property builder
|
Chris@0
|
115 */
|
Chris@13
|
116 public function property(string $name) : Builder\Property {
|
Chris@0
|
117 return new Builder\Property($name);
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * Creates a function builder.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @param string $name Name of the function
|
Chris@0
|
124 *
|
Chris@0
|
125 * @return Builder\Function_ The created function builder
|
Chris@0
|
126 */
|
Chris@13
|
127 public function function(string $name) : Builder\Function_ {
|
Chris@0
|
128 return new Builder\Function_($name);
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * Creates a namespace/class use builder.
|
Chris@0
|
133 *
|
Chris@17
|
134 * @param Node\Name|string $name Name of the entity (namespace or class) to alias
|
Chris@0
|
135 *
|
Chris@17
|
136 * @return Builder\Use_ The created use builder
|
Chris@0
|
137 */
|
Chris@13
|
138 public function use($name) : Builder\Use_ {
|
Chris@0
|
139 return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@13
|
142 /**
|
Chris@17
|
143 * Creates a function use builder.
|
Chris@17
|
144 *
|
Chris@17
|
145 * @param Node\Name|string $name Name of the function to alias
|
Chris@17
|
146 *
|
Chris@17
|
147 * @return Builder\Use_ The created use function builder
|
Chris@17
|
148 */
|
Chris@17
|
149 public function useFunction($name) : Builder\Use_ {
|
Chris@17
|
150 return new Builder\Use_($name, Use_::TYPE_FUNCTION);
|
Chris@17
|
151 }
|
Chris@17
|
152
|
Chris@17
|
153 /**
|
Chris@17
|
154 * Creates a constant use builder.
|
Chris@17
|
155 *
|
Chris@17
|
156 * @param Node\Name|string $name Name of the const to alias
|
Chris@17
|
157 *
|
Chris@17
|
158 * @return Builder\Use_ The created use const builder
|
Chris@17
|
159 */
|
Chris@17
|
160 public function useConst($name) : Builder\Use_ {
|
Chris@17
|
161 return new Builder\Use_($name, Use_::TYPE_CONSTANT);
|
Chris@17
|
162 }
|
Chris@17
|
163
|
Chris@17
|
164 /**
|
Chris@13
|
165 * Creates node a for a literal value.
|
Chris@13
|
166 *
|
Chris@13
|
167 * @param Expr|bool|null|int|float|string|array $value $value
|
Chris@13
|
168 *
|
Chris@13
|
169 * @return Expr
|
Chris@13
|
170 */
|
Chris@13
|
171 public function val($value) : Expr {
|
Chris@13
|
172 return BuilderHelpers::normalizeValue($value);
|
Chris@13
|
173 }
|
Chris@13
|
174
|
Chris@13
|
175 /**
|
Chris@17
|
176 * Creates variable node.
|
Chris@17
|
177 *
|
Chris@17
|
178 * @param string|Expr $name Name
|
Chris@17
|
179 *
|
Chris@17
|
180 * @return Expr\Variable
|
Chris@17
|
181 */
|
Chris@17
|
182 public function var($name) : Expr\Variable {
|
Chris@17
|
183 if (!\is_string($name) && !$name instanceof Expr) {
|
Chris@17
|
184 throw new \LogicException('Variable name must be string or Expr');
|
Chris@17
|
185 }
|
Chris@17
|
186
|
Chris@17
|
187 return new Expr\Variable($name);
|
Chris@17
|
188 }
|
Chris@17
|
189
|
Chris@17
|
190 /**
|
Chris@13
|
191 * Normalizes an argument list.
|
Chris@13
|
192 *
|
Chris@13
|
193 * Creates Arg nodes for all arguments and converts literal values to expressions.
|
Chris@13
|
194 *
|
Chris@13
|
195 * @param array $args List of arguments to normalize
|
Chris@13
|
196 *
|
Chris@13
|
197 * @return Arg[]
|
Chris@13
|
198 */
|
Chris@13
|
199 public function args(array $args) : array {
|
Chris@13
|
200 $normalizedArgs = [];
|
Chris@13
|
201 foreach ($args as $arg) {
|
Chris@13
|
202 if ($arg instanceof Arg) {
|
Chris@13
|
203 $normalizedArgs[] = $arg;
|
Chris@13
|
204 } else {
|
Chris@13
|
205 $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
|
Chris@13
|
206 }
|
Chris@13
|
207 }
|
Chris@13
|
208 return $normalizedArgs;
|
Chris@13
|
209 }
|
Chris@13
|
210
|
Chris@13
|
211 /**
|
Chris@13
|
212 * Creates a function call node.
|
Chris@13
|
213 *
|
Chris@13
|
214 * @param string|Name|Expr $name Function name
|
Chris@13
|
215 * @param array $args Function arguments
|
Chris@13
|
216 *
|
Chris@13
|
217 * @return Expr\FuncCall
|
Chris@13
|
218 */
|
Chris@13
|
219 public function funcCall($name, array $args = []) : Expr\FuncCall {
|
Chris@13
|
220 return new Expr\FuncCall(
|
Chris@13
|
221 BuilderHelpers::normalizeNameOrExpr($name),
|
Chris@13
|
222 $this->args($args)
|
Chris@13
|
223 );
|
Chris@13
|
224 }
|
Chris@13
|
225
|
Chris@13
|
226 /**
|
Chris@13
|
227 * Creates a method call node.
|
Chris@13
|
228 *
|
Chris@13
|
229 * @param Expr $var Variable the method is called on
|
Chris@13
|
230 * @param string|Identifier|Expr $name Method name
|
Chris@13
|
231 * @param array $args Method arguments
|
Chris@13
|
232 *
|
Chris@13
|
233 * @return Expr\MethodCall
|
Chris@13
|
234 */
|
Chris@13
|
235 public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
|
Chris@13
|
236 return new Expr\MethodCall(
|
Chris@13
|
237 $var,
|
Chris@13
|
238 BuilderHelpers::normalizeIdentifierOrExpr($name),
|
Chris@13
|
239 $this->args($args)
|
Chris@13
|
240 );
|
Chris@13
|
241 }
|
Chris@13
|
242
|
Chris@13
|
243 /**
|
Chris@13
|
244 * Creates a static method call node.
|
Chris@13
|
245 *
|
Chris@13
|
246 * @param string|Name|Expr $class Class name
|
Chris@13
|
247 * @param string|Identifier|Expr $name Method name
|
Chris@13
|
248 * @param array $args Method arguments
|
Chris@13
|
249 *
|
Chris@13
|
250 * @return Expr\StaticCall
|
Chris@13
|
251 */
|
Chris@13
|
252 public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
|
Chris@13
|
253 return new Expr\StaticCall(
|
Chris@13
|
254 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
255 BuilderHelpers::normalizeIdentifierOrExpr($name),
|
Chris@13
|
256 $this->args($args)
|
Chris@13
|
257 );
|
Chris@13
|
258 }
|
Chris@13
|
259
|
Chris@13
|
260 /**
|
Chris@13
|
261 * Creates an object creation node.
|
Chris@13
|
262 *
|
Chris@13
|
263 * @param string|Name|Expr $class Class name
|
Chris@13
|
264 * @param array $args Constructor arguments
|
Chris@13
|
265 *
|
Chris@13
|
266 * @return Expr\New_
|
Chris@13
|
267 */
|
Chris@13
|
268 public function new($class, array $args = []) : Expr\New_ {
|
Chris@13
|
269 return new Expr\New_(
|
Chris@13
|
270 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
271 $this->args($args)
|
Chris@13
|
272 );
|
Chris@13
|
273 }
|
Chris@13
|
274
|
Chris@13
|
275 /**
|
Chris@13
|
276 * Creates a constant fetch node.
|
Chris@13
|
277 *
|
Chris@13
|
278 * @param string|Name $name Constant name
|
Chris@13
|
279 *
|
Chris@13
|
280 * @return Expr\ConstFetch
|
Chris@13
|
281 */
|
Chris@13
|
282 public function constFetch($name) : Expr\ConstFetch {
|
Chris@13
|
283 return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
|
Chris@13
|
284 }
|
Chris@17
|
285
|
Chris@17
|
286 /**
|
Chris@17
|
287 * Creates a property fetch node.
|
Chris@17
|
288 *
|
Chris@17
|
289 * @param Expr $var Variable holding object
|
Chris@17
|
290 * @param string|Identifier|Expr $name Property name
|
Chris@17
|
291 *
|
Chris@17
|
292 * @return Expr\PropertyFetch
|
Chris@17
|
293 */
|
Chris@17
|
294 public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
|
Chris@17
|
295 return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
|
Chris@17
|
296 }
|
Chris@13
|
297
|
Chris@13
|
298 /**
|
Chris@13
|
299 * Creates a class constant fetch node.
|
Chris@13
|
300 *
|
Chris@13
|
301 * @param string|Name|Expr $class Class name
|
Chris@13
|
302 * @param string|Identifier $name Constant name
|
Chris@13
|
303 *
|
Chris@13
|
304 * @return Expr\ClassConstFetch
|
Chris@13
|
305 */
|
Chris@13
|
306 public function classConstFetch($class, $name): Expr\ClassConstFetch {
|
Chris@13
|
307 return new Expr\ClassConstFetch(
|
Chris@13
|
308 BuilderHelpers::normalizeNameOrExpr($class),
|
Chris@13
|
309 BuilderHelpers::normalizeIdentifier($name)
|
Chris@13
|
310 );
|
Chris@13
|
311 }
|
Chris@13
|
312
|
Chris@13
|
313 /**
|
Chris@13
|
314 * Creates nested Concat nodes from a list of expressions.
|
Chris@13
|
315 *
|
Chris@13
|
316 * @param Expr|string ...$exprs Expressions or literal strings
|
Chris@13
|
317 *
|
Chris@13
|
318 * @return Concat
|
Chris@13
|
319 */
|
Chris@13
|
320 public function concat(...$exprs) : Concat {
|
Chris@13
|
321 $numExprs = count($exprs);
|
Chris@13
|
322 if ($numExprs < 2) {
|
Chris@13
|
323 throw new \LogicException('Expected at least two expressions');
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@13
|
326 $lastConcat = $this->normalizeStringExpr($exprs[0]);
|
Chris@13
|
327 for ($i = 1; $i < $numExprs; $i++) {
|
Chris@13
|
328 $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
|
Chris@13
|
329 }
|
Chris@13
|
330 return $lastConcat;
|
Chris@13
|
331 }
|
Chris@13
|
332
|
Chris@13
|
333 /**
|
Chris@13
|
334 * @param string|Expr $expr
|
Chris@13
|
335 * @return Expr
|
Chris@13
|
336 */
|
Chris@13
|
337 private function normalizeStringExpr($expr) : Expr {
|
Chris@13
|
338 if ($expr instanceof Expr) {
|
Chris@13
|
339 return $expr;
|
Chris@13
|
340 }
|
Chris@13
|
341
|
Chris@13
|
342 if (\is_string($expr)) {
|
Chris@13
|
343 return new String_($expr);
|
Chris@13
|
344 }
|
Chris@13
|
345
|
Chris@13
|
346 throw new \LogicException('Expected string or Expr');
|
Chris@0
|
347 }
|
Chris@0
|
348 }
|