-<?php
+<?php declare(strict_types=1);
namespace PhpParser;
-use PhpParser\Builder;
+use PhpParser\Node\Arg;
+use PhpParser\Node\Expr;
+use PhpParser\Node\Expr\BinaryOp\Concat;
+use PhpParser\Node\Identifier;
+use PhpParser\Node\Name;
+use PhpParser\Node\Scalar\String_;
+use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
-/**
- * The following methods use reserved keywords, so their implementation is defined with an underscore and made available
- * with the reserved name through __call() magic.
- *
- * @method Builder\Namespace_ namespace(string $name) Creates a namespace builder.
- * @method Builder\Class_ class(string $name) Creates a class builder.
- * @method Builder\Interface_ interface(string $name) Creates an interface builder.
- * @method Builder\Trait_ trait(string $name) Creates a trait builder.
- * @method Builder\Function_ function(string $name) Creates a function builder.
- * @method Builder\Use_ use(string $name) Creates a namespace/class use builder.
- */
class BuilderFactory
{
/**
*
* @return Builder\Namespace_ The created namespace builder
*/
- protected function _namespace($name) {
+ public function namespace($name) : Builder\Namespace_ {
return new Builder\Namespace_($name);
}
*
* @return Builder\Class_ The created class builder
*/
- protected function _class($name) {
+ public function class(string $name) : Builder\Class_ {
return new Builder\Class_($name);
}
*
* @return Builder\Interface_ The created interface builder
*/
- protected function _interface($name) {
+ public function interface(string $name) : Builder\Interface_ {
return new Builder\Interface_($name);
}
*
* @return Builder\Trait_ The created trait builder
*/
- protected function _trait($name) {
+ public function trait(string $name) : Builder\Trait_ {
return new Builder\Trait_($name);
}
*
* @return Builder\Method The created method builder
*/
- public function method($name) {
+ public function method(string $name) : Builder\Method {
return new Builder\Method($name);
}
*
* @return Builder\Param The created parameter builder
*/
- public function param($name) {
+ public function param(string $name) : Builder\Param {
return new Builder\Param($name);
}
*
* @return Builder\Property The created property builder
*/
- public function property($name) {
+ public function property(string $name) : Builder\Property {
return new Builder\Property($name);
}
*
* @return Builder\Function_ The created function builder
*/
- protected function _function($name) {
+ public function function(string $name) : Builder\Function_ {
return new Builder\Function_($name);
}
*
* @return Builder\Use_ The create use builder
*/
- protected function _use($name) {
+ public function use($name) : Builder\Use_ {
return new Builder\Use_($name, Use_::TYPE_NORMAL);
}
- public function __call($name, array $args) {
- if (method_exists($this, '_' . $name)) {
- return call_user_func_array(array($this, '_' . $name), $args);
+ /**
+ * Creates node a for a literal value.
+ *
+ * @param Expr|bool|null|int|float|string|array $value $value
+ *
+ * @return Expr
+ */
+ public function val($value) : Expr {
+ return BuilderHelpers::normalizeValue($value);
+ }
+
+ /**
+ * Normalizes an argument list.
+ *
+ * Creates Arg nodes for all arguments and converts literal values to expressions.
+ *
+ * @param array $args List of arguments to normalize
+ *
+ * @return Arg[]
+ */
+ public function args(array $args) : array {
+ $normalizedArgs = [];
+ foreach ($args as $arg) {
+ if ($arg instanceof Arg) {
+ $normalizedArgs[] = $arg;
+ } else {
+ $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
+ }
+ }
+ return $normalizedArgs;
+ }
+
+ /**
+ * Creates a function call node.
+ *
+ * @param string|Name|Expr $name Function name
+ * @param array $args Function arguments
+ *
+ * @return Expr\FuncCall
+ */
+ public function funcCall($name, array $args = []) : Expr\FuncCall {
+ return new Expr\FuncCall(
+ BuilderHelpers::normalizeNameOrExpr($name),
+ $this->args($args)
+ );
+ }
+
+ /**
+ * Creates a method call node.
+ *
+ * @param Expr $var Variable the method is called on
+ * @param string|Identifier|Expr $name Method name
+ * @param array $args Method arguments
+ *
+ * @return Expr\MethodCall
+ */
+ public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
+ return new Expr\MethodCall(
+ $var,
+ BuilderHelpers::normalizeIdentifierOrExpr($name),
+ $this->args($args)
+ );
+ }
+
+ /**
+ * Creates a static method call node.
+ *
+ * @param string|Name|Expr $class Class name
+ * @param string|Identifier|Expr $name Method name
+ * @param array $args Method arguments
+ *
+ * @return Expr\StaticCall
+ */
+ public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
+ return new Expr\StaticCall(
+ BuilderHelpers::normalizeNameOrExpr($class),
+ BuilderHelpers::normalizeIdentifierOrExpr($name),
+ $this->args($args)
+ );
+ }
+
+ /**
+ * Creates an object creation node.
+ *
+ * @param string|Name|Expr $class Class name
+ * @param array $args Constructor arguments
+ *
+ * @return Expr\New_
+ */
+ public function new($class, array $args = []) : Expr\New_ {
+ return new Expr\New_(
+ BuilderHelpers::normalizeNameOrExpr($class),
+ $this->args($args)
+ );
+ }
+
+ /**
+ * Creates a constant fetch node.
+ *
+ * @param string|Name $name Constant name
+ *
+ * @return Expr\ConstFetch
+ */
+ public function constFetch($name) : Expr\ConstFetch {
+ return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
+ }
+
+ /**
+ * Creates a class constant fetch node.
+ *
+ * @param string|Name|Expr $class Class name
+ * @param string|Identifier $name Constant name
+ *
+ * @return Expr\ClassConstFetch
+ */
+ public function classConstFetch($class, $name): Expr\ClassConstFetch {
+ return new Expr\ClassConstFetch(
+ BuilderHelpers::normalizeNameOrExpr($class),
+ BuilderHelpers::normalizeIdentifier($name)
+ );
+ }
+
+ /**
+ * Creates nested Concat nodes from a list of expressions.
+ *
+ * @param Expr|string ...$exprs Expressions or literal strings
+ *
+ * @return Concat
+ */
+ public function concat(...$exprs) : Concat {
+ $numExprs = count($exprs);
+ if ($numExprs < 2) {
+ throw new \LogicException('Expected at least two expressions');
+ }
+
+ $lastConcat = $this->normalizeStringExpr($exprs[0]);
+ for ($i = 1; $i < $numExprs; $i++) {
+ $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
+ }
+ return $lastConcat;
+ }
+
+ /**
+ * @param string|Expr $expr
+ * @return Expr
+ */
+ private function normalizeStringExpr($expr) : Expr {
+ if ($expr instanceof Expr) {
+ return $expr;
+ }
+
+ if (\is_string($expr)) {
+ return new String_($expr);
}
- throw new \LogicException(sprintf('Method "%s" does not exist', $name));
+ throw new \LogicException('Expected string or Expr');
}
}