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: | |
16: | protected NameContext $nameContext; |
17: | |
18: | |
19: | protected bool $preserveOriginalNames; |
20: | |
21: | |
22: | protected bool $replaceNodes; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | |
35: | |
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: | |
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: | if ($node instanceof Stmt\Namespace_) { |
57: | $this->nameContext->startNamespace($node->name); |
58: | } elseif ($node instanceof Stmt\Use_) { |
59: | foreach ($node->uses as $use) { |
60: | $this->addAlias($use, $node->type, null); |
61: | } |
62: | } elseif ($node instanceof Stmt\GroupUse) { |
63: | foreach ($node->uses as $use) { |
64: | $this->addAlias($use, $node->type, $node->prefix); |
65: | } |
66: | } elseif ($node instanceof Stmt\Class_) { |
67: | if (null !== $node->extends) { |
68: | $node->extends = $this->resolveClassName($node->extends); |
69: | } |
70: | |
71: | foreach ($node->implements as &$interface) { |
72: | $interface = $this->resolveClassName($interface); |
73: | } |
74: | |
75: | $this->resolveAttrGroups($node); |
76: | if (null !== $node->name) { |
77: | $this->addNamespacedName($node); |
78: | } else { |
79: | $node->namespacedName = null; |
80: | } |
81: | } elseif ($node instanceof Stmt\Interface_) { |
82: | foreach ($node->extends as &$interface) { |
83: | $interface = $this->resolveClassName($interface); |
84: | } |
85: | |
86: | $this->resolveAttrGroups($node); |
87: | $this->addNamespacedName($node); |
88: | } elseif ($node instanceof Stmt\Enum_) { |
89: | foreach ($node->implements as &$interface) { |
90: | $interface = $this->resolveClassName($interface); |
91: | } |
92: | |
93: | $this->resolveAttrGroups($node); |
94: | $this->addNamespacedName($node); |
95: | } elseif ($node instanceof Stmt\Trait_) { |
96: | $this->resolveAttrGroups($node); |
97: | $this->addNamespacedName($node); |
98: | } elseif ($node instanceof Stmt\Function_) { |
99: | $this->resolveSignature($node); |
100: | $this->resolveAttrGroups($node); |
101: | $this->addNamespacedName($node); |
102: | } elseif ($node instanceof Stmt\ClassMethod |
103: | || $node instanceof Expr\Closure |
104: | || $node instanceof Expr\ArrowFunction |
105: | ) { |
106: | $this->resolveSignature($node); |
107: | $this->resolveAttrGroups($node); |
108: | } elseif ($node instanceof Stmt\Property) { |
109: | if (null !== $node->type) { |
110: | $node->type = $this->resolveType($node->type); |
111: | } |
112: | $this->resolveAttrGroups($node); |
113: | } elseif ($node instanceof Node\PropertyHook) { |
114: | foreach ($node->params as $param) { |
115: | $param->type = $this->resolveType($param->type); |
116: | $this->resolveAttrGroups($param); |
117: | } |
118: | $this->resolveAttrGroups($node); |
119: | } elseif ($node instanceof Stmt\Const_) { |
120: | foreach ($node->consts as $const) { |
121: | $this->addNamespacedName($const); |
122: | } |
123: | } elseif ($node instanceof Stmt\ClassConst) { |
124: | if (null !== $node->type) { |
125: | $node->type = $this->resolveType($node->type); |
126: | } |
127: | $this->resolveAttrGroups($node); |
128: | } elseif ($node instanceof Stmt\EnumCase) { |
129: | $this->resolveAttrGroups($node); |
130: | } elseif ($node instanceof Expr\StaticCall |
131: | || $node instanceof Expr\StaticPropertyFetch |
132: | || $node instanceof Expr\ClassConstFetch |
133: | || $node instanceof Expr\New_ |
134: | || $node instanceof Expr\Instanceof_ |
135: | ) { |
136: | if ($node->class instanceof Name) { |
137: | $node->class = $this->resolveClassName($node->class); |
138: | } |
139: | } elseif ($node instanceof Stmt\Catch_) { |
140: | foreach ($node->types as &$type) { |
141: | $type = $this->resolveClassName($type); |
142: | } |
143: | } elseif ($node instanceof Expr\FuncCall) { |
144: | if ($node->name instanceof Name) { |
145: | $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_FUNCTION); |
146: | } |
147: | } elseif ($node instanceof Expr\ConstFetch) { |
148: | $node->name = $this->resolveName($node->name, Stmt\Use_::TYPE_CONSTANT); |
149: | } elseif ($node instanceof Stmt\TraitUse) { |
150: | foreach ($node->traits as &$trait) { |
151: | $trait = $this->resolveClassName($trait); |
152: | } |
153: | |
154: | foreach ($node->adaptations as $adaptation) { |
155: | if (null !== $adaptation->trait) { |
156: | $adaptation->trait = $this->resolveClassName($adaptation->trait); |
157: | } |
158: | |
159: | if ($adaptation instanceof Stmt\TraitUseAdaptation\Precedence) { |
160: | foreach ($adaptation->insteadof as &$insteadof) { |
161: | $insteadof = $this->resolveClassName($insteadof); |
162: | } |
163: | } |
164: | } |
165: | } |
166: | |
167: | return null; |
168: | } |
169: | |
170: | |
171: | private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void { |
172: | |
173: | $name = $prefix ? Name::concat($prefix, $use->name) : $use->name; |
174: | |
175: | $type |= $use->type; |
176: | |
177: | $this->nameContext->addAlias( |
178: | $name, (string) $use->getAlias(), $type, $use->getAttributes() |
179: | ); |
180: | } |
181: | |
182: | |
183: | private function resolveSignature($node): void { |
184: | foreach ($node->params as $param) { |
185: | $param->type = $this->resolveType($param->type); |
186: | $this->resolveAttrGroups($param); |
187: | } |
188: | $node->returnType = $this->resolveType($node->returnType); |
189: | } |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | private function resolveType(?Node $node): ?Node { |
197: | if ($node instanceof Name) { |
198: | return $this->resolveClassName($node); |
199: | } |
200: | if ($node instanceof Node\NullableType) { |
201: | $node->type = $this->resolveType($node->type); |
202: | return $node; |
203: | } |
204: | if ($node instanceof Node\UnionType || $node instanceof Node\IntersectionType) { |
205: | foreach ($node->types as &$type) { |
206: | $type = $this->resolveType($type); |
207: | } |
208: | return $node; |
209: | } |
210: | return $node; |
211: | } |
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: | |
220: | |
221: | protected function resolveName(Name $name, int $type): Name { |
222: | if (!$this->replaceNodes) { |
223: | $resolvedName = $this->nameContext->getResolvedName($name, $type); |
224: | if (null !== $resolvedName) { |
225: | $name->setAttribute('resolvedName', $resolvedName); |
226: | } else { |
227: | $name->setAttribute('namespacedName', FullyQualified::concat( |
228: | $this->nameContext->getNamespace(), $name, $name->getAttributes())); |
229: | } |
230: | return $name; |
231: | } |
232: | |
233: | if ($this->preserveOriginalNames) { |
234: | |
235: | $originalName = $name; |
236: | $name = clone $originalName; |
237: | $name->setAttribute('originalName', $originalName); |
238: | } |
239: | |
240: | $resolvedName = $this->nameContext->getResolvedName($name, $type); |
241: | if (null !== $resolvedName) { |
242: | return $resolvedName; |
243: | } |
244: | |
245: | |
246: | |
247: | $name->setAttribute('namespacedName', FullyQualified::concat( |
248: | $this->nameContext->getNamespace(), $name, $name->getAttributes())); |
249: | return $name; |
250: | } |
251: | |
252: | protected function resolveClassName(Name $name): Name { |
253: | return $this->resolveName($name, Stmt\Use_::TYPE_NORMAL); |
254: | } |
255: | |
256: | protected function addNamespacedName(Node $node): void { |
257: | $node->namespacedName = Name::concat( |
258: | $this->nameContext->getNamespace(), (string) $node->name); |
259: | } |
260: | |
261: | protected function resolveAttrGroups(Node $node): void { |
262: | foreach ($node->attrGroups as $attrGroup) { |
263: | foreach ($attrGroup->attrs as $attr) { |
264: | $attr->name = $this->resolveClassName($attr->name); |
265: | } |
266: | } |
267: | } |
268: | } |
269: | |