Name.php 7.6 KB

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