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