1 <?php declare(strict_types=1);
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_;
17 * Creates a namespace builder.
19 * @param null|string|Node\Name $name Name of the namespace
21 * @return Builder\Namespace_ The created namespace builder
23 public function namespace($name) : Builder\Namespace_ {
24 return new Builder\Namespace_($name);
28 * Creates a class builder.
30 * @param string $name Name of the class
32 * @return Builder\Class_ The created class builder
34 public function class(string $name) : Builder\Class_ {
35 return new Builder\Class_($name);
39 * Creates an interface builder.
41 * @param string $name Name of the interface
43 * @return Builder\Interface_ The created interface builder
45 public function interface(string $name) : Builder\Interface_ {
46 return new Builder\Interface_($name);
50 * Creates a trait builder.
52 * @param string $name Name of the trait
54 * @return Builder\Trait_ The created trait builder
56 public function trait(string $name) : Builder\Trait_ {
57 return new Builder\Trait_($name);
61 * Creates a method builder.
63 * @param string $name Name of the method
65 * @return Builder\Method The created method builder
67 public function method(string $name) : Builder\Method {
68 return new Builder\Method($name);
72 * Creates a parameter builder.
74 * @param string $name Name of the parameter
76 * @return Builder\Param The created parameter builder
78 public function param(string $name) : Builder\Param {
79 return new Builder\Param($name);
83 * Creates a property builder.
85 * @param string $name Name of the property
87 * @return Builder\Property The created property builder
89 public function property(string $name) : Builder\Property {
90 return new Builder\Property($name);
94 * Creates a function builder.
96 * @param string $name Name of the function
98 * @return Builder\Function_ The created function builder
100 public function function(string $name) : Builder\Function_ {
101 return new Builder\Function_($name);
105 * Creates a namespace/class use builder.
107 * @param string|Node\Name Name to alias
109 * @return Builder\Use_ The create use builder
111 public function use($name) : Builder\Use_ {
112 return new Builder\Use_($name, Use_::TYPE_NORMAL);
116 * Creates node a for a literal value.
118 * @param Expr|bool|null|int|float|string|array $value $value
122 public function val($value) : Expr {
123 return BuilderHelpers::normalizeValue($value);
127 * Normalizes an argument list.
129 * Creates Arg nodes for all arguments and converts literal values to expressions.
131 * @param array $args List of arguments to normalize
135 public function args(array $args) : array {
136 $normalizedArgs = [];
137 foreach ($args as $arg) {
138 if ($arg instanceof Arg) {
139 $normalizedArgs[] = $arg;
141 $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
144 return $normalizedArgs;
148 * Creates a function call node.
150 * @param string|Name|Expr $name Function name
151 * @param array $args Function arguments
153 * @return Expr\FuncCall
155 public function funcCall($name, array $args = []) : Expr\FuncCall {
156 return new Expr\FuncCall(
157 BuilderHelpers::normalizeNameOrExpr($name),
163 * Creates a method call node.
165 * @param Expr $var Variable the method is called on
166 * @param string|Identifier|Expr $name Method name
167 * @param array $args Method arguments
169 * @return Expr\MethodCall
171 public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
172 return new Expr\MethodCall(
174 BuilderHelpers::normalizeIdentifierOrExpr($name),
180 * Creates a static method call node.
182 * @param string|Name|Expr $class Class name
183 * @param string|Identifier|Expr $name Method name
184 * @param array $args Method arguments
186 * @return Expr\StaticCall
188 public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
189 return new Expr\StaticCall(
190 BuilderHelpers::normalizeNameOrExpr($class),
191 BuilderHelpers::normalizeIdentifierOrExpr($name),
197 * Creates an object creation node.
199 * @param string|Name|Expr $class Class name
200 * @param array $args Constructor arguments
204 public function new($class, array $args = []) : Expr\New_ {
205 return new Expr\New_(
206 BuilderHelpers::normalizeNameOrExpr($class),
212 * Creates a constant fetch node.
214 * @param string|Name $name Constant name
216 * @return Expr\ConstFetch
218 public function constFetch($name) : Expr\ConstFetch {
219 return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
223 * Creates a class constant fetch node.
225 * @param string|Name|Expr $class Class name
226 * @param string|Identifier $name Constant name
228 * @return Expr\ClassConstFetch
230 public function classConstFetch($class, $name): Expr\ClassConstFetch {
231 return new Expr\ClassConstFetch(
232 BuilderHelpers::normalizeNameOrExpr($class),
233 BuilderHelpers::normalizeIdentifier($name)
238 * Creates nested Concat nodes from a list of expressions.
240 * @param Expr|string ...$exprs Expressions or literal strings
244 public function concat(...$exprs) : Concat {
245 $numExprs = count($exprs);
247 throw new \LogicException('Expected at least two expressions');
250 $lastConcat = $this->normalizeStringExpr($exprs[0]);
251 for ($i = 1; $i < $numExprs; $i++) {
252 $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
258 * @param string|Expr $expr
261 private function normalizeStringExpr($expr) : Expr {
262 if ($expr instanceof Expr) {
266 if (\is_string($expr)) {
267 return new String_($expr);
270 throw new \LogicException('Expected string or Expr');