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