1: <?php declare(strict_types=1);
2:
3: namespace PhpParser\NodeVisitor;
4:
5: use PhpParser\ErrorHandler;
6: use PhpParser\NameContext;
7: use PhpParser\Node;
8: use PhpParser\Node\Expr;
9: use PhpParser\Node\Name;
10: use PhpParser\Node\Name\FullyQualified;
11: use PhpParser\Node\Stmt;
12: use PhpParser\NodeVisitorAbstract;
13:
14: class NameResolver extends NodeVisitorAbstract {
15: /** @var NameContext Naming context */
16: protected NameContext $nameContext;
17:
18: /** @var bool Whether to preserve original names */
19: protected bool $preserveOriginalNames;
20:
21: /** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
22: protected bool $replaceNodes;
23:
24: /**
25: * Constructs a name resolution visitor.
26: *
27: * Options:
28: * * preserveOriginalNames (default false): An "originalName" attribute will be added to
29: * all name nodes that underwent resolution.
30: * * replaceNodes (default true): Resolved names are replaced in-place. Otherwise, a
31: * resolvedName attribute is added. (Names that cannot be statically resolved receive a
32: * namespacedName attribute, as usual.)
33: *
34: * @param ErrorHandler|null $errorHandler Error handler
35: * @param array{preserveOriginalNames?: bool, replaceNodes?: bool} $options Options
36: */
37: public function __construct(?ErrorHandler $errorHandler = null, array $options = []) {
38: $this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
39: $this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
40: $this->replaceNodes = $options['replaceNodes'] ?? true;
41: }
42:
43: /**
44: * Get name resolution context.
45: */
46: public function getNameContext(): NameContext {
47: return $this->nameContext;
48: }
49:
50: public function beforeTraverse(array $nodes): ?array {
51: $this->nameContext->startNamespace();
52: return null;
53: }
54:
55: public function enterNode(Node $node) {
56: // Dispatch on the node type in a single check, rather than a chain of instanceof checks
57: switch ($node->getType()) {
58: case 'Stmt_Namespace':
59: assert($node instanceof Stmt\Namespace_);
60: $this->nameContext->startNamespace($node->name);
61: break;
62: case 'Stmt_Use':
63: assert($node instanceof Stmt\Use_);
64: foreach ($node->uses as $use) {
65: $this->addAlias($use, $node->type, null);
66: }
67: break;
68: case 'Stmt_GroupUse':
69: assert($node instanceof Stmt\GroupUse);
70: foreach ($node->uses as $use) {
71: $this->addAlias($use, $node->type, $node->prefix);
72: }
73: break;
74: case 'Stmt_Class':
75: assert($node instanceof Stmt\Class_);
76: if (null !== $node->extends) {
77: $node->extends = $this->resolveClassName($node->extends);
78: }
79:
80: foreach ($node->implements as &$interface) {
81: $interface = $this->resolveClassName($interface);
82: }
83:
84: $this->resolveAttrGroups($node);
85: if (null !== $node->name) {
86: $this->addNamespacedName($node);
87: } else {
88: $node->namespacedName = null;
89: }
90: break;
91: case 'Stmt_Interface':
92: assert($node instanceof Stmt\Interface_);
93: foreach ($node->extends as &$interface) {
94: $interface = $this->resolveClassName($interface);
95: }
96:
97: $this->resolveAttrGroups($node);
98: $this->addNamespacedName($node);
99: break;
100: case 'Stmt_Enum':
101: assert($node instanceof Stmt\Enum_);
102: foreach ($node->implements as &$interface) {
103: $interface = $this->resolveClassName($interface);
104: }
105:
106: $this->resolveAttrGroups($node);
107: $this->addNamespacedName($node);
108: break;
109: case 'Stmt_Trait':
110: assert($node instanceof Stmt\Trait_);
111: $this->resolveAttrGroups($node);
112: $this->addNamespacedName($node);
113: break;
114: case 'Stmt_Function':
115: assert($node instanceof Stmt\Function_);
116: $this->resolveSignature($node);
117: $this->resolveAttrGroups($node);
118: $this->addNamespacedName($node);
119: break;
120: case 'Stmt_ClassMethod':
121: case 'Expr_Closure':
122: case 'Expr_ArrowFunction':
123: assert($node instanceof Stmt\ClassMethod
124: || $node instanceof Expr\Closure
125: || $node instanceof Expr\ArrowFunction);
126: $this->resolveSignature($node);
127: $this->resolveAttrGroups($node);
128: break;
129: case 'Stmt_Property':
130: assert($node instanceof Stmt\Property);
131: if (null !== $node->type) {
132: $node->type = $this->resolveType($node->type);
133: }
134: $this->resolveAttrGroups($node);
135: break;
136: case 'PropertyHook':
137: assert($node instanceof Node\PropertyHook);
138: foreach ($node->params as $param) {
139: $param->type = $this->resolveType($param->type);
140: $this->resolveAttrGroups($param);
141: }
142: $this->resolveAttrGroups($node);
143: break;
144: case 'Stmt_Const':
145: assert($node instanceof Stmt\Const_);
146: foreach ($node->consts as $const) {
147: $this->addNamespacedName($const);
148: }
149: $this->resolveAttrGroups($node);
150: break;
151: case 'Stmt_ClassConst':
152: assert($node instanceof Stmt\ClassConst);
153: if (null !== $node->type) {
154: $node->type = $this->resolveType($node->type);
155: }
156: $this->resolveAttrGroups($node);
157: break;
158: case 'Stmt_EnumCase':
159: assert($node instanceof Stmt\EnumCase);
160: $this->resolveAttrGroups($node);
161: break;
162: case 'Expr_StaticCall':
163: case 'Expr_StaticPropertyFetch':
164: case 'Expr_ClassConstFetch':
165: case 'Expr_New':
166: case 'Expr_Instanceof':
167: assert($node instanceof Expr\StaticCall
168: || $node instanceof Expr\StaticPropertyFetch
169: || $node instanceof Expr\ClassConstFetch
170: || $node instanceof Expr\New_
171: || $node instanceof Expr\Instanceof_);
172: if ($node->class instanceof Name) {
173: $node->class = $this->resolveClassName($node->class);
174: }
175: break;
176: case 'Stmt_Catch':
177: assert($node instanceof Stmt\Catch_);
178: foreach ($node->types as &$type) {
179: $type = $this->resolveClassName($type);
180: }
181: break;
182: case 'Expr_FuncCall':
183: assert($node instanceof Expr\FuncCall);
184: if ($node->name instanceof Name) {
185: $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION);
186: }
187: break;
188: case 'Expr_ConstFetch':
189: assert($node instanceof Expr\ConstFetch);
190: $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT);
191: break;
192: case 'Stmt_TraitUse':
193: assert($node instanceof Stmt\TraitUse);
194: foreach ($node->traits as &$trait) {
195: $trait = $this->resolveClassName($trait);
196: }
197:
198: foreach ($node->adaptations as $adaptation) {
199: if (null !== $adaptation->trait) {
200: $adaptation->trait = $this->resolveClassName($adaptation->trait);
201: }
202:
203: if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) {
204: foreach ($adaptation->insteadof as &$insteadof) {
205: $insteadof = $this->resolveClassName($insteadof);
206: }
207: }
208: }
209: break;
210: }
211:
212: return null;
213: }
214:
215: /** @param Stmt\Use_::TYPE_* $type */
216: private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void {
217: // Add prefix for group uses
218: $name = $prefix ? Name::concat($prefix, $use->name) : $use->name;
219: // Type is determined either by individual element or whole use declaration
220: $type |= $use->type;
221:
222: $this->nameContext->addAlias(
223: $name, (string) $use->getAlias(), $type, $use->getAttributes()
224: );
225: }
226:
227: /** @param Stmt\Function_|Stmt\ClassMethod|Expr\Closure|Expr\ArrowFunction $node */
228: private function resolveSignature($node): void {
229: foreach ($node->params as $param) {
230: $param->type = $this->resolveType($param->type);
231: $this->resolveAttrGroups($param);
232: }
233: $node->returnType = $this->resolveType($node->returnType);
234: }
235:
236: /**
237: * @template T of Node\Identifier|Name|Node\ComplexType|null
238: * @param T $node
239: * @return T
240: */
241: private function resolveType(?Node $node): ?Node {
242: if ($node instanceof Name) {
243: return $this->resolveClassName($node);
244: }
245: if ($node instanceof Node\NullableType) {
246: $node->type = $this->resolveType($node->type);
247: return $node;
248: }
249: if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) {
250: foreach ($node->types as &$type) {
251: $type = $this->resolveType($type);
252: }
253: return $node;
254: }
255: return $node;
256: }
257:
258: /**
259: * Resolve name, according to name resolver options.
260: *
261: * @param Name $name Function or constant name to resolve
262: * @param Stmt\Use_::TYPE_* $type One of Stmt\Use_::TYPE_*
263: *
264: * @return Name Resolved name, or original name with attribute
265: */
266: protected function resolveName(Name $name, int $type): Name {
267: if (!$this->replaceNodes) {
268: $resolvedName = $this->nameContext->getResolvedName($name, $type);
269: if (null !== $resolvedName) {
270: $name->setAttribute('resolvedName', $resolvedName);
271: } else {
272: $name->setAttribute('namespacedName', FullyQualified::concat(
273: $this->nameContext->getNamespace(), $name, $name->getAttributes()));
274: }
275: return $name;
276: }
277:
278: if ($this->preserveOriginalNames) {
279: // Save the original name
280: $originalName = $name;
281: $name = clone $originalName;
282: $name->setAttribute('originalName', $originalName);
283: }
284:
285: $resolvedName = $this->nameContext->getResolvedName($name, $type);
286: if (null !== $resolvedName) {
287: return $resolvedName;
288: }
289:
290: // unqualified names inside a namespace cannot be resolved at compile-time
291: // add the namespaced version of the name as an attribute
292: $name->setAttribute('namespacedName', FullyQualified::concat(
293: $this->nameContext->getNamespace(), $name, $name->getAttributes()));
294: return $name;
295: }
296:
297: protected function resolveClassName(Name $name): Name {
298: return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL);
299: }
300:
301: protected function addNamespacedName(Node $node): void {
302: $node->namespacedName = Name::concat(
303: $this->nameContext->getNamespace(), (string) $node->name);
304: }
305:
306: protected function resolveAttrGroups(Node $node): void {
307: foreach ($node->attrGroups as $attrGroup) {
308: foreach ($attrGroup->attrs as $attr) {
309: $attr->name = $this->resolveClassName($attr->name);
310: }
311: }
312: }
313: }
314: