Name.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PhpParser\NodeAbstract;
  4. class Name extends NodeAbstract {
  5. /** @var string Name as string */
  6. public string $name;
  7. /** @var array<string, bool> */
  8. private static array $specialClassNames = [
  9. 'self' => true,
  10. 'parent' => true,
  11. 'static' => true,
  12. ];
  13. /**
  14. * Constructs a name node.
  15. *
  16. * @param string|string[]|self $name Name as string, part array or Name instance (copy ctor)
  17. * @param array<string, mixed> $attributes Additional attributes
  18. */
  19. final public function __construct($name, array $attributes = []) {
  20. $this->attributes = $attributes;
  21. $this->name = self::prepareName($name);
  22. }
  23. public function getSubNodeNames(): array {
  24. return ['name'];
  25. }
  26. /**
  27. * Get parts of name (split by the namespace separator).
  28. *
  29. * @return string[] Parts of name
  30. */
  31. public function getParts(): array {
  32. return \explode('\\', $this->name);
  33. }
  34. /**
  35. * Gets the first part of the name, i.e. everything before the first namespace separator.
  36. *
  37. * @return string First part of the name
  38. */
  39. public function getFirst(): string {
  40. if (false !== $pos = \strpos($this->name, '\\')) {
  41. return \substr($this->name, 0, $pos);
  42. }
  43. return $this->name;
  44. }
  45. /**
  46. * Gets the last part of the name, i.e. everything after the last namespace separator.
  47. *
  48. * @return string Last part of the name
  49. */
  50. public function getLast(): string {
  51. if (false !== $pos = \strrpos($this->name, '\\')) {
  52. return \substr($this->name, $pos + 1);
  53. }
  54. return $this->name;
  55. }
  56. /**
  57. * Checks whether the name is unqualified. (E.g. Name)
  58. *
  59. * @return bool Whether the name is unqualified
  60. */
  61. public function isUnqualified(): bool {
  62. return false === \strpos($this->name, '\\');
  63. }
  64. /**
  65. * Checks whether the name is qualified. (E.g. Name\Name)
  66. *
  67. * @return bool Whether the name is qualified
  68. */
  69. public function isQualified(): bool {
  70. return false !== \strpos($this->name, '\\');
  71. }
  72. /**
  73. * Checks whether the name is fully qualified. (E.g. \Name)
  74. *
  75. * @return bool Whether the name is fully qualified
  76. */
  77. public function isFullyQualified(): bool {
  78. return false;
  79. }
  80. /**
  81. * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
  82. *
  83. * @return bool Whether the name is relative
  84. */
  85. public function isRelative(): bool {
  86. return false;
  87. }
  88. /**
  89. * Returns a string representation of the name itself, without taking the name type into
  90. * account (e.g., not including a leading backslash for fully qualified names).
  91. *
  92. * @return string String representation
  93. */
  94. public function toString(): string {
  95. return $this->name;
  96. }
  97. /**
  98. * Returns a string representation of the name as it would occur in code (e.g., including
  99. * leading backslash for fully qualified names.
  100. *
  101. * @return string String representation
  102. */
  103. public function toCodeString(): string {
  104. return $this->toString();
  105. }
  106. /**
  107. * Returns lowercased string representation of the name, without taking the name type into
  108. * account (e.g., no leading backslash for fully qualified names).
  109. *
  110. * @return string Lowercased string representation
  111. */
  112. public function toLowerString(): string {
  113. return strtolower($this->name);
  114. }
  115. /**
  116. * Checks whether the identifier is a special class name (self, parent or static).
  117. *
  118. * @return bool Whether identifier is a special class name
  119. */
  120. public function isSpecialClassName(): bool {
  121. return isset(self::$specialClassNames[strtolower($this->name)]);
  122. }
  123. /**
  124. * Returns a string representation of the name by imploding the namespace parts with the
  125. * namespace separator.
  126. *
  127. * @return string String representation
  128. */
  129. public function __toString(): string {
  130. return $this->name;
  131. }
  132. /**
  133. * Gets a slice of a name (similar to array_slice).
  134. *
  135. * This method returns a new instance of the same type as the original and with the same
  136. * attributes.
  137. *
  138. * If the slice is empty, null is returned. The null value will be correctly handled in
  139. * concatenations using concat().
  140. *
  141. * Offset and length have the same meaning as in array_slice().
  142. *
  143. * @param int $offset Offset to start the slice at (may be negative)
  144. * @param int|null $length Length of the slice (may be negative)
  145. *
  146. * @return static|null Sliced name
  147. */
  148. public function slice(int $offset, ?int $length = null) {
  149. if ($offset === 1 && $length === null) {
  150. // Short-circuit the common case.
  151. if (false !== $pos = \strpos($this->name, '\\')) {
  152. return new static(\substr($this->name, $pos + 1));
  153. }
  154. return null;
  155. }
  156. $parts = \explode('\\', $this->name);
  157. $numParts = \count($parts);
  158. $realOffset = $offset < 0 ? $offset + $numParts : $offset;
  159. if ($realOffset < 0 || $realOffset > $numParts) {
  160. throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
  161. }
  162. if (null === $length) {
  163. $realLength = $numParts - $realOffset;
  164. } else {
  165. $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
  166. if ($realLength < 0 || $realLength > $numParts - $realOffset) {
  167. throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
  168. }
  169. }
  170. if ($realLength === 0) {
  171. // Empty slice is represented as null
  172. return null;
  173. }
  174. return new static(array_slice($parts, $realOffset, $realLength), $this->attributes);
  175. }
  176. /**
  177. * Concatenate two names, yielding a new Name instance.
  178. *
  179. * The type of the generated instance depends on which class this method is called on, for
  180. * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
  181. *
  182. * If one of the arguments is null, a new instance of the other name will be returned. If both
  183. * arguments are null, null will be returned. As such, writing
  184. * Name::concat($namespace, $shortName)
  185. * where $namespace is a Name node or null will work as expected.
  186. *
  187. * @param string|string[]|self|null $name1 The first name
  188. * @param string|string[]|self|null $name2 The second name
  189. * @param array<string, mixed> $attributes Attributes to assign to concatenated name
  190. *
  191. * @return static|null Concatenated name
  192. */
  193. public static function concat($name1, $name2, array $attributes = []) {
  194. if (null === $name1 && null === $name2) {
  195. return null;
  196. }
  197. if (null === $name1) {
  198. return new static($name2, $attributes);
  199. }
  200. if (null === $name2) {
  201. return new static($name1, $attributes);
  202. } else {
  203. return new static(
  204. self::prepareName($name1) . '\\' . self::prepareName($name2), $attributes
  205. );
  206. }
  207. }
  208. /**
  209. * Prepares a (string, array or Name node) name for use in name changing methods by converting
  210. * it to a string.
  211. *
  212. * @param string|string[]|self $name Name to prepare
  213. *
  214. * @return string Prepared name
  215. */
  216. private static function prepareName($name): string {
  217. if (\is_string($name)) {
  218. if ('' === $name) {
  219. throw new \InvalidArgumentException('Name cannot be empty');
  220. }
  221. return $name;
  222. }
  223. if (\is_array($name)) {
  224. if (empty($name)) {
  225. throw new \InvalidArgumentException('Name cannot be empty');
  226. }
  227. return implode('\\', $name);
  228. }
  229. if ($name instanceof self) {
  230. return $name->name;
  231. }
  232. throw new \InvalidArgumentException(
  233. 'Expected string, array of parts or Name instance'
  234. );
  235. }
  236. public function getType(): string {
  237. return 'Name';
  238. }
  239. }