Function_.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. {
  9. protected $name;
  10. protected $stmts = [];
  11. /**
  12. * Creates a function builder.
  13. *
  14. * @param string $name Name of the function
  15. */
  16. public function __construct(string $name) {
  17. $this->name = $name;
  18. }
  19. /**
  20. * Adds a statement.
  21. *
  22. * @param Node|PhpParser\Builder $stmt The statement to add
  23. *
  24. * @return $this The builder instance (for fluid interface)
  25. */
  26. public function addStmt($stmt) {
  27. $this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
  28. return $this;
  29. }
  30. /**
  31. * Returns the built function node.
  32. *
  33. * @return Stmt\Function_ The built function node
  34. */
  35. public function getNode() : Node {
  36. return new Stmt\Function_($this->name, [
  37. 'byRef' => $this->returnByRef,
  38. 'params' => $this->params,
  39. 'returnType' => $this->returnType,
  40. 'stmts' => $this->stmts,
  41. ], $this->attributes);
  42. }
  43. }