Namespace_.php 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 Namespace_ extends Declaration
  8. {
  9. private $name;
  10. private $stmts = [];
  11. /**
  12. * Creates a namespace builder.
  13. *
  14. * @param Node\Name|string|null $name Name of the namespace
  15. */
  16. public function __construct($name) {
  17. $this->name = null !== $name ? BuilderHelpers::normalizeName($name) : null;
  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 node.
  32. *
  33. * @return Node The built node
  34. */
  35. public function getNode() : Node {
  36. return new Stmt\Namespace_($this->name, $this->stmts, $this->attributes);
  37. }
  38. }