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: | |
11: | |
12: | public string $name; |
13: | |
14: | |
15: | private static array $specialClassNames = [ |
16: | 'self' => true, |
17: | 'parent' => true, |
18: | 'static' => true, |
19: | ]; |
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | final public function __construct($name, array $attributes = []) { |
28: | $this->attributes = $attributes; |
29: | $this->name = self::prepareName($name); |
30: | } |
31: | |
32: | public function getSubNodeNames(): array { |
33: | return ['name']; |
34: | } |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | public function getParts(): array { |
43: | return \explode('\\', $this->name); |
44: | } |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | public function getFirst(): string { |
52: | if (false !== $pos = \strpos($this->name, '\\')) { |
53: | return \substr($this->name, 0, $pos); |
54: | } |
55: | return $this->name; |
56: | } |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | public function getLast(): string { |
64: | if (false !== $pos = \strrpos($this->name, '\\')) { |
65: | return \substr($this->name, $pos + 1); |
66: | } |
67: | return $this->name; |
68: | } |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | public function isUnqualified(): bool { |
76: | return false === \strpos($this->name, '\\'); |
77: | } |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | public function isQualified(): bool { |
85: | return false !== \strpos($this->name, '\\'); |
86: | } |
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: | public function isFullyQualified(): bool { |
94: | return false; |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public function isRelative(): bool { |
103: | return false; |
104: | } |
105: | |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | |
113: | public function toString(): string { |
114: | return $this->name; |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function toCodeString(): string { |
125: | return $this->toString(); |
126: | } |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | public function toLowerString(): string { |
136: | return strtolower($this->name); |
137: | } |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | public function isSpecialClassName(): bool { |
145: | return isset(self::$specialClassNames[strtolower($this->name)]); |
146: | } |
147: | |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | public function __toString(): string { |
156: | return $this->name; |
157: | } |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | public function slice(int $offset, ?int $length = null) { |
176: | if ($offset === 1 && $length === null) { |
177: | |
178: | if (false !== $pos = \strpos($this->name, '\\')) { |
179: | return new static(\substr($this->name, $pos + 1)); |
180: | } |
181: | return null; |
182: | } |
183: | |
184: | $parts = \explode('\\', $this->name); |
185: | $numParts = \count($parts); |
186: | |
187: | $realOffset = $offset < 0 ? $offset + $numParts : $offset; |
188: | if ($realOffset < 0 || $realOffset > $numParts) { |
189: | throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset)); |
190: | } |
191: | |
192: | if (null === $length) { |
193: | $realLength = $numParts - $realOffset; |
194: | } else { |
195: | $realLength = $length < 0 ? $length + $numParts - $realOffset : $length; |
196: | if ($realLength < 0 || $realLength > $numParts - $realOffset) { |
197: | throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length)); |
198: | } |
199: | } |
200: | |
201: | if ($realLength === 0) { |
202: | |
203: | return null; |
204: | } |
205: | |
206: | return new static(array_slice($parts, $realOffset, $realLength), $this->attributes); |
207: | } |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | |
222: | |
223: | |
224: | |
225: | |
226: | public static function concat($name1, $name2, array $attributes = []) { |
227: | if (null === $name1 && null === $name2) { |
228: | return null; |
229: | } |
230: | if (null === $name1) { |
231: | return new static($name2, $attributes); |
232: | } |
233: | if (null === $name2) { |
234: | return new static($name1, $attributes); |
235: | } else { |
236: | return new static( |
237: | self::prepareName($name1) . '\\' . self::prepareName($name2), $attributes |
238: | ); |
239: | } |
240: | } |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | |
249: | |
250: | |
251: | private static function prepareName($name): string { |
252: | if (\is_string($name)) { |
253: | if ('' === $name) { |
254: | throw new \InvalidArgumentException('Name cannot be empty'); |
255: | } |
256: | |
257: | return $name; |
258: | } |
259: | if (\is_array($name)) { |
260: | if (empty($name)) { |
261: | throw new \InvalidArgumentException('Name cannot be empty'); |
262: | } |
263: | |
264: | return implode('\\', $name); |
265: | } |
266: | if ($name instanceof self) { |
267: | return $name->name; |
268: | } |
269: | |
270: | throw new \InvalidArgumentException( |
271: | 'Expected string, array of parts or Name instance' |
272: | ); |
273: | } |
274: | |
275: | public function getType(): string { |
276: | return 'Name'; |
277: | } |
278: | } |
279: | |