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