Use_.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Builder;
  4. use PhpParser\BuilderHelpers;
  5. use PhpParser\Node;
  6. use PhpParser\Node\Stmt;
  7. class Use_ implements Builder
  8. {
  9. protected $name;
  10. protected $type;
  11. protected $alias = null;
  12. /**
  13. * Creates a name use (alias) builder.
  14. *
  15. * @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias
  16. * @param int $type One of the Stmt\Use_::TYPE_* constants
  17. */
  18. public function __construct($name, int $type) {
  19. $this->name = BuilderHelpers::normalizeName($name);
  20. $this->type = $type;
  21. }
  22. /**
  23. * Sets alias for used name.
  24. *
  25. * @param string $alias Alias to use (last component of full name by default)
  26. *
  27. * @return $this The builder instance (for fluid interface)
  28. */
  29. public function as(string $alias) {
  30. $this->alias = $alias;
  31. return $this;
  32. }
  33. /**
  34. * Returns the built node.
  35. *
  36. * @return Stmt\Use_ The built node
  37. */
  38. public function getNode() : Node {
  39. return new Stmt\Use_([
  40. new Stmt\UseUse($this->name, $this->alias)
  41. ], $this->type);
  42. }
  43. }