You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
10 KiB

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