1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | use PhpParser\Node\Name; |
6: | use PhpParser\Node\Name\FullyQualified; |
7: | use PhpParser\Node\Stmt; |
8: | |
9: | class NameContext |
10: | { |
11: | |
12: | protected $namespace; |
13: | |
14: | |
15: | protected $aliases = []; |
16: | |
17: | |
18: | protected $origAliases = []; |
19: | |
20: | |
21: | protected $errorHandler; |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | public function __construct(ErrorHandler $errorHandler) { |
29: | $this->errorHandler = $errorHandler; |
30: | } |
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | public function startNamespace(?Name $namespace = null) { |
40: | $this->namespace = $namespace; |
41: | $this->origAliases = $this->aliases = [ |
42: | Stmt\Use_::TYPE_NORMAL => [], |
43: | Stmt\Use_::TYPE_FUNCTION => [], |
44: | Stmt\Use_::TYPE_CONSTANT => [], |
45: | ]; |
46: | } |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []) { |
57: | |
58: | if ($type === Stmt\Use_::TYPE_CONSTANT) { |
59: | $aliasLookupName = $aliasName; |
60: | } else { |
61: | $aliasLookupName = strtolower($aliasName); |
62: | } |
63: | |
64: | if (isset($this->aliases[$type][$aliasLookupName])) { |
65: | $typeStringMap = [ |
66: | Stmt\Use_::TYPE_NORMAL => '', |
67: | Stmt\Use_::TYPE_FUNCTION => 'function ', |
68: | Stmt\Use_::TYPE_CONSTANT => 'const ', |
69: | ]; |
70: | |
71: | $this->errorHandler->handleError(new Error( |
72: | sprintf( |
73: | 'Cannot use %s%s as %s because the name is already in use', |
74: | $typeStringMap[$type], $name, $aliasName |
75: | ), |
76: | $errorAttrs |
77: | )); |
78: | return; |
79: | } |
80: | |
81: | $this->aliases[$type][$aliasLookupName] = $name; |
82: | $this->origAliases[$type][$aliasName] = $name; |
83: | } |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | public function getNamespace() { |
91: | return $this->namespace; |
92: | } |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public function getResolvedName(Name $name, int $type) { |
103: | |
104: | if ($type === Stmt\Use_::TYPE_NORMAL && $name->isSpecialClassName()) { |
105: | if (!$name->isUnqualified()) { |
106: | $this->errorHandler->handleError(new Error( |
107: | sprintf("'\\%s' is an invalid class name", $name->toString()), |
108: | $name->getAttributes() |
109: | )); |
110: | } |
111: | return $name; |
112: | } |
113: | |
114: | |
115: | if ($name->isFullyQualified()) { |
116: | return $name; |
117: | } |
118: | |
119: | |
120: | if (null !== $resolvedName = $this->resolveAlias($name, $type)) { |
121: | return $resolvedName; |
122: | } |
123: | |
124: | if ($type !== Stmt\Use_::TYPE_NORMAL && $name->isUnqualified()) { |
125: | if (null === $this->namespace) { |
126: | |
127: | return new FullyQualified($name, $name->getAttributes()); |
128: | } |
129: | |
130: | |
131: | return null; |
132: | } |
133: | |
134: | |
135: | return FullyQualified::concat($this->namespace, $name, $name->getAttributes()); |
136: | } |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | public function getResolvedClassName(Name $name) : Name { |
146: | return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL); |
147: | } |
148: | |
149: | |
150: | |
151: | |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | public function getPossibleNames(string $name, int $type) : array { |
158: | $lcName = strtolower($name); |
159: | |
160: | if ($type === Stmt\Use_::TYPE_NORMAL) { |
161: | |
162: | if ($lcName === "self" || $lcName === "parent" || $lcName === "static") { |
163: | return [new Name($name)]; |
164: | } |
165: | } |
166: | |
167: | |
168: | $possibleNames = [new FullyQualified($name)]; |
169: | |
170: | if (null !== $nsRelativeName = $this->getNamespaceRelativeName($name, $lcName, $type)) { |
171: | |
172: | |
173: | if (null === $this->resolveAlias($nsRelativeName, $type)) { |
174: | $possibleNames[] = $nsRelativeName; |
175: | } |
176: | } |
177: | |
178: | |
179: | foreach ($this->origAliases[Stmt\Use_::TYPE_NORMAL] as $alias => $orig) { |
180: | $lcOrig = $orig->toLowerString(); |
181: | if (0 === strpos($lcName, $lcOrig . '\\')) { |
182: | $possibleNames[] = new Name($alias . substr($name, strlen($lcOrig))); |
183: | } |
184: | } |
185: | |
186: | |
187: | foreach ($this->origAliases[$type] as $alias => $orig) { |
188: | if ($type === Stmt\Use_::TYPE_CONSTANT) { |
189: | |
190: | $normalizedOrig = $this->normalizeConstName($orig->toString()); |
191: | if ($normalizedOrig === $this->normalizeConstName($name)) { |
192: | $possibleNames[] = new Name($alias); |
193: | } |
194: | } else { |
195: | |
196: | if ($orig->toLowerString() === $lcName) { |
197: | $possibleNames[] = new Name($alias); |
198: | } |
199: | } |
200: | } |
201: | |
202: | return $possibleNames; |
203: | } |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | |
212: | |
213: | public function getShortName(string $name, int $type) : Name { |
214: | $possibleNames = $this->getPossibleNames($name, $type); |
215: | |
216: | |
217: | $shortestName = null; |
218: | $shortestLength = \INF; |
219: | foreach ($possibleNames as $possibleName) { |
220: | $length = strlen($possibleName->toCodeString()); |
221: | if ($length < $shortestLength) { |
222: | $shortestName = $possibleName; |
223: | $shortestLength = $length; |
224: | } |
225: | } |
226: | |
227: | return $shortestName; |
228: | } |
229: | |
230: | private function resolveAlias(Name $name, $type) { |
231: | $firstPart = $name->getFirst(); |
232: | |
233: | if ($name->isQualified()) { |
234: | |
235: | $checkName = strtolower($firstPart); |
236: | if (isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName])) { |
237: | $alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName]; |
238: | return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes()); |
239: | } |
240: | } elseif ($name->isUnqualified()) { |
241: | |
242: | $checkName = $type === Stmt\Use_::TYPE_CONSTANT ? $firstPart : strtolower($firstPart); |
243: | if (isset($this->aliases[$type][$checkName])) { |
244: | |
245: | return new FullyQualified($this->aliases[$type][$checkName], $name->getAttributes()); |
246: | } |
247: | } |
248: | |
249: | |
250: | return null; |
251: | } |
252: | |
253: | private function getNamespaceRelativeName(string $name, string $lcName, int $type) { |
254: | if (null === $this->namespace) { |
255: | return new Name($name); |
256: | } |
257: | |
258: | if ($type === Stmt\Use_::TYPE_CONSTANT) { |
259: | |
260: | |
261: | if ($lcName === "true" || $lcName === "false" || $lcName === "null") { |
262: | return new Name($name); |
263: | } |
264: | } |
265: | |
266: | $namespacePrefix = strtolower($this->namespace . '\\'); |
267: | if (0 === strpos($lcName, $namespacePrefix)) { |
268: | return new Name(substr($name, strlen($namespacePrefix))); |
269: | } |
270: | |
271: | return null; |
272: | } |
273: | |
274: | private function normalizeConstName(string $name) { |
275: | $nsSep = strrpos($name, '\\'); |
276: | if (false === $nsSep) { |
277: | return $name; |
278: | } |
279: | |
280: | |
281: | $ns = substr($name, 0, $nsSep); |
282: | $shortName = substr($name, $nsSep + 1); |
283: | return strtolower($ns) . '\\' . $shortName; |
284: | } |
285: | } |
286: | |