c28f40fa1289a16be10734514bbcb7c726ea8708
[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 method builder.
62      *
63      * @param string $name Name of the method
64      *
65      * @return Builder\Method The created method builder
66      */
67     public function method(string $name) : Builder\Method {
68         return new Builder\Method($name);
69     }
70
71     /**
72      * Creates a parameter builder.
73      *
74      * @param string $name Name of the parameter
75      *
76      * @return Builder\Param The created parameter builder
77      */
78     public function param(string $name) : Builder\Param {
79         return new Builder\Param($name);
80     }
81
82     /**
83      * Creates a property builder.
84      *
85      * @param string $name Name of the property
86      *
87      * @return Builder\Property The created property builder
88      */
89     public function property(string $name) : Builder\Property {
90         return new Builder\Property($name);
91     }
92
93     /**
94      * Creates a function builder.
95      *
96      * @param string $name Name of the function
97      *
98      * @return Builder\Function_ The created function builder
99      */
100     public function function(string $name) : Builder\Function_ {
101         return new Builder\Function_($name);
102     }
103
104     /**
105      * Creates a namespace/class use builder.
106      *
107      * @param string|Node\Name Name to alias
108      *
109      * @return Builder\Use_ The create use builder
110      */
111     public function use($name) : Builder\Use_ {
112         return new Builder\Use_($name, Use_::TYPE_NORMAL);
113     }
114
115     /**
116      * Creates node a for a literal value.
117      *
118      * @param Expr|bool|null|int|float|string|array $value $value
119      *
120      * @return Expr
121      */
122     public function val($value) : Expr {
123         return BuilderHelpers::normalizeValue($value);
124     }
125
126     /**
127      * Normalizes an argument list.
128      *
129      * Creates Arg nodes for all arguments and converts literal values to expressions.
130      *
131      * @param array $args List of arguments to normalize
132      *
133      * @return Arg[]
134      */
135     public function args(array $args) : array {
136         $normalizedArgs = [];
137         foreach ($args as $arg) {
138             if ($arg instanceof Arg) {
139                 $normalizedArgs[] = $arg;
140             } else {
141                 $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
142             }
143         }
144         return $normalizedArgs;
145     }
146
147     /**
148      * Creates a function call node.
149      *
150      * @param string|Name|Expr $name Function name
151      * @param array            $args Function arguments
152      *
153      * @return Expr\FuncCall
154      */
155     public function funcCall($name, array $args = []) : Expr\FuncCall {
156         return new Expr\FuncCall(
157             BuilderHelpers::normalizeNameOrExpr($name),
158             $this->args($args)
159         );
160     }
161
162     /**
163      * Creates a method call node.
164      *
165      * @param Expr                   $var  Variable the method is called on
166      * @param string|Identifier|Expr $name Method name
167      * @param array                  $args Method arguments
168      *
169      * @return Expr\MethodCall
170      */
171     public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
172         return new Expr\MethodCall(
173             $var,
174             BuilderHelpers::normalizeIdentifierOrExpr($name),
175             $this->args($args)
176         );
177     }
178
179     /**
180      * Creates a static method call node.
181      *
182      * @param string|Name|Expr       $class Class name
183      * @param string|Identifier|Expr $name  Method name
184      * @param array                  $args  Method arguments
185      *
186      * @return Expr\StaticCall
187      */
188     public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
189         return new Expr\StaticCall(
190             BuilderHelpers::normalizeNameOrExpr($class),
191             BuilderHelpers::normalizeIdentifierOrExpr($name),
192             $this->args($args)
193         );
194     }
195
196     /**
197      * Creates an object creation node.
198      *
199      * @param string|Name|Expr $class Class name
200      * @param array            $args  Constructor arguments
201      *
202      * @return Expr\New_
203      */
204     public function new($class, array $args = []) : Expr\New_ {
205         return new Expr\New_(
206             BuilderHelpers::normalizeNameOrExpr($class),
207             $this->args($args)
208         );
209     }
210
211     /**
212      * Creates a constant fetch node.
213      *
214      * @param string|Name $name Constant name
215      *
216      * @return Expr\ConstFetch
217      */
218     public function constFetch($name) : Expr\ConstFetch {
219         return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
220     }
221
222     /**
223      * Creates a class constant fetch node.
224      *
225      * @param string|Name|Expr  $class Class name
226      * @param string|Identifier $name  Constant name
227      *
228      * @return Expr\ClassConstFetch
229      */
230     public function classConstFetch($class, $name): Expr\ClassConstFetch {
231         return new Expr\ClassConstFetch(
232             BuilderHelpers::normalizeNameOrExpr($class),
233             BuilderHelpers::normalizeIdentifier($name)
234         );
235     }
236
237     /**
238      * Creates nested Concat nodes from a list of expressions.
239      *
240      * @param Expr|string ...$exprs Expressions or literal strings
241      *
242      * @return Concat
243      */
244     public function concat(...$exprs) : Concat {
245         $numExprs = count($exprs);
246         if ($numExprs < 2) {
247             throw new \LogicException('Expected at least two expressions');
248         }
249
250         $lastConcat = $this->normalizeStringExpr($exprs[0]);
251         for ($i = 1; $i < $numExprs; $i++) {
252             $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
253         }
254         return $lastConcat;
255     }
256
257     /**
258      * @param string|Expr $expr
259      * @return Expr
260      */
261     private function normalizeStringExpr($expr) : Expr {
262         if ($expr instanceof Expr) {
263             return $expr;
264         }
265
266         if (\is_string($expr)) {
267             return new String_($expr);
268         }
269
270         throw new \LogicException('Expected string or Expr');
271     }
272 }