Name.php 6.0 KB

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