BuilderFactory.php 9.8 KB

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