3 namespace PhpParser\Node;
5 use PhpParser\NodeAbstract;
7 class Name extends NodeAbstract
10 * @var string[] Parts of the name
15 * Constructs a name node.
17 * @param string|array|self $name Name as string, part array or Name instance (copy ctor)
18 * @param array $attributes Additional attributes
20 public function __construct($name, array $attributes = array()) {
21 parent::__construct($attributes);
22 $this->parts = self::prepareName($name);
25 public function getSubNodeNames() {
26 return array('parts');
30 * Gets the first part of the name, i.e. everything before the first namespace separator.
32 * @return string First part of the name
34 public function getFirst() {
35 return $this->parts[0];
39 * Gets the last part of the name, i.e. everything after the last namespace separator.
41 * @return string Last part of the name
43 public function getLast() {
44 return $this->parts[count($this->parts) - 1];
48 * Checks whether the name is unqualified. (E.g. Name)
50 * @return bool Whether the name is unqualified
52 public function isUnqualified() {
53 return 1 == count($this->parts);
57 * Checks whether the name is qualified. (E.g. Name\Name)
59 * @return bool Whether the name is qualified
61 public function isQualified() {
62 return 1 < count($this->parts);
66 * Checks whether the name is fully qualified. (E.g. \Name)
68 * @return bool Whether the name is fully qualified
70 public function isFullyQualified() {
75 * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
77 * @return bool Whether the name is relative
79 public function isRelative() {
84 * Returns a string representation of the name by imploding the namespace parts with the
85 * namespace separator.
87 * @return string String representation
89 public function toString() {
90 return implode('\\', $this->parts);
94 * Returns a string representation of the name by imploding the namespace parts with the
95 * namespace separator.
97 * @return string String representation
99 public function __toString() {
100 return implode('\\', $this->parts);
104 * Gets a slice of a name (similar to array_slice).
106 * This method returns a new instance of the same type as the original and with the same
109 * If the slice is empty, null is returned. The null value will be correctly handled in
110 * concatenations using concat().
112 * Offset and length have the same meaning as in array_slice().
114 * @param int $offset Offset to start the slice at (may be negative)
115 * @param int|null $length Length of the slice (may be negative)
117 * @return static|null Sliced name
119 public function slice($offset, $length = null) {
120 $numParts = count($this->parts);
122 $realOffset = $offset < 0 ? $offset + $numParts : $offset;
123 if ($realOffset < 0 || $realOffset > $numParts) {
124 throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
127 if (null === $length) {
128 $realLength = $numParts - $realOffset;
130 $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
131 if ($realLength < 0 || $realLength > $numParts) {
132 throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
136 if ($realLength === 0) {
137 // Empty slice is represented as null
141 return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
145 * Concatenate two names, yielding a new Name instance.
147 * The type of the generated instance depends on which class this method is called on, for
148 * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
150 * If one of the arguments is null, a new instance of the other name will be returned. If both
151 * arguments are null, null will be returned. As such, writing
152 * Name::concat($namespace, $shortName)
153 * where $namespace is a Name node or null will work as expected.
155 * @param string|array|self|null $name1 The first name
156 * @param string|array|self|null $name2 The second name
157 * @param array $attributes Attributes to assign to concatenated name
159 * @return static|null Concatenated name
161 public static function concat($name1, $name2, array $attributes = []) {
162 if (null === $name1 && null === $name2) {
164 } elseif (null === $name1) {
165 return new static(self::prepareName($name2), $attributes);
166 } else if (null === $name2) {
167 return new static(self::prepareName($name1), $attributes);
170 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
176 * Prepares a (string, array or Name node) name for use in name changing methods by converting
179 * @param string|array|self $name Name to prepare
181 * @return array Prepared name
183 private static function prepareName($name) {
184 if (\is_string($name)) {
185 return explode('\\', $name);
186 } elseif (\is_array($name)) {
188 } elseif ($name instanceof self) {
192 throw new \InvalidArgumentException(
193 'Expected string, array of parts or Name instance'