1 <?php declare(strict_types=1);
3 namespace PhpParser\Node;
5 use PhpParser\NodeAbstract;
7 class Name extends NodeAbstract
10 * @var string[] Parts of the name
14 private static $specialClassNames = [
21 * Constructs a name node.
23 * @param string|string[]|self $name Name as string, part array or Name instance (copy ctor)
24 * @param array $attributes Additional attributes
26 public function __construct($name, array $attributes = []) {
27 parent::__construct($attributes);
28 $this->parts = self::prepareName($name);
31 public function getSubNodeNames() : array {
36 * Gets the first part of the name, i.e. everything before the first namespace separator.
38 * @return string First part of the name
40 public function getFirst() : string {
41 return $this->parts[0];
45 * Gets the last part of the name, i.e. everything after the last namespace separator.
47 * @return string Last part of the name
49 public function getLast() : string {
50 return $this->parts[count($this->parts) - 1];
54 * Checks whether the name is unqualified. (E.g. Name)
56 * @return bool Whether the name is unqualified
58 public function isUnqualified() : bool {
59 return 1 === count($this->parts);
63 * Checks whether the name is qualified. (E.g. Name\Name)
65 * @return bool Whether the name is qualified
67 public function isQualified() : bool {
68 return 1 < count($this->parts);
72 * Checks whether the name is fully qualified. (E.g. \Name)
74 * @return bool Whether the name is fully qualified
76 public function isFullyQualified() : bool {
81 * Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
83 * @return bool Whether the name is relative
85 public function isRelative() : bool {
90 * Returns a string representation of the name itself, without taking taking the name type into
91 * account (e.g., not including a leading backslash for fully qualified names).
93 * @return string String representation
95 public function toString() : string {
96 return implode('\\', $this->parts);
100 * Returns a string representation of the name as it would occur in code (e.g., including
101 * leading backslash for fully qualified names.
103 * @return string String representation
105 public function toCodeString() : string {
106 return $this->toString();
110 * Returns lowercased string representation of the name, without taking the name type into
111 * account (e.g., no leading backslash for fully qualified names).
113 * @return string Lowercased string representation
115 public function toLowerString() : string {
116 return strtolower(implode('\\', $this->parts));
120 * Checks whether the identifier is a special class name (self, parent or static).
122 * @return bool Whether identifier is a special class name
124 public function isSpecialClassName() : bool {
125 return count($this->parts) === 1
126 && isset(self::$specialClassNames[strtolower($this->parts[0])]);
130 * Returns a string representation of the name by imploding the namespace parts with the
131 * namespace separator.
133 * @return string String representation
135 public function __toString() : string {
136 return implode('\\', $this->parts);
140 * Gets a slice of a name (similar to array_slice).
142 * This method returns a new instance of the same type as the original and with the same
145 * If the slice is empty, null is returned. The null value will be correctly handled in
146 * concatenations using concat().
148 * Offset and length have the same meaning as in array_slice().
150 * @param int $offset Offset to start the slice at (may be negative)
151 * @param int|null $length Length of the slice (may be negative)
153 * @return static|null Sliced name
155 public function slice(int $offset, int $length = null) {
156 $numParts = count($this->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));
163 if (null === $length) {
164 $realLength = $numParts - $realOffset;
166 $realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
167 if ($realLength < 0 || $realLength > $numParts) {
168 throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
172 if ($realLength === 0) {
173 // Empty slice is represented as null
177 return new static(array_slice($this->parts, $realOffset, $realLength), $this->attributes);
181 * Concatenate two names, yielding a new Name instance.
183 * The type of the generated instance depends on which class this method is called on, for
184 * example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
186 * If one of the arguments is null, a new instance of the other name will be returned. If both
187 * arguments are null, null will be returned. As such, writing
188 * Name::concat($namespace, $shortName)
189 * where $namespace is a Name node or null will work as expected.
191 * @param string|string[]|self|null $name1 The first name
192 * @param string|string[]|self|null $name2 The second name
193 * @param array $attributes Attributes to assign to concatenated name
195 * @return static|null Concatenated name
197 public static function concat($name1, $name2, array $attributes = []) {
198 if (null === $name1 && null === $name2) {
200 } elseif (null === $name1) {
201 return new static(self::prepareName($name2), $attributes);
202 } elseif (null === $name2) {
203 return new static(self::prepareName($name1), $attributes);
206 array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
212 * Prepares a (string, array or Name node) name for use in name changing methods by converting
215 * @param string|string[]|self $name Name to prepare
217 * @return string[] Prepared name
219 private static function prepareName($name) : array {
220 if (\is_string($name)) {
222 throw new \InvalidArgumentException('Name cannot be empty');
225 return explode('\\', $name);
226 } elseif (\is_array($name)) {
228 throw new \InvalidArgumentException('Name cannot be empty');
232 } elseif ($name instanceof self) {
236 throw new \InvalidArgumentException(
237 'Expected string, array of parts or Name instance'
241 public function getType() : string {