Identifier.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\NodeAbstract;
  4. /**
  5. * Represents a non-namespaced name. Namespaced names are represented using Name nodes.
  6. */
  7. class Identifier extends NodeAbstract {
  8. /**
  9. * @psalm-var non-empty-string
  10. * @var string Identifier as string
  11. */
  12. public string $name;
  13. /** @var array<string, bool> */
  14. private static array $specialClassNames = [
  15. 'self' => true,
  16. 'parent' => true,
  17. 'static' => true,
  18. ];
  19. /**
  20. * Constructs an identifier node.
  21. *
  22. * @param string $name Identifier as string
  23. * @param array<string, mixed> $attributes Additional attributes
  24. */
  25. public function __construct(string $name, array $attributes = []) {
  26. if ($name === '') {
  27. throw new \InvalidArgumentException('Identifier name cannot be empty');
  28. }
  29. $this->attributes = $attributes;
  30. $this->name = $name;
  31. }
  32. public function getSubNodeNames(): array {
  33. return ['name'];
  34. }
  35. /**
  36. * Get identifier as string.
  37. *
  38. * @psalm-return non-empty-string
  39. * @return string Identifier as string.
  40. */
  41. public function toString(): string {
  42. return $this->name;
  43. }
  44. /**
  45. * Get lowercased identifier as string.
  46. *
  47. * @psalm-return non-empty-string&lowercase-string
  48. * @return string Lowercased identifier as string
  49. */
  50. public function toLowerString(): string {
  51. return strtolower($this->name);
  52. }
  53. /**
  54. * Checks whether the identifier is a special class name (self, parent or static).
  55. *
  56. * @return bool Whether identifier is a special class name
  57. */
  58. public function isSpecialClassName(): bool {
  59. return isset(self::$specialClassNames[strtolower($this->name)]);
  60. }
  61. /**
  62. * Get identifier as string.
  63. *
  64. * @psalm-return non-empty-string
  65. * @return string Identifier as string
  66. */
  67. public function __toString(): string {
  68. return $this->name;
  69. }
  70. public function getType(): string {
  71. return 'Identifier';
  72. }
  73. }