| 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: | |
| 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: | |
| 216: | private function addAlias(Node\UseItem $use, int $type, ?Name $prefix = null): void { |
| 217: | |
| 218: | $name = $prefix ? Name::concat($prefix, $use->name) : $use->name; |
| 219: | |
| 220: | $type |= $use->type; |
| 221: | |
| 222: | $this->nameContext->addAlias( |
| 223: | $name, (string) $use->getAlias(), $type, $use->getAttributes() |
| 224: | ); |
| 225: | } |
| 226: | |
| 227: | |
| 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: | |
| 238: | |
| 239: | |
| 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: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 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: | |
| 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: | |
| 291: | |
| 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: | |