BuilderFactory.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 node a for a literal value.
  149. *
  150. * @param Expr|bool|null|int|float|string|array $value $value
  151. *
  152. * @return Expr
  153. */
  154. public function val($value) : Expr {
  155. return BuilderHelpers::normalizeValue($value);
  156. }
  157. /**
  158. * Creates variable node.
  159. *
  160. * @param string|Expr $name Name
  161. *
  162. * @return Expr\Variable
  163. */
  164. public function var($name) : Expr\Variable {
  165. if (!\is_string($name) && !$name instanceof Expr) {
  166. throw new \LogicException('Variable name must be string or Expr');
  167. }
  168. return new Expr\Variable($name);
  169. }
  170. /**
  171. * Normalizes an argument list.
  172. *
  173. * Creates Arg nodes for all arguments and converts literal values to expressions.
  174. *
  175. * @param array $args List of arguments to normalize
  176. *
  177. * @return Arg[]
  178. */
  179. public function args(array $args) : array {
  180. $normalizedArgs = [];
  181. foreach ($args as $arg) {
  182. if ($arg instanceof Arg) {
  183. $normalizedArgs[] = $arg;
  184. } else {
  185. $normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
  186. }
  187. }
  188. return $normalizedArgs;
  189. }
  190. /**
  191. * Creates a function call node.
  192. *
  193. * @param string|Name|Expr $name Function name
  194. * @param array $args Function arguments
  195. *
  196. * @return Expr\FuncCall
  197. */
  198. public function funcCall($name, array $args = []) : Expr\FuncCall {
  199. return new Expr\FuncCall(
  200. BuilderHelpers::normalizeNameOrExpr($name),
  201. $this->args($args)
  202. );
  203. }
  204. /**
  205. * Creates a method call node.
  206. *
  207. * @param Expr $var Variable the method is called on
  208. * @param string|Identifier|Expr $name Method name
  209. * @param array $args Method arguments
  210. *
  211. * @return Expr\MethodCall
  212. */
  213. public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
  214. return new Expr\MethodCall(
  215. $var,
  216. BuilderHelpers::normalizeIdentifierOrExpr($name),
  217. $this->args($args)
  218. );
  219. }
  220. /**
  221. * Creates a static method call node.
  222. *
  223. * @param string|Name|Expr $class Class name
  224. * @param string|Identifier|Expr $name Method name
  225. * @param array $args Method arguments
  226. *
  227. * @return Expr\StaticCall
  228. */
  229. public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
  230. return new Expr\StaticCall(
  231. BuilderHelpers::normalizeNameOrExpr($class),
  232. BuilderHelpers::normalizeIdentifierOrExpr($name),
  233. $this->args($args)
  234. );
  235. }
  236. /**
  237. * Creates an object creation node.
  238. *
  239. * @param string|Name|Expr $class Class name
  240. * @param array $args Constructor arguments
  241. *
  242. * @return Expr\New_
  243. */
  244. public function new($class, array $args = []) : Expr\New_ {
  245. return new Expr\New_(
  246. BuilderHelpers::normalizeNameOrExpr($class),
  247. $this->args($args)
  248. );
  249. }
  250. /**
  251. * Creates a constant fetch node.
  252. *
  253. * @param string|Name $name Constant name
  254. *
  255. * @return Expr\ConstFetch
  256. */
  257. public function constFetch($name) : Expr\ConstFetch {
  258. return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
  259. }
  260. /**
  261. * Creates a property fetch node.
  262. *
  263. * @param Expr $var Variable holding object
  264. * @param string|Identifier|Expr $name Property name
  265. *
  266. * @return Expr\PropertyFetch
  267. */
  268. public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
  269. return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
  270. }
  271. /**
  272. * Creates a class constant fetch node.
  273. *
  274. * @param string|Name|Expr $class Class name
  275. * @param string|Identifier $name Constant name
  276. *
  277. * @return Expr\ClassConstFetch
  278. */
  279. public function classConstFetch($class, $name): Expr\ClassConstFetch {
  280. return new Expr\ClassConstFetch(
  281. BuilderHelpers::normalizeNameOrExpr($class),
  282. BuilderHelpers::normalizeIdentifier($name)
  283. );
  284. }
  285. /**
  286. * Creates nested Concat nodes from a list of expressions.
  287. *
  288. * @param Expr|string ...$exprs Expressions or literal strings
  289. *
  290. * @return Concat
  291. */
  292. public function concat(...$exprs) : Concat {
  293. $numExprs = count($exprs);
  294. if ($numExprs < 2) {
  295. throw new \LogicException('Expected at least two expressions');
  296. }
  297. $lastConcat = $this->normalizeStringExpr($exprs[0]);
  298. for ($i = 1; $i < $numExprs; $i++) {
  299. $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i]));
  300. }
  301. return $lastConcat;
  302. }
  303. /**
  304. * @param string|Expr $expr
  305. * @return Expr
  306. */
  307. private function normalizeStringExpr($expr) : Expr {
  308. if ($expr instanceof Expr) {
  309. return $expr;
  310. }
  311. if (\is_string($expr)) {
  312. return new String_($expr);
  313. }
  314. throw new \LogicException('Expected string or Expr');
  315. }
  316. }