2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-10 17:56:56 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2017-04-28 18:13:06 +02:00
|
|
|
use PhpParser\Node\Arg;
|
2017-04-24 21:42:59 +02:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
2018-03-03 15:40:51 +01:00
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
2017-04-24 21:42:59 +02:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2015-03-10 16:05:55 +01:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
class BuilderFactory
|
2012-03-10 17:56:56 +01:00
|
|
|
{
|
2014-12-11 17:24:10 +01:00
|
|
|
/**
|
|
|
|
* Creates a namespace builder.
|
2016-04-09 11:41:38 +02:00
|
|
|
*
|
2015-03-10 16:05:55 +01:00
|
|
|
* @param null|string|Node\Name $name Name of the namespace
|
2014-12-11 17:24:10 +01:00
|
|
|
*
|
|
|
|
* @return Builder\Namespace_ The created namespace builder
|
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function namespace($name) : Builder\Namespace_ {
|
2014-12-11 17:24:10 +01:00
|
|
|
return new Builder\Namespace_($name);
|
|
|
|
}
|
|
|
|
|
2012-03-11 09:02:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a class builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the class
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Class_ The created class builder
|
2012-03-10 17:56:56 +01:00
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function class(string $name) : Builder\Class_ {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Class_($name);
|
2012-03-10 17:56:56 +01:00
|
|
|
}
|
|
|
|
|
2013-05-23 10:31:59 +02:00
|
|
|
/**
|
2014-12-19 17:58:47 +01:00
|
|
|
* Creates an interface builder.
|
2013-05-23 10:31:59 +02:00
|
|
|
*
|
|
|
|
* @param string $name Name of the interface
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Interface_ The created interface builder
|
2013-05-23 10:31:59 +02:00
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function interface(string $name) : Builder\Interface_ {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Interface_($name);
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Creates a trait builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the trait
|
|
|
|
*
|
|
|
|
* @return Builder\Trait_ The created trait builder
|
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function trait(string $name) : Builder\Trait_ {
|
2014-12-19 17:58:47 +01:00
|
|
|
return new Builder\Trait_($name);
|
|
|
|
}
|
|
|
|
|
2018-07-20 02:28:57 +02:00
|
|
|
/**
|
|
|
|
* Creates a trait use builder.
|
|
|
|
*
|
|
|
|
* @param Node\Name|string ...$traits Trait names
|
|
|
|
*
|
|
|
|
* @return Builder\TraitUse The create trait use builder
|
|
|
|
*/
|
|
|
|
public function useTrait(...$traits) : Builder\TraitUse {
|
|
|
|
return new Builder\TraitUse(...$traits);
|
|
|
|
}
|
|
|
|
|
2018-07-20 12:24:40 +02:00
|
|
|
/**
|
|
|
|
* Creates a trait use adaptation builder.
|
|
|
|
*
|
|
|
|
* @param Node\Name|string|null $trait Trait name
|
|
|
|
* @param Node\Identifier|string $method Method name
|
|
|
|
*
|
|
|
|
* @return Builder\TraitUseAdaptation The create trait use adaptation builder
|
|
|
|
*/
|
|
|
|
public function traitUseAdaptation($trait, $method = null) : Builder\TraitUseAdaptation {
|
2019-01-19 11:18:00 +01:00
|
|
|
if ($method === null) {
|
2018-07-20 12:24:40 +02:00
|
|
|
$method = $trait;
|
|
|
|
$trait = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Builder\TraitUseAdaptation($trait, $method);
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Creates a method builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the method
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Method The created method builder
|
2012-03-10 23:25:26 +01:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function method(string $name) : Builder\Method {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Method($name);
|
2012-03-10 23:25:26 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 00:06:02 +01:00
|
|
|
/**
|
|
|
|
* Creates a parameter builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the parameter
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Param The created parameter builder
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function param(string $name) : Builder\Param {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Param($name);
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 08:42:13 +01:00
|
|
|
/**
|
|
|
|
* Creates a property builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the property
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Property The created property builder
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function property(string $name) : Builder\Property {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Property($name);
|
2012-03-11 08:42:13 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 09:02:52 +01:00
|
|
|
/**
|
|
|
|
* Creates a function builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the function
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Builder\Function_ The created function builder
|
2012-03-11 09:02:52 +01:00
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function function(string $name) : Builder\Function_ {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Builder\Function_($name);
|
2012-03-11 09:02:52 +01:00
|
|
|
}
|
|
|
|
|
2015-03-10 16:05:55 +01:00
|
|
|
/**
|
|
|
|
* Creates a namespace/class use builder.
|
|
|
|
*
|
2018-07-19 16:54:04 +02:00
|
|
|
* @param Node\Name|string $name Name of the entity (namespace or class) to alias
|
2015-03-10 16:05:55 +01:00
|
|
|
*
|
2018-07-20 21:43:43 +02:00
|
|
|
* @return Builder\Use_ The created use builder
|
2015-03-10 16:05:55 +01:00
|
|
|
*/
|
2017-09-02 20:06:50 +02:00
|
|
|
public function use($name) : Builder\Use_ {
|
2015-03-10 16:05:55 +01:00
|
|
|
return new Builder\Use_($name, Use_::TYPE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:54:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a function use builder.
|
|
|
|
*
|
|
|
|
* @param Node\Name|string $name Name of the function to alias
|
|
|
|
*
|
2018-07-20 21:43:43 +02:00
|
|
|
* @return Builder\Use_ The created use function builder
|
2018-07-19 16:54:04 +02:00
|
|
|
*/
|
|
|
|
public function useFunction($name) : Builder\Use_ {
|
|
|
|
return new Builder\Use_($name, Use_::TYPE_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a constant use builder.
|
|
|
|
*
|
|
|
|
* @param Node\Name|string $name Name of the const to alias
|
|
|
|
*
|
2018-07-20 21:43:43 +02:00
|
|
|
* @return Builder\Use_ The created use const builder
|
2018-07-19 16:54:04 +02:00
|
|
|
*/
|
|
|
|
public function useConst($name) : Builder\Use_ {
|
|
|
|
return new Builder\Use_($name, Use_::TYPE_CONSTANT);
|
|
|
|
}
|
|
|
|
|
2021-03-21 20:58:21 +01:00
|
|
|
/**
|
|
|
|
* Creates a class constant builder.
|
|
|
|
*
|
|
|
|
* @param string|Identifier $name Name
|
|
|
|
* @param Node\Expr|bool|null|int|float|string|array $value Value
|
|
|
|
*
|
|
|
|
* @return Builder\ClassConst The created use const builder
|
|
|
|
*/
|
|
|
|
public function classConst($name, $value) : Builder\ClassConst {
|
|
|
|
return new Builder\ClassConst($name, $value);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:28:03 +02:00
|
|
|
/**
|
|
|
|
* Creates node a for a literal value.
|
|
|
|
*
|
2017-04-24 21:42:59 +02:00
|
|
|
* @param Expr|bool|null|int|float|string|array $value $value
|
2017-04-24 21:28:03 +02:00
|
|
|
*
|
2017-04-24 21:42:59 +02:00
|
|
|
* @return Expr
|
2017-04-24 21:28:03 +02:00
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function val($value) : Expr {
|
2017-04-24 21:28:03 +02:00
|
|
|
return BuilderHelpers::normalizeValue($value);
|
|
|
|
}
|
|
|
|
|
2018-07-20 13:39:38 +02:00
|
|
|
/**
|
|
|
|
* Creates variable node.
|
|
|
|
*
|
|
|
|
* @param string|Expr $name Name
|
|
|
|
*
|
|
|
|
* @return Expr\Variable
|
|
|
|
*/
|
|
|
|
public function var($name) : Expr\Variable {
|
2018-07-20 21:59:42 +02:00
|
|
|
if (!\is_string($name) && !$name instanceof Expr) {
|
|
|
|
throw new \LogicException('Variable name must be string or Expr');
|
|
|
|
}
|
|
|
|
|
2018-07-20 13:39:38 +02:00
|
|
|
return new Expr\Variable($name);
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:13:06 +02:00
|
|
|
/**
|
|
|
|
* 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[]
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function args(array $args) : array {
|
2017-04-28 18:13:06 +02:00
|
|
|
$normalizedArgs = [];
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
if ($arg instanceof Arg) {
|
|
|
|
$normalizedArgs[] = $arg;
|
|
|
|
} else {
|
|
|
|
$normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $normalizedArgs;
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:40:51 +01:00
|
|
|
/**
|
|
|
|
* 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,
|
2018-03-03 22:14:42 +01:00
|
|
|
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
2018-03-03 15:40:51 +01:00
|
|
|
$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),
|
2018-03-03 22:14:42 +01:00
|
|
|
BuilderHelpers::normalizeIdentifierOrExpr($name),
|
2018-03-03 15:40:51 +01:00
|
|
|
$this->args($args)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-03 22:25:58 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-03 22:14:42 +01:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
2018-07-20 13:39:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a property fetch node.
|
|
|
|
*
|
|
|
|
* @param Expr $var Variable holding object
|
|
|
|
* @param string|Identifier|Expr $name Property name
|
|
|
|
*
|
|
|
|
* @return Expr\PropertyFetch
|
|
|
|
*/
|
|
|
|
public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
|
2018-07-20 21:59:42 +02:00
|
|
|
return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
|
2018-07-20 13:39:38 +02:00
|
|
|
}
|
2018-03-03 22:14:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:42:59 +02:00
|
|
|
/**
|
|
|
|
* Creates nested Concat nodes from a list of expressions.
|
|
|
|
*
|
|
|
|
* @param Expr|string ...$exprs Expressions or literal strings
|
|
|
|
*
|
|
|
|
* @return Concat
|
|
|
|
*/
|
2017-04-28 21:40:59 +02:00
|
|
|
public function concat(...$exprs) : Concat {
|
2017-04-24 21:42:59 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:40:51 +01:00
|
|
|
/**
|
|
|
|
* @param string|Expr $expr
|
|
|
|
* @return Expr
|
|
|
|
*/
|
|
|
|
private function normalizeStringExpr($expr) : Expr {
|
2017-04-24 21:42:59 +02:00
|
|
|
if ($expr instanceof Expr) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
|
2018-03-03 15:40:51 +01:00
|
|
|
if (\is_string($expr)) {
|
2017-04-24 21:42:59 +02:00
|
|
|
return new String_($expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \LogicException('Expected string or Expr');
|
|
|
|
}
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|