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