BuilderFactory.php 9.4 KB

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