Function_.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser;
  4. use PhpParser\BuilderHelpers;
  5. use PhpParser\Node;
  6. use PhpParser\Node\Stmt;
  7. class Function_ extends FunctionLike {
  8. protected string $name;
  9. /** @var list<Stmt> */
  10. protected array $stmts = [];
  11. /** @var list<Node\AttributeGroup> */
  12. protected array $attributeGroups = [];
  13. /**
  14. * Creates a function builder.
  15. *
  16. * @param string $name Name of the function
  17. */
  18. public function __construct(string $name) {
  19. $this->name = $name;
  20. }
  21. /**
  22. * Adds a statement.
  23. *
  24. * @param Node|PhpParser\Builder $stmt The statement to add
  25. *
  26. * @return $this The builder instance (for fluid interface)
  27. */
  28. public function addStmt($stmt) {
  29. $this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
  30. return $this;
  31. }
  32. /**
  33. * Adds an attribute group.
  34. *
  35. * @param Node\Attribute|Node\AttributeGroup $attribute
  36. *
  37. * @return $this The builder instance (for fluid interface)
  38. */
  39. public function addAttribute($attribute) {
  40. $this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute);
  41. return $this;
  42. }
  43. /**
  44. * Returns the built function node.
  45. *
  46. * @return Stmt\Function_ The built function node
  47. */
  48. public function getNode(): Node {
  49. return new Stmt\Function_($this->name, [
  50. 'byRef' => $this->returnByRef,
  51. 'params' => $this->params,
  52. 'returnType' => $this->returnType,
  53. 'stmts' => $this->stmts,
  54. 'attrGroups' => $this->attributeGroups,
  55. ], $this->attributes);
  56. }
  57. }