BuilderFactory.php 10 KB

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