No Description

Name.php 7.5KB

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