UseItem.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\Node;
  4. use PhpParser\NodeAbstract;
  5. use PhpParser\Node\Stmt\Use_;
  6. class UseItem extends NodeAbstract {
  7. /**
  8. * @var Use_::TYPE_* One of the Stmt\Use_::TYPE_* constants. Will only differ from TYPE_UNKNOWN for mixed group uses
  9. */
  10. public int $type;
  11. /** @var Node\Name Namespace, class, function or constant to alias */
  12. public Name $name;
  13. /** @var Identifier|null Alias */
  14. public ?Identifier $alias;
  15. /**
  16. * Constructs an alias (use) item node.
  17. *
  18. * @param Node\Name $name Namespace/Class to alias
  19. * @param null|string|Identifier $alias Alias
  20. * @param Use_::TYPE_* $type Type of the use element (for mixed group use only)
  21. * @param array<string, mixed> $attributes Additional attributes
  22. */
  23. public function __construct(Node\Name $name, $alias = null, int $type = Use_::TYPE_UNKNOWN, array $attributes = []) {
  24. $this->attributes = $attributes;
  25. $this->type = $type;
  26. $this->name = $name;
  27. $this->alias = \is_string($alias) ? new Identifier($alias) : $alias;
  28. }
  29. public function getSubNodeNames(): array {
  30. return ['type', 'name', 'alias'];
  31. }
  32. /**
  33. * Get alias. If not explicitly given this is the last component of the used name.
  34. */
  35. public function getAlias(): Identifier {
  36. if (null !== $this->alias) {
  37. return $this->alias;
  38. }
  39. return new Identifier($this->name->getLast());
  40. }
  41. public function getType(): string {
  42. return 'UseItem';
  43. }
  44. }
  45. // @deprecated compatibility alias
  46. class_alias(UseItem::class, Stmt\UseUse::class);