Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@13
|
2
|
Chris@13
|
3 namespace PhpParser;
|
Chris@13
|
4
|
Chris@13
|
5 use PhpParser\Node\Name;
|
Chris@13
|
6 use PhpParser\Node\Name\FullyQualified;
|
Chris@13
|
7 use PhpParser\Node\Stmt;
|
Chris@13
|
8
|
Chris@13
|
9 class NameContext
|
Chris@13
|
10 {
|
Chris@13
|
11 /** @var null|Name Current namespace */
|
Chris@13
|
12 protected $namespace;
|
Chris@13
|
13
|
Chris@13
|
14 /** @var Name[][] Map of format [aliasType => [aliasName => originalName]] */
|
Chris@13
|
15 protected $aliases = [];
|
Chris@13
|
16
|
Chris@13
|
17 /** @var Name[][] Same as $aliases but preserving original case */
|
Chris@13
|
18 protected $origAliases = [];
|
Chris@13
|
19
|
Chris@13
|
20 /** @var ErrorHandler Error handler */
|
Chris@13
|
21 protected $errorHandler;
|
Chris@13
|
22
|
Chris@13
|
23 /**
|
Chris@13
|
24 * Create a name context.
|
Chris@13
|
25 *
|
Chris@13
|
26 * @param ErrorHandler $errorHandler Error handling used to report errors
|
Chris@13
|
27 */
|
Chris@13
|
28 public function __construct(ErrorHandler $errorHandler) {
|
Chris@13
|
29 $this->errorHandler = $errorHandler;
|
Chris@13
|
30 }
|
Chris@13
|
31
|
Chris@13
|
32 /**
|
Chris@13
|
33 * Start a new namespace.
|
Chris@13
|
34 *
|
Chris@13
|
35 * This also resets the alias table.
|
Chris@13
|
36 *
|
Chris@13
|
37 * @param Name|null $namespace Null is the global namespace
|
Chris@13
|
38 */
|
Chris@13
|
39 public function startNamespace(Name $namespace = null) {
|
Chris@13
|
40 $this->namespace = $namespace;
|
Chris@13
|
41 $this->origAliases = $this->aliases = [
|
Chris@13
|
42 Stmt\Use_::TYPE_NORMAL => [],
|
Chris@13
|
43 Stmt\Use_::TYPE_FUNCTION => [],
|
Chris@13
|
44 Stmt\Use_::TYPE_CONSTANT => [],
|
Chris@13
|
45 ];
|
Chris@13
|
46 }
|
Chris@13
|
47
|
Chris@13
|
48 /**
|
Chris@13
|
49 * Add an alias / import.
|
Chris@13
|
50 *
|
Chris@13
|
51 * @param Name $name Original name
|
Chris@13
|
52 * @param string $aliasName Aliased name
|
Chris@13
|
53 * @param int $type One of Stmt\Use_::TYPE_*
|
Chris@13
|
54 * @param array $errorAttrs Attributes to use to report an error
|
Chris@13
|
55 */
|
Chris@13
|
56 public function addAlias(Name $name, string $aliasName, int $type, array $errorAttrs = []) {
|
Chris@13
|
57 // Constant names are case sensitive, everything else case insensitive
|
Chris@13
|
58 if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
Chris@13
|
59 $aliasLookupName = $aliasName;
|
Chris@13
|
60 } else {
|
Chris@13
|
61 $aliasLookupName = strtolower($aliasName);
|
Chris@13
|
62 }
|
Chris@13
|
63
|
Chris@13
|
64 if (isset($this->aliases[$type][$aliasLookupName])) {
|
Chris@13
|
65 $typeStringMap = [
|
Chris@13
|
66 Stmt\Use_::TYPE_NORMAL => '',
|
Chris@13
|
67 Stmt\Use_::TYPE_FUNCTION => 'function ',
|
Chris@13
|
68 Stmt\Use_::TYPE_CONSTANT => 'const ',
|
Chris@13
|
69 ];
|
Chris@13
|
70
|
Chris@13
|
71 $this->errorHandler->handleError(new Error(
|
Chris@13
|
72 sprintf(
|
Chris@13
|
73 'Cannot use %s%s as %s because the name is already in use',
|
Chris@13
|
74 $typeStringMap[$type], $name, $aliasName
|
Chris@13
|
75 ),
|
Chris@13
|
76 $errorAttrs
|
Chris@13
|
77 ));
|
Chris@13
|
78 return;
|
Chris@13
|
79 }
|
Chris@13
|
80
|
Chris@13
|
81 $this->aliases[$type][$aliasLookupName] = $name;
|
Chris@13
|
82 $this->origAliases[$type][$aliasName] = $name;
|
Chris@13
|
83 }
|
Chris@13
|
84
|
Chris@13
|
85 /**
|
Chris@13
|
86 * Get current namespace.
|
Chris@13
|
87 *
|
Chris@13
|
88 * @return null|Name Namespace (or null if global namespace)
|
Chris@13
|
89 */
|
Chris@13
|
90 public function getNamespace() {
|
Chris@13
|
91 return $this->namespace;
|
Chris@13
|
92 }
|
Chris@13
|
93
|
Chris@13
|
94 /**
|
Chris@13
|
95 * Get resolved name.
|
Chris@13
|
96 *
|
Chris@13
|
97 * @param Name $name Name to resolve
|
Chris@13
|
98 * @param int $type One of Stmt\Use_::TYPE_{FUNCTION|CONSTANT}
|
Chris@13
|
99 *
|
Chris@13
|
100 * @return null|Name Resolved name, or null if static resolution is not possible
|
Chris@13
|
101 */
|
Chris@13
|
102 public function getResolvedName(Name $name, int $type) {
|
Chris@13
|
103 // don't resolve special class names
|
Chris@13
|
104 if ($type === Stmt\Use_::TYPE_NORMAL && $name->isSpecialClassName()) {
|
Chris@13
|
105 if (!$name->isUnqualified()) {
|
Chris@13
|
106 $this->errorHandler->handleError(new Error(
|
Chris@13
|
107 sprintf("'\\%s' is an invalid class name", $name->toString()),
|
Chris@13
|
108 $name->getAttributes()
|
Chris@13
|
109 ));
|
Chris@13
|
110 }
|
Chris@13
|
111 return $name;
|
Chris@13
|
112 }
|
Chris@13
|
113
|
Chris@13
|
114 // fully qualified names are already resolved
|
Chris@13
|
115 if ($name->isFullyQualified()) {
|
Chris@13
|
116 return $name;
|
Chris@13
|
117 }
|
Chris@13
|
118
|
Chris@13
|
119 // Try to resolve aliases
|
Chris@13
|
120 if (null !== $resolvedName = $this->resolveAlias($name, $type)) {
|
Chris@13
|
121 return $resolvedName;
|
Chris@13
|
122 }
|
Chris@13
|
123
|
Chris@13
|
124 if ($type !== Stmt\Use_::TYPE_NORMAL && $name->isUnqualified()) {
|
Chris@13
|
125 if (null === $this->namespace) {
|
Chris@13
|
126 // outside of a namespace unaliased unqualified is same as fully qualified
|
Chris@13
|
127 return new FullyQualified($name, $name->getAttributes());
|
Chris@13
|
128 }
|
Chris@13
|
129
|
Chris@13
|
130 // Cannot resolve statically
|
Chris@13
|
131 return null;
|
Chris@13
|
132 }
|
Chris@13
|
133
|
Chris@13
|
134 // if no alias exists prepend current namespace
|
Chris@13
|
135 return FullyQualified::concat($this->namespace, $name, $name->getAttributes());
|
Chris@13
|
136 }
|
Chris@13
|
137
|
Chris@13
|
138 /**
|
Chris@13
|
139 * Get resolved class name.
|
Chris@13
|
140 *
|
Chris@13
|
141 * @param Name $name Class ame to resolve
|
Chris@13
|
142 *
|
Chris@13
|
143 * @return Name Resolved name
|
Chris@13
|
144 */
|
Chris@13
|
145 public function getResolvedClassName(Name $name) : Name {
|
Chris@13
|
146 return $this->getResolvedName($name, Stmt\Use_::TYPE_NORMAL);
|
Chris@13
|
147 }
|
Chris@13
|
148
|
Chris@13
|
149 /**
|
Chris@13
|
150 * Get possible ways of writing a fully qualified name (e.g., by making use of aliases).
|
Chris@13
|
151 *
|
Chris@13
|
152 * @param string $name Fully-qualified name (without leading namespace separator)
|
Chris@13
|
153 * @param int $type One of Stmt\Use_::TYPE_*
|
Chris@13
|
154 *
|
Chris@13
|
155 * @return Name[] Possible representations of the name
|
Chris@13
|
156 */
|
Chris@13
|
157 public function getPossibleNames(string $name, int $type) : array {
|
Chris@13
|
158 $lcName = strtolower($name);
|
Chris@13
|
159
|
Chris@13
|
160 if ($type === Stmt\Use_::TYPE_NORMAL) {
|
Chris@13
|
161 // self, parent and static must always be unqualified
|
Chris@13
|
162 if ($lcName === "self" || $lcName === "parent" || $lcName === "static") {
|
Chris@13
|
163 return [new Name($name)];
|
Chris@13
|
164 }
|
Chris@13
|
165 }
|
Chris@13
|
166
|
Chris@13
|
167 // Collect possible ways to write this name, starting with the fully-qualified name
|
Chris@13
|
168 $possibleNames = [new FullyQualified($name)];
|
Chris@13
|
169
|
Chris@13
|
170 if (null !== $nsRelativeName = $this->getNamespaceRelativeName($name, $lcName, $type)) {
|
Chris@13
|
171 // Make sure there is no alias that makes the normally namespace-relative name
|
Chris@13
|
172 // into something else
|
Chris@13
|
173 if (null === $this->resolveAlias($nsRelativeName, $type)) {
|
Chris@13
|
174 $possibleNames[] = $nsRelativeName;
|
Chris@13
|
175 }
|
Chris@13
|
176 }
|
Chris@13
|
177
|
Chris@13
|
178 // Check for relevant namespace use statements
|
Chris@13
|
179 foreach ($this->origAliases[Stmt\Use_::TYPE_NORMAL] as $alias => $orig) {
|
Chris@13
|
180 $lcOrig = $orig->toLowerString();
|
Chris@13
|
181 if (0 === strpos($lcName, $lcOrig . '\\')) {
|
Chris@13
|
182 $possibleNames[] = new Name($alias . substr($name, strlen($lcOrig)));
|
Chris@13
|
183 }
|
Chris@13
|
184 }
|
Chris@13
|
185
|
Chris@13
|
186 // Check for relevant type-specific use statements
|
Chris@13
|
187 foreach ($this->origAliases[$type] as $alias => $orig) {
|
Chris@13
|
188 if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
Chris@13
|
189 // Constants are are complicated-sensitive
|
Chris@13
|
190 $normalizedOrig = $this->normalizeConstName($orig->toString());
|
Chris@13
|
191 if ($normalizedOrig === $this->normalizeConstName($name)) {
|
Chris@13
|
192 $possibleNames[] = new Name($alias);
|
Chris@13
|
193 }
|
Chris@13
|
194 } else {
|
Chris@13
|
195 // Everything else is case-insensitive
|
Chris@13
|
196 if ($orig->toLowerString() === $lcName) {
|
Chris@13
|
197 $possibleNames[] = new Name($alias);
|
Chris@13
|
198 }
|
Chris@13
|
199 }
|
Chris@13
|
200 }
|
Chris@13
|
201
|
Chris@13
|
202 return $possibleNames;
|
Chris@13
|
203 }
|
Chris@13
|
204
|
Chris@13
|
205 /**
|
Chris@13
|
206 * Get shortest representation of this fully-qualified name.
|
Chris@13
|
207 *
|
Chris@13
|
208 * @param string $name Fully-qualified name (without leading namespace separator)
|
Chris@13
|
209 * @param int $type One of Stmt\Use_::TYPE_*
|
Chris@13
|
210 *
|
Chris@13
|
211 * @return Name Shortest representation
|
Chris@13
|
212 */
|
Chris@13
|
213 public function getShortName(string $name, int $type) : Name {
|
Chris@13
|
214 $possibleNames = $this->getPossibleNames($name, $type);
|
Chris@13
|
215
|
Chris@13
|
216 // Find shortest name
|
Chris@13
|
217 $shortestName = null;
|
Chris@13
|
218 $shortestLength = \INF;
|
Chris@13
|
219 foreach ($possibleNames as $possibleName) {
|
Chris@13
|
220 $length = strlen($possibleName->toCodeString());
|
Chris@13
|
221 if ($length < $shortestLength) {
|
Chris@13
|
222 $shortestName = $possibleName;
|
Chris@13
|
223 $shortestLength = $length;
|
Chris@13
|
224 }
|
Chris@13
|
225 }
|
Chris@13
|
226
|
Chris@13
|
227 return $shortestName;
|
Chris@13
|
228 }
|
Chris@13
|
229
|
Chris@13
|
230 private function resolveAlias(Name $name, $type) {
|
Chris@13
|
231 $firstPart = $name->getFirst();
|
Chris@13
|
232
|
Chris@13
|
233 if ($name->isQualified()) {
|
Chris@13
|
234 // resolve aliases for qualified names, always against class alias table
|
Chris@13
|
235 $checkName = strtolower($firstPart);
|
Chris@13
|
236 if (isset($this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName])) {
|
Chris@13
|
237 $alias = $this->aliases[Stmt\Use_::TYPE_NORMAL][$checkName];
|
Chris@13
|
238 return FullyQualified::concat($alias, $name->slice(1), $name->getAttributes());
|
Chris@13
|
239 }
|
Chris@13
|
240 } elseif ($name->isUnqualified()) {
|
Chris@13
|
241 // constant aliases are case-sensitive, function aliases case-insensitive
|
Chris@13
|
242 $checkName = $type === Stmt\Use_::TYPE_CONSTANT ? $firstPart : strtolower($firstPart);
|
Chris@13
|
243 if (isset($this->aliases[$type][$checkName])) {
|
Chris@13
|
244 // resolve unqualified aliases
|
Chris@13
|
245 return new FullyQualified($this->aliases[$type][$checkName], $name->getAttributes());
|
Chris@13
|
246 }
|
Chris@13
|
247 }
|
Chris@13
|
248
|
Chris@13
|
249 // No applicable aliases
|
Chris@13
|
250 return null;
|
Chris@13
|
251 }
|
Chris@13
|
252
|
Chris@13
|
253 private function getNamespaceRelativeName(string $name, string $lcName, int $type) {
|
Chris@13
|
254 if (null === $this->namespace) {
|
Chris@13
|
255 return new Name($name);
|
Chris@13
|
256 }
|
Chris@13
|
257
|
Chris@13
|
258 if ($type === Stmt\Use_::TYPE_CONSTANT) {
|
Chris@13
|
259 // The constants true/false/null always resolve to the global symbols, even inside a
|
Chris@13
|
260 // namespace, so they may be used without qualification
|
Chris@13
|
261 if ($lcName === "true" || $lcName === "false" || $lcName === "null") {
|
Chris@13
|
262 return new Name($name);
|
Chris@13
|
263 }
|
Chris@13
|
264 }
|
Chris@13
|
265
|
Chris@13
|
266 $namespacePrefix = strtolower($this->namespace . '\\');
|
Chris@13
|
267 if (0 === strpos($lcName, $namespacePrefix)) {
|
Chris@13
|
268 return new Name(substr($name, strlen($namespacePrefix)));
|
Chris@13
|
269 }
|
Chris@13
|
270
|
Chris@13
|
271 return null;
|
Chris@13
|
272 }
|
Chris@13
|
273
|
Chris@13
|
274 private function normalizeConstName(string $name) {
|
Chris@13
|
275 $nsSep = strrpos($name, '\\');
|
Chris@13
|
276 if (false === $nsSep) {
|
Chris@13
|
277 return $name;
|
Chris@13
|
278 }
|
Chris@13
|
279
|
Chris@13
|
280 // Constants have case-insensitive namespace and case-sensitive short-name
|
Chris@13
|
281 $ns = substr($name, 0, $nsSep);
|
Chris@13
|
282 $shortName = substr($name, $nsSep + 1);
|
Chris@13
|
283 return strtolower($ns) . '\\' . $shortName;
|
Chris@13
|
284 }
|
Chris@13
|
285 }
|