Use_.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser\BuilderAbstract;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Stmt;
  6. /**
  7. * @method $this as(string $alias) Sets alias for used name.
  8. */
  9. class Use_ extends BuilderAbstract {
  10. protected $name;
  11. protected $type;
  12. protected $alias = null;
  13. /**
  14. * Creates a name use (alias) builder.
  15. *
  16. * @param Node\Name|string $name Name of the entity (namespace, class, function, constant) to alias
  17. * @param int $type One of the Stmt\Use_::TYPE_* constants
  18. */
  19. public function __construct($name, $type) {
  20. $this->name = $this->normalizeName($name);
  21. $this->type = $type;
  22. }
  23. /**
  24. * Sets alias for used name.
  25. *
  26. * @param string $alias Alias to use (last component of full name by default)
  27. *
  28. * @return $this The builder instance (for fluid interface)
  29. */
  30. protected function as_($alias) {
  31. $this->alias = $alias;
  32. return $this;
  33. }
  34. public function __call($name, $args) {
  35. if (method_exists($this, $name . '_')) {
  36. return call_user_func_array(array($this, $name . '_'), $args);
  37. }
  38. throw new \LogicException(sprintf('Method "%s" does not exist', $name));
  39. }
  40. /**
  41. * Returns the built node.
  42. *
  43. * @return Node The built node
  44. */
  45. public function getNode() {
  46. $alias = null !== $this->alias ? $this->alias : $this->name->getLast();
  47. return new Stmt\Use_(array(
  48. new Stmt\UseUse($this->name, $alias)
  49. ), $this->type);
  50. }
  51. }